/Can someone please explain what is happening in this code? What is the outcome and why? I need step by step on this please. I am having a hard time understanding arrays!/
#include <stdio.h>
int main(void) {
int i, t[5];
t[0] = 0;
for(i = 1; i < 5; i++)
t[i] = t[i - 1] + i;
printf("%d",t[4]);
return 0;
}
its simply calculating sum of first 4 numbers.
t[1]=t[0]+1=1; t[2]=t[1]+2=1+2=3; t[3]=t[2]+3=1+2+3=6;
t[4]=t[3]+4=1+2+3+4=10;
Hey pro,
You see the steps here first you initialized an integer array of 5 elements then in the next step you had given its first element an value of 0 then your loop runs for 4 times and has you had not mentioned any brackets in for loop sor the only statement after it is get executed it the loop and the statement t[i] = t[i-1] + i is finding the sum ie t[1] = t[0]+1 = 0+1 =1,t[2]=t[1]+2=1+2=3,t[3]=t[2]+2=3+2=5,t[4]=t[3]+4=5+4=9.
and so finally the output is 9, hope i am right and you find it ok, but if there is any mistake that you might think then please comment it to me.
Happy coding
What does this mean: t[i] = t[i - 1] + i;
What is this for loop saying? for(i = 1; i < 5; i++)