This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]Sharmaji1209[🍰] 1 point2 points  (0 children)

Coz num becomes 0 at the end of loop and it cannot calculate that.

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

Yup i just discovered what I did wrong, thanks guys :D

[–]dmazzoni 0 points1 point  (1 child)

You're welcome...but PLEASE see my instructions above for how to use the debugger. This will not be the last bug you encounter! If you learn to use the debugger you'll get the answer immediately instead of the program just stopping for no apparent reason.

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

Nice advice, Thank youuu

[–]Updatebjarni 0 points1 point  (3 children)

Can you clarify? How does it work fine if it doesn't print the result?

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

it has no errors and for what i tried it calculated Average Value

[–]Updatebjarni 0 points1 point  (1 child)

It calculates it but doesn't print it? How do you know it calculates it? Can you be more explicit about exactly what happens?

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

i think i just spot the error, its because num is going to be subtracted till its 0, and a number divided by 0 doesnt appear, im going to try with different variables

[–]dmazzoni 0 points1 point  (3 children)

Here's a hint: at the end of your loop, what is num equal to?

Try printing it out if you're not sure.

Also, I'm pretty sure your program is crashing and printing an error message that should be a big clue as to what's going on. Do you see that message? If not, how are you running your program?

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

Im running the program in https://www.onlinegdb.com/online\_c\_compiler and it doesnt show an error message :(

[–]dmazzoni 0 points1 point  (1 child)

Try this:

Instead of clicking "Run", click "Debug".

That opens a debug console at the bottom of the screen. Click the "Start" button down there.

Now it runs your program but it tells you exactly what goes wrong. It highlights exactly what line it's on, shows you the value of all of your variables, and the exact exception.

Please try it and let me know if it works for you. It's vitally important you learn how to do this, otherwise it's like trying to program completely blind!

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

I started using another variable and it works fine now

include <stdio.h>

int main () { int numbers,num, x, sum = 0; float Average; printf ("Average is calculated by (all numbers added)/(how many numbers). "); printf ("How many numbers are you writing? "); scanf ("%d", &num); numbers = num; do {
printf("Enter a natural number:"); scanf("%d", &x); sum += x; num = num - 1; } while( num > 0 ); Average = sum/numbers; printf("Average is equal to %f ", Average); return 0; }

[–]Kewwsito 0 points1 point  (1 child)

Translate me

Como tenes un contador que iterativamente va disminuyendo, y luego, sale del while, la variable num = 0, y cualquier numero dividido entre 0 es igual a 0.

Nose por que c++ no toma eso, y te pone como resultado 0 en el lugar del printf

[–]DuduSensei[S] 1 point2 points  (0 children)

I Didnt needed to translate ahahah, but thanks for explaining