- 1
I need some help. Not sure whats wrong with my code
I need to make this code to take 100 numbers and average them, but they keep outputting to the wrong average. Even when I don't have 100 numbers. I'm not sure what is causing it and how to fix it. Here is my code and attempt: https://code.sololearn.com/ca24a8A16a15/#cpp
16 Antworten
+ 1
I put the cin>>x; in the while loop but nothing happens after entering some numbers. It won't average. What is causing that?
+ 1
Some numbers are not enough...Because you are asking for 100 numbers, so you must enter all 100 numbers before it'll output the average, for testing purpose, you can consider making that number to 5
0
You should be putting your cin >> x in the while loop, in your codes, it'll only ask for x once, therefore not giving you average since there's only 1 number
0
Okay, thanks!
0
I have another step to my project, I have to make a similar program but converted into a do-while instead of a while loop. I don't think the order is correct.
Here is my attempt: https://code.sololearn.com/cA3a27a16A4A/#cpp
0
yeah, the sequence are wrong. Firstly, you are going to ask the N before do while loop, in your codes, the N will be changing constantly which I don't think that's what you want? Secondly, the calculation of average should be after the do while loop. This is so that the average will only be calculated after all numbers are inputted.
0
Having the calculation of the average in the if-else statement, does that mean moving the whole if-else statement below the while loop part or after the entire do-while loop?
0
moving the entire if else below the dowhile loop
0
what makes the average output to be 2.07334e-317
0
Can you send the updated version of your codes?
0
Your line 23, you shouldn't display the average before giving it a value, so you should only print out the value after calculating it. The seemingly random average output you got is actually the original value of average in memory that you are supposed to override.
0
Ok, thank you! That worked. I thought that might be the case but wasn't sure.
0
No problem 👌
0
Might have to ask you for more help! You've been very helpful.
0
Write "cin>>x" in the while loop