Standard Deviation Formula

萝らか妹 提交于 2020-06-09 05:27:09

问题


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;

}


回答1:


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

cin >> grade;

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 the while loop. And there is no other line of the program that takes any input from cin. So I never read anything from cin 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, because grade 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 the break at the end of the loop is completely redundant. If you did not have that if, the program would next go back to the top of the loop and then if grade were equal to -1 it would exit. So either way you exit at the end of the loop if grade becomes -1 during the loop.




回答2:


Side note: With while(grade != -1), the if (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)



来源:https://stackoverflow.com/questions/60132671/standard-deviation-formula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!