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?
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.
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86
#include <iostream>
usingnamespace 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(){
longint 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;
}