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;
}