What do you think?

identify the error in the following code segment.

1
2
3
4
5
6
7
int main()
{
int x[8];
 for (int i=0;i<=8;i++) 
x[i]=i;
 return 0;
}



I think that this does anything. What are your views?
Last edited on
I think you want someone to do your homework for you.
No that was not my intention, I already looked at this and thought it returned nothing and that the error was that it did not call for anything. I just wanted another opinion to see if i was correct.
Consider this instead
1
2
3
4
5
6
7
8
int main()
{
   int x[8];
   for (int i=0;i<=8;i++) 
      x[i]=i;
   //...
   return 0;
}


Also, note the indentation
I think that the error is that it returns 0.
No, the error is this i<=8
could you please explain? Is it because since 8is the array and in the loop it said i++ meaning 8+1?
no, the first index of an array is 0. Your array has 8 elements, hence the last valid index is 7. Just count 0...7 (-> 8 indexes)

So your comparison must be i<8 since 8 is alread out ouf bounds
Oh , I forgot that it starts at zero. Thank you so much for your help.
Topic archived. No new replies allowed.