Ok here one question i am really struggling on: -write a program that reads a series of positive numbers from the keyboard and determines and displays the sum of the numbers. Assume that the user types the sentinel value -1 to indicate "end of data entry" I have written this soo far, but cant think what to do next: #include <stdio.h> int main() { int number1, number2,number, number3, sum; printf ("Enter a positive number\n"); scanf ("%d", &number1); printf ("Enter a second postive number\n"); scanf ("%d", &number2); printf ("Enter the third positive number or -1 to end\n"); scanf ("%d", &number); printf ("Enter the fourth positive number or -1 to end\n"); scanf ("%d", &number3); if (number!= -1) { printf ("Enter the fifth positive number or -1 to end\n"); } else printf ("End result:\n"); { sum = number1+number2+number; printf ("The addition of the numbers = %d\n", sum); } return 0; }
Hmmm. let me guess. This is in the "while loop" section of your class. Well, here's a clue: while it's not -1 add newNumber to total
Thanks for ur time.....well the problem is i havent done this in class yet!!!! we are currently just doing the theory and i am the only one to start the practical.......so could u tell me how i could write the while loop thing please???? becuase i read other past questions and i would need that loop thing for further questions......so if u could explain me!!!! thanks.
sorry, I'm afraid I've never done C programming... Search in Google for the correct syntax. I know we're not supposed to help people with their homework here (or is that just Experts Exchange?) but here goes. Logic could be something like: ======== int sum, number1; sum = 0; number1 = 0; printf ("Enter a positive number\n"); scanf ("%d", &number1); while (number1 != -1) do sum += number1; printf ("Enter a positive number\n"); scanf ("%d", &number1); done printf ("The addition of the numbers = %d\n", sum); ========== make sense?
hey geniosity.....thanks again!!!! well i will definetly try this out now.........noo thats not homework. ......its just that i want to get ahead of the class work, it makes me understand more.
hey just checked........theres more to it!!!! if u could just explain me the difference between "while" and "if" statement please....that should be enough!!!! thanks. and could u explain me why u set sum and number1 to 0??? : sum = 0; number1 = 0;
Hello, long total = 0L; int number = 0; for(;;) { scanf ("%d", &number); if(number==-1) break; total += (long)number; } printf("Total = %l\n", total); PHP: Explanation: for(;;) PHP: The syntax of the for statement is: for(initial values; condition; action) Here you want none of these; no condition means the loop will run forever. if(number--1) break; PHP: The break statement can be used to escape a loop block. I'm using a long variable for 'total' so that there is no overflow when adding up all these integers. An alternative, more verbose but beginner-friendly approach would be: do { scanf ("%d", &number); if(number!=-1) total += (long)number; } while(number!=-1); printf("Total = %l\n", total); PHP: Basically, while will execute the whole block until its condition stops being true. if, on the other hand, does this only once. If you do not set total and number to 0, they can represent an arbitrary value; that's the way it is in C. When you create a new variable, it just references a block of memory and you have no guarantee of what that block currently contains.
Sorry, didn't see your response. IF is a yes/no statement WHILE is a loop that happens while something is true I set these variables to 0 just to initialise them, making sure they're 0. Not sure whether that's really necessary, but you never know what value they may hold, I even think in C this is more necessary, because of the way it handles memory. You just want to ensure that these variables are 0 when you start adding to them...
In C -- (as in any language IMHO) -- it is critical to initialize variables to known, safe values. You cannot assume that a compiler will assign an appropriate starting value to a variable. How can it know what you intend? Compilers should choke on this. An uninitialzed variable could crash the program and possibly the computer. This is an important source of holes in programs. In essence, you use IF statements to redirect code. In your case, you should be saying that if the number is greater than zero, add it to the running total, otherwise do nothing. The IF statement immediately goes on to the next statement in the code once it has done its work. In loops, WHILE and FOR statements also test whether something is true, with the difference that they keep on working until that condition is met. IF statements run once. WHILE loops can run indefinitely.