Mar 25, 2013 at 12:51pm UTC
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 Mar 25, 2013 at 12:53pm UTC
Mar 25, 2013 at 12:55pm UTC
I think you want someone to do your homework for you.
Mar 25, 2013 at 1:01pm UTC
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.
Mar 25, 2013 at 1:17pm UTC
I think that the error is that it returns 0.
Mar 25, 2013 at 1:25pm UTC
No, the error is this i<=8
Mar 25, 2013 at 1:33pm UTC
could you please explain? Is it because since 8is the array and in the loop it said i++ meaning 8+1?
Mar 25, 2013 at 2:03pm UTC
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
Mar 25, 2013 at 2:12pm UTC
Oh , I forgot that it starts at zero. Thank you so much for your help.