+ 2
Why there isn't an error in this statement ( a[0-1]) ?
Here is the code ,assume all necessary libraries included: int main() { int a[5] = {1,2,3,4,5}; for (int i=0; i<5; i++) { a[i]= 2* a[i-1]; cout<<i; } return 0; }
12 Answers
+ 7
compiler ignored error because it is a runtime exception
+ 6
Start the for loop at 1 to fix. Then change a[0] however you wanted it to be.
+ 3
i=0
a [i-1] = a [ -1] = error; a>=0
+ 3
There is an error i.e. a [-1]
+ 1
Because there is no a[-1]. Arrays starts at index 0.
Try this instead :
...
for (int i=1; i<5; i++) {
a[i] = 2 * a[i-1];
cout << i;
}
...
+ 1
Thank you for your help , I appreciate it.
+ 1
Sorry for disturbing you , but why this error is ignored by compiler ?
0
no problem ;)
0
I thought the same thing , but when i run this program the output is 01234.
0
@mohammad, the output of this code (if you ignore the a[-1]) is 01234 because you wrote :
...
for (int i = 0; i < 5; i++) {
...
cout << i;
}
...
0
good question... :/
0
@NimWing Yuan , thanks :)