Question

Please help me my head is ready to blow

#include<stdio.h>
int main(void){
    unsigned short sum1=0;unsigned short counter=0;

    printf("Enter the number of integers you want to sum\n");scanf("%hd",&counter);
    for (unsigned int i=1;i<=counter;++i)
    { 
        printf("The i is %d and the sum is %d\n",i,sum1);
        sum1 =0;// 2 iteration sum =0;
        printf("The i is %d and the sum is %d\n",i,sum1);

        for(unsigned int j=1;j<=i;++j)
            sum1 =sum1+j;// 1 iteration sum=1;
        printf("The i is %d and the sum is %d\n\n",i,sum1);
    }
return 0;
}

Until now the book I read In the nested loops used to put curly braces But not in this example... Question 1) Why in the second iteration sum will be 3 and not 2 (I ask this because sum initializes to 0 before go to the nested for ) ? Question 2) Why when I want to printf() the j hits error ? Can anyone explain me EXACTLY HOW THIS PROGRAM WORKS ? I mean 1st iteration,2nd Iteration.... Thank you Brothers....

Was it helpful?

Solution

This code:

for (unsigned int i=1;i<=counter;++i)
{ printf("The i is %d and the sum is %d\n",i,sum1);
sum1 =0;// 2 iteration sum =0;
printf("The i is %d and the sum is %d\n",i,sum1);
for(unsigned int j=1;j<=i;++j)
sum1 =sum1+j;// 1 iteration sum=1;
printf("The i is %d and the sum is %d\n\n",i,sum1);}

is equivalent to:

for (unsigned int i=1;i<=counter;++i) { 
    printf("The i is %d and the sum is %d\n",i,sum1);
    sum1 =0;// 2 iteration sum =0;
    printf("The i is %d and the sum is %d\n",i,sum1);
    for(unsigned int j=1;j<=i;++j) {
        sum1 =sum1+j;// 1 iteration sum=1;
    }
    printf("The i is %d and the sum is %d\n\n",i,sum1);
}

This is because in for-loops without braces, only the very next line is included in the loop.

Now in the first iteration, you will get:

"The i is 1 and the sum is 0"
"The i is 1 and the sum is 0"
"The i is 1 and the sum is 1" //Enters inner for-loop

Second:

"The i is 2 and the sum is 1" //Hasn't reset yet
"The i is 2 and the sum is 0" //Reset
"The i is 2 and the sum is 3" //Sum was 0, then added 1 when j was 1, 
                              //then added 2 when j was 2

Now, the reason you can't print j, is because your printf statements are all outside of your inner for-loop, so j is not defined :)

OTHER TIPS

In C you cannot declare your variables inside a for-loop sequence:

for(int i=0; i<=10; i++) is wrong
And
int i;
for (i=0; i<=10; i++) is correct

Also you can say int a,b,c=3; instead of declaring them separately int a, int b, int c=3;

To help with your question ( I wish I could comment, but I need more reputation), if your statement (for, if, while) has just one (or none) operations to do, you don't need the curly braces:
for (i=0; i<=10; i++)
        printf("%i ", i);

When having more operations, you need a curly brace for the compilator to know how many of them are inside the for loop:

for (i=0; i<=10; i++){
        printf("%i ", i);
        if(i%2==1)
                printf("Odd number");
        printf("\n");
}

Edit:
int i;
for(i=0; i<=10; i++){
     int j = i+5;
     printf("%i", j);
}
Works very well, but j will not be available outside the for loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top