标准偏差公式 [英] Standard Deviation Formula
问题描述
我有此作业,我将在其中显示提示:
I have this assignment in which i will show the prompt for it:
编写一个程序,该程序从用户读取一组测试成绩数据. (测试 分数应以十进制形式输入,以表示百分比等级.一旦用户拥有 输入完测试分数(输入-1表示输入结束),打印出 输入的测试,测试的平均值和标准偏差.使用以下公式 (Welford方法)来计算标准差: (标准Deviaton公式)
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;
}
推荐答案
我发现调试程序的好方法通常是假装我是计算机,并尝试一次单行浏览该程序以了解什么我知道.
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;
然后我从输入中读取一个数字,并将grade
设置为该数字.
So then I read a number from the input and set grade
to that number.
然后进入while
循环.请注意,我绝不会回到while
循环之前的任何行.程序中没有其他行从cin
获取任何输入.因此,我再也不会从cin
读取任何内容.
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.
您可以在第一个数字之后键入所需的所有数字.该程序没有读取它们中的任何一个.它正在忙于您要执行的操作,这是在保持循环的同时
grade
不等于-1.
它将继续循环,直到发生其他情况导致程序无法运行,
因为grade
永远不会在循环内被更改.
每次到达循环顶部时,grade
仍等于您输入的第一个数字.
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.
顺便说一句,循环末尾的if
和break
是完全多余的.
如果您没有if
,则该程序接下来将返回到循环的顶部,然后如果grade
等于-1,则它将退出.因此,如果grade
在循环过程中变为-1,则您都可以在循环末尾退出.
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.
这篇关于标准偏差公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!