...

View Full Version : Resolved Unwanted addition in my C program using "while"



AGENT_P6
09-05-2012, 05:50 AM
SOLVED

Hello all. I am brand new to programming and would like just an explanation as to exactly why I am getting a specific result

After learning if, else, while, and a couple other things I decided to think of my own little simple program to see how I do without copying from the book. Figured it would help me gain some basic critical thinking for programming when I'm the one creating my own problems and fixing my own problems. Everything went according to the way I wanted it to, however it seems that somehow I am getting one final unwanted addition outside my while loop, which is what I'm trying to figure out.

Here's the code (My program goal parameters written in the comment on top)


/* A program that counts from 0-100 in increments of 2, 0-150 in increments of 3, and 0-50 in increments of 1 */
#include <stdio.h>
main()
{
int doubles = 0, triples = 0, singles = 0;

while (doubles <= 100){
printf("doubles tally: %d\n", doubles);
printf("triples tally: %d\n", triples);
printf("singles tally: %d\n", singles);
doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;
}
printf("dude, that's a lot of doubles, triples, and singles\n");
printf("doubles total: %d\n", doubles);
printf("triples total: %d\n", triples);
printf("singles total: %d\n", singles);

return 0;
}


When I build and run, everything is just peachy except the doubles total is 102, the triples total is 153, and singles total is 51, when they should all respectively be 100, 150, and 50 (or at least that was the plan anyway:o)

If I remove my four final printf statements, the totals become what I want them to...but I don't understand how the printfs cause the values to change.

The only explanation I can come up with is that the inclusion of the printfs actually for some reason cause the while to be looped one final [unwanted] time???

Thanks for your help:D:thumbsup:

EDIT: I'm 27 and work full time; this is not a homework assignment and I am not a student. (I'm teaching myself via some reading materials and online resources)
Just didn't want any miscommunication since school is starting back up and homework assignments aren't allowed on the forums.

Fou-Lu
09-05-2012, 07:13 AM
Its a simple issue with your loop condition: while (doubles <= 100){. When doubles reaches 100, it proceeds to loop for a final time. Simply compare to < 100 and it will give you the final results of 50, 100 and 150.

AGENT_P6
09-05-2012, 12:03 PM
Its a simple issue with your loop condition: while (doubles <= 100){. When doubles reaches 100, it proceeds to loop for a final time. Simply compare to < 100 and it will give you the final results of 50, 100 and 150.

Thanks for the reply Fou-Lu.
Although I understand what you're saying, Im still unsure because if the four printf statements are taken away, i get 50,100,150.

So what Im saying is when doubles reaches 100 and those four printf statements at the bottom are taken out of the program, the loop does NOT execute that one last time.

Alternatively, with those printf statements in there, when doubles reaches 100, the loop DOES execute one last time

Im trying to figure out how in the heck a printf causes this

abduraooft
09-05-2012, 02:07 PM
if the four printf statements are taken away, i get 50,100,150.
The increment statements come after the first three printf statements.

Change your code to

while (doubles < 100){
doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;
printf("doubles tally: %d\n", doubles);
printf("triples tally: %d\n", triples);
printf("singles tally: %d\n", singles);

} and see what happens

AGENT_P6
09-05-2012, 05:35 PM
Hey again and thanks for the replies.

I understand that I should be using "is less than 100" rather than "is equal to or less than 100" in order to get the desired results

What I'm trying to figure out is why this

/* A program that counts from 0-100 in increments of 2, 0-150 in increments of 3, and 0-50 in increments of 1 */
#include <stdio.h>
main()
{
int doubles = 0, triples = 0, singles = 0;

while (doubles <= 100){
printf("doubles tally: %d\n", doubles);
printf("triples tally: %d\n", triples);
printf("singles tally: %d\n", singles);
doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;
}
printf("dude, that's a lot of doubles, triples, and singles\n");
printf("doubles total: %d\n", doubles);
printf("triples total: %d\n", triples);
printf("singles total: %d\n", singles);

return 0;
}

will give me the totals of 102, 153, and 51 WHEN THIS:


/* A program that counts from 0-100 in increments of 2, 0-150 in increments of 3, and 0-50 in increments of 1 */
#include <stdio.h>
main()
{
int doubles = 0, triples = 0, singles = 0;

while (doubles <= 100){
printf("doubles tally: %d\n", doubles);
printf("triples tally: %d\n", triples);
printf("singles tally: %d\n", singles);
doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;

}
return 0;
}

will give me 100, 150, and 50. Why does the inclusion of the last 4 printf statements cause the loop to execute 51 times, when the EXCLUSION of the last 4 printf statements causes the loop to only execute 50 times

Sorry for the confusion, as this stuff is new to me.

I fully understand that I should be using "less than" rather than "equal to or less than". The purpose of my inquiry is only to understand WHY my the first set of code in this post makes for totals of 102, 153, and 51, yet the second set of code in this post results in 100, 150, and 50 when they are BOTH using the "equal to or less than" operator

Sorry to be a pain...I'm just really inquisitive and want to know exactly why things work the way that they do :)

Fou-Lu
09-05-2012, 07:31 PM
The first one shows 1 to many versus just removing them since the values are incremented in the loop after the printfs. When condition is <=100, then it increments them once more leaving them with the values 51, 102 and 153.
Removing the = sign will leave the variables with the values 50, 100, and 150 after the loop, so you can still add the last four printf's if desired.

tracknut
09-05-2012, 07:38 PM
while (doubles <= 100){
printf("doubles tally: %d\n", doubles);
printf("triples tally: %d\n", triples);
printf("singles tally: %d\n", singles);
doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;
}

This code will print out (on its last iteration through the loop) 100, 150, 50, but immediately after you print those values out, you then execute:


doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;

Which sets doubles to 102, triples to 153 and singles to 51. Correct?

Then the loop exits, and in one example you print the final values of these variables (102, 153, 51). In the other example, you don't print them out so you don't notice that you incremented them.

Dave

AGENT_P6
09-05-2012, 08:16 PM
while (doubles <= 100){
printf("doubles tally: %d\n", doubles);
printf("triples tally: %d\n", triples);
printf("singles tally: %d\n", singles);
doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;
}

This code will print out (on its last iteration through the loop) 100, 150, 50, but immediately after you print those values out, you then execute:


doubles = doubles + 2;
triples = triples + 3;
singles = singles + 1;

Which sets doubles to 102, triples to 153 and singles to 51. Correct?

Then the loop exits, and in one example you print the final values of these variables (102, 153, 51). In the other example, you don't print them out so you don't notice that you incremented them.

Dave

This explains it :D

It makes sense now. The program always counted to the wrong numbers, but only one set of code had the final printfs to make me aware of that 51st loop.

Told u i was new to this *facepalm*

Im going to click the thanx button for all of u tho, as all of your posts helped me learn and understand more.

:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum