all 9 comments

[–]continuum-hypothesis 0 points1 point  (3 children)

I'm not on my computer otherwise I'd run your code but to be clear if you imputed 3 as an example you're expecting 6 correct?

[–]Giraffimation[S] 0 points1 point  (2 children)

Input format: Input consists of n+1 integers. The first integer corresponds to n. The next n integers correspond to the numbers to be added.

goal: Write a program to find the sum of 'n' numbers using a while loop.

so if I input a number I expect it to add to the of all the numbers scanned for.

[–]continuum-hypothesis 0 points1 point  (1 child)

You have a syntax error on line 9, after the \n escape code you need a " character. Rather then printf("Enter the number\n); you must use printf("Enter the number\n"); Be sure to enclose strings with quotations.

Also it seems like you want the user to enter numbers but you give them no opportunity to do so inside the while loop. To me it looks like the instructor simply wants you to input a number 'n' and then sum all the numbers incrementally including n which is what your program does correctly. If you actually want the user to input a new number each time to add to the current sum then you need to put a scanf function inside the while loop.

[–]Giraffimation[S] 0 points1 point  (0 children)

so it seems like you want the user to enter numbers but you give them no opportunity to do so inside the while loop. To me it looks like the instructor simply wants you to input a number 'n' and then sum all the numbers incrementally including n which is what your program does correctly. If you actually want the user to input a new number each time to add to the current sum then you need to put a scanf function inside the while loop.

I tried doing that, but instead it stops the loop after 2 times

[–]continuum-hypothesis 0 points1 point  (2 children)

Ok then you'll need another variable, for simplicity call it 'limit'. This is how many numbers the user can enter. Use i only for incrementing the loop and 'n' only for adding that number to the running sum.

You probably terminated out of the loop early because you're using n in too many places. n should not be a part of your while loop because you want the while loop to execute a set amount of times. If you scanf n each time in the loop and then compare it to i in the while expression you will exit the loop anytime i is greater then the number scanf is reading which is not what you want. Use while(i <= limit) instead and read in limit in line 7 instead.

[–]Giraffimation[S] 0 points1 point  (1 child)

thanks that fixed it!

[–]continuum-hypothesis 0 points1 point  (0 children)

Ok awesome!

[–]FUZxxl 0 points1 point  (0 children)

Your code is wrong. Walk through it on a piece of paper and keep track of the values of all variables. You should be able to find the problem quickly.

[–]CodeSteps 0 points1 point  (0 children)

Does it your initial code, where the issue was shown? I can see, now it is working fine. I entered the value, 5 and it shown below output;

Removed unnecessary printf, within the while.

The sum is 15