Debugging

Hi everyone,

I was told that there are 6 bugs in the code below. So far I've found only 4 of them. You can see the corrections I made in comments. Could you give me a hand to find the rest of them?

Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;

void display(long a[5],int times){
    
    cout << times << "-th loop result : ";
    
    for(int i=0;i<5;i++){ 
        cout << "a[" << i <<"]= " <<  a[i] << " " ;
    }
    
    cout << endl;
    
}

int main(){
    
    long  int product; // long int product = 1
    long  productArray[5];
    
    for( int i=1; i<=5 ; i++){
        product *=i;
        productArray[i] = product; // productArray[i-1] = product;  
        display(productArray,i);
    }
    
    int i=0,j; // int i=0, j=0;
    int index[5];
    
    while(j<5){
        int remainder = productArray[i]%4;
        if(remainder=0){ // if(remainder == 0)
            index[j] = i;
            j++;
        }
        i++;
    }
    
    cout << "values in productArray( mod 4 equal to 0)  = ";
    while(j<5){
        cout << "a[" << productArray[index[j]] << "] ";
        j++;
    }
    
    cout << endl;
    
    return 0;
}
With the Microsoft compiler:
-W4 -analyze catches (generates warnings for) four errors
C++ core guidelines check catches (generates warnings for ) one more error
Peer review catches one logical error.

There may be more errors: ergo, the need to test the code thoroughly.
There may be undetected errors even after testing: that is a fact of life.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86

#include <iostream>
using namespace std;

void display(long a[5],int times){
    
    cout << times << "-th loop result : ";
    
    for(int i=0;i<5;i++){ 
        cout << "a[" << i <<"]= " <<  a[i] << " " ;
    }
    
    cout << endl;
    
}

int main(){
    
    long  int product; // (1) *** warning C26494: Variable 'product' is uninitialized. Always initialize an object. 
    long  productArray[5]; // (2) *** warning C26494: Variable 'productArray' is uninitialized. Always initialize an object.
    
    for( int i=1; i<=5 ; i++){ // (3) *** warning C6201: Index '5' is out of valid index range '0' to '4' for possibly stack allocated buffer 'productArray'.
        product *=i; // (1) *** warning C6001: Using uninitialized memory 'product'.
                     // (1) *** warning C4700: uninitialized local variable 'product' used
        productArray[i] = product; // (3) *** warning C6201: Index '5' is out of valid index range '0' to '4' for possibly stack allocated buffer 'productArray'.
        display(productArray,i); // (2) *** warning (code review): elements of the array at positions > i are not initialised
    }
    
    int i=0,j; 
    int index[5];
    
    while(j<5){ // (4) *** warning C6001: Using uninitialized memory 'j'.
                // (4) ***  warning C4700: uninitialized local variable 'j' used
        int remainder = productArray[i]%4;
        if(remainder=0){  //  (5) *** warning C6282: Incorrect operator:  assignment of constant in Boolean context. Consider using '==' instead.
                          //  (5) *** warning C4706: assignment within conditional expression
            index[j] = i;
            j++;
        }
        i++;
    }
    
    cout << "values in productArray( mod 4 equal to 0)  = ";
    while(j<5){ // (6) *** warning (code review): dead code: j is always >= 5 at this point 
        cout << "a[" << productArray[index[j]] << "] ";
        j++;
    }
    
    cout << endl;
    
    return 0;
}
Topic archived. No new replies allowed.