Hi I'm a newbie at programming working with stephen kochan's programming in C. I have an encountered a code in the book where I am unable to understand the output and utterly frustrated after spending hours. Below is the code
Code:
#include<stdio.h>
int main(void)
{
int numbers[10] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int i, j;
for(j = 0; j < 10; ++j)
for(i = 0; i < j; ++i)// does i remain 0 throughout?
numbers[j] += numbers[i];
for(j = 0; j < 10; ++j)
printf("%i ", numbers[j]);
printf("\n");
return 0;
}
in my mind, the 0th index of both j and i is 1. Therefore, the first value of numbers[j] += number[i] should be 2.
However, the output of the program is:
1, 1, 2, 4, 8, 16, 32, 64, 128, 256
As i understand, the elements of the array j are getting added up with previous elements and as a result 1 + 1 = 2 and 2 + 1+ 1 =4, etc.
So what happens to i. Please explain why does i not get executed throughout.