+ 6
What is this output & why ?
I wrote a code in which I declared an array & assigned no value to any index of the array & printed out the values from array , I got confusing output, pattern observed was that even index got non-zero number & odd index got 0 always . Why is this so ? https://code.sololearn.com/cPwJUBx51o3b/?ref=app
3 Answers
+ 1
You probably had small 64-bit numbers stored there, so every other 32-bits (the size of int) are zeros
+ 1
Your array wasn't initialized so it contains garbage values. Please read these previous discussions regarding array and variable initialization:
See explanation from ~ swim ~ here:
https://www.sololearn.com/Discuss/1128723/?ref=app
See explanation from Dennis, and ~ swim ~ here:
https://www.sololearn.com/Discuss/1744856/?ref=app
You can simply change the array declaration to `int a[7] {};` this way the array will be initialized with default int type value (zero).
+ 1
it is a wild value, the last value stored in the memory address you ordered, use this function to clear it
#include <iostream>
using namespace std;
void clear_arr(int *array, unsigned sum_elmt) {
for (unsigned i = 0; i < sum_elmt; ++ i)
array[i] = 0;
}
int main() {
int arr[7];
clear_arr(arr, 7);
cout<<arr[0]<<endl<<arr[1]<<endl<<arr[2]<<endl<<arr[3]<<endl<<arr[4]<<endl<<arr[5]<<endl<<arr[6];
return 0;
}



