Drai
Drai

Reputation: 1

C Programming, Loop difficulties

I'm currently working on a problem and it's very annoying. It's a loop that I know how to do but trying to understand -WHY- the second case does not work.

The first part is fine.

int n;
int trianglenumber;
trianglenumber = 0;
for (n = 1; n <= 200; n++)
    trianglenumber = trianglenumber + n;
printf("%I,", trianglenumber");

Output is 20100

int n;
int trianglenumber;
trianglenumber = 0;
int result = 0;
for (n = 1; n <= 200; n++)
    result = trianglenumber + n;
printf("%I,", trianglenumber"); 

The program responds with the solution of 0 which I do not understand. Why do you need to use trianglenumber twice and then add it to n? Why can't you just set any variable to take the trianglenumber's place like I did with result and get the same answer? Much appreciated. Just trying to figure this out. It's pretty basic I guess but I don't see it.

Upvotes: 0

Views: 83

Answers (4)

Kanwaljit Singh
Kanwaljit Singh

Reputation: 4377

  1. The program responds with the solution of 0 which I do not understand.

You are printing "trianglenumber" which is not incrementing at all. As "trianglenumber" is zero as you intilialize so output is zero.

  1. Why do you need to use trianglenumber twice and then add it to N?

First we create "trianglenumber" variable as type of integer. Second "trianglenumber=0" because if you not initialize it, it may take some garbage value and you can't get desired output.

Writing trianglenumber = trianglenumber + n; because in each iteration value of trianglenumber gets incremented.

For example-

In first iteration.

trianglenumber = trianglenumber + n;

trianglenumber = 0 + 1 = 1

In second iteration n = 2 and trianglenumber becomes 1 from first iteration.

trianglenumber = trianglenumber + n;

trianglenumber = 1 + 2 = 3

Similarly in each iteration.

  1. I think from both above answers your 3rd question is answered.

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

In second case, trianglenumber + n; does not increment trianglenumber. So, in every iteration of the loop, you are putting just the value of n in result.

Finally, you end up printing the trianglenumber which is still at the first assigned value, 0. That justifies the output you get.

That said, %I is not a standard conversion specifier, you should use %i. Using invalid conversion specifier invokes undefined behavior.

Upvotes: 2

Teshtek
Teshtek

Reputation: 1342

I have a solution, there are a lot of errors :

#include <stdio.h>
int main (){
    int n;
    int trianglenumber=0;
    for(n = 1; n<=200; n++)
        trianglenumber += n;
    printf("%i", trianglenumber); 
return 0;
}

Use just triangualrnumber witout the result, also %I dosen't exist, put the "" correctlly, and triangualnumber never increments, it's just 0

Upvotes: 0

axiac
axiac

Reputation: 72177

I commented out in the second fragment of code the lines that do not contain trianglenumber. This is the result:

// int n;
int trianglenumber;
trianglenumber = 0;
// int result = 0;
// for(n = 1; n<=200; n++)
    result = trianglenumber + n;
printf("%I,", trianglenumber"); 

As you can see, trianglenumber is initialized with 0 and never modified. The last line of code prints its value that was 0 all the time during the program execution1.


1 This is not entirely true but it's ok for the scope of this discussion.

Upvotes: 0

Related Questions