I have this assignment in which i will show the prompt for it:
Write a program that reads in a set of test score data from the user. (Test scores should be entered in decimal form to represent the percentage grade). Once the user has finished entering the test scores (entering -1 will signal the end of input), print out the number of tests entered, the average of the tests, and the standard deviation. Use the following formula (Welford’s method) to calculate the standard deviation: (Standard Deviaton Formula)
You can compute this quantity by keeping track of the count (number of tests), the sum, and the sum of squares as you process the input values. Note: Although there are other ways to calculate the standard deviation, please use this method. This method is used since it only requires one pass of the data. Do not use arrays or vectors.
Now the code below is what I have so far. In the terminal, I'm getting wrong numbers for the average and Std. Deviation outputs (Terminal Output). Is there anything wrong with my math? Any help is appreciated.
#include <iostream>
#include <cmath>
using namespace std;
int main()
double sum = 0;
double sumSq = 0;
double grade = 0;
int numtests = 0;
cout << "Enter the test scores. Once finished, enter -1 to end input.\n";
cin >> grade;
while(grade != -1)
{
cin >>grade;
sum = sum + grade;
sumSq = sumSq + pow(grade,2);
numtests++;
}
double average =(sum/numtests);
double std = (sumSq - numtests*pow(average,2)/(numtests - 1));
cout << "The number of scores: "<<numtests<<"\n";
cout << "Average: "<<average<<"\n";
cout << "Std. Deviation: "<<std<<"\n";
return 0;
}
Side note: With
while(grade != -1)
, theif (grade != -1) break;
is redundant/superflous.But, when you try to square a number, you are using
pow(2,x)
which is "2 raised to the x power"--Not what you want.To square a number
x
, you want:pow(x,2)
I find a good way to debug a program is often to pretend that I am the computer, and try stepping through the program one line at a time to see what I do.
If I follow your program, after a few lines I get to the statement
So then I read a number from the input and set
grade
to that number.Then I enter the
while
loop. Notice that I never go back to any line earlier than thewhile
loop. And there is no other line of the program that takes any input fromcin
. So I never read anything fromcin
again.You can type all the numbers you want after the first one. The program isn't reading any of them. It's busy doing what you told it to do, which is to keep looping while
grade
is not equal to -1. It will continue looping until something else happens to stop the program from running, becausegrade
is never getting changed within the loop. Every time you come to the top of the loop,grade
is still equal to the first number you entered.By the way, the
if
with thebreak
at the end of the loop is completely redundant. If you did not have thatif
, the program would next go back to the top of the loop and then ifgrade
were equal to -1 it would exit. So either way you exit at the end of the loop ifgrade
becomes -1 during the loop.