Hi Vrsa, the issue is your program invokes undefined behavior, so there's no point in trying to make sense of the output itself.
Your array is declared as having a size of 5.
This means it has 5 slots for numbers to be assigned to.
x[0], x[1], x[2], x[3], and x[4] are valid elements to access. Notice that it starts at 0, not 1.
After the for loop, your i variable still exists and is set to the value 5. The element at x[5] doesn't exist. You should not try to access it.
You can avoid this problem by limiting the i variable to the scope of the for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
usingnamespace std;
int main()
{
int x[5];
for (inti = 0; i < 5; i++){
x[i] = i;
}
cout << x[i] << " "; // COMPILER ERROR: i is not defined in this scope
return 0;
}