2014-02-07 96 views
-1

好吧,我正在尝试使用'while'循环编写一个程序来计算某个测试分数的平均值。我的第一个输入是测试的数量,之后的每个输入都是一组分数(所以第一个输入是5,然后接下来的5个输入是5个不同的测试分数)。我必须一次输入所有变量,以便找到所有测试分数的总和,然后计算平均值。使用while循环计算平均测试分数

我完全停留在如何做到这一点,甚至不知道从哪里开始。

回答

0

,让你开始但不能透露太多......

要做到这一点,我认为要做到这一点的方式:

首先需要输入的测试#。

Cin >> test_amt; 

好的,现在你知道有多少测试。大!所以现在你应该使用一个while循环来完成每个测试并为分数输入!我会在这里使用for循环,但如果你想使用while循环,那么肯定。

counter = 0; 
while(counter != test_amt-1) 
{ 
    cin >> score; 
    //I would use a vector to store each score. 
    //this way, you easily know which score matches up with which test 
    score = 0; 
    counter++; 
} 

我希望这可以让你开始。如果没有,请让我知道,我会很乐意帮助更多。

1

一些伪

total <- 0 
N <- input number of tests 
i <- 1 
while i <= N 
    data[i] <- input data 
    total <- total + data[i] 
    i <- i + 1 
avg <- total/N 
0

通过逐个添加它们并最终获得平均值,将所有分数取单个变量。下面我给出了一个提示,将帮助您使用while循环。但你可以使用你自己的逻辑:

int test_amount, counter = 0, total = 0, score; 
int avg; 

cout<<"Enter test amount"<<endl; 
cin>>test_amount; 
while(counter < (test_amount-1)) 
{ 
    cout<<"Enter the test score"<<endl; 
    cin>>score; 
    total=total+score; 
    counter++; 
} 
avg = total/test_amount; 

如果你想存储的分数,以及你可能会想到向量或数组。由于“测试数量”的数量并不固定,您还可以使用动态数组,链接列表等。考虑其他方法,发布自己的代码,Google也提供各种帮助。