2012-03-21 24 views
0

我必须找到给定的最低输入,然后平均减去最低分。我遇到了一些麻烦,我的averageScore函数发现阵列中得分最低。我得到非常奇怪的数字作为我的输出。任何建议如何调整这将不胜感激。提前致谢。从阵列中删除最低的输入?

#include <iostream> 
#include <cstdlib> 
using namespace std; 

//function prototypes 
double* allocate(int&); 
double averageScore(int&); 

int main() 
{ 
    double* testArray; 
     int numOfScores; 
    double average; 

    testArray = allocate(numOfScores); 
    average = averageScore(numOfScores); 

    //delete memory created 
    delete[] testArray; 

    return 0; 
} 

//function to collect user info, dynamically allocate 
double* allocate(int &numOfScores) 
{ 
    double* testArray; 

    //prompt user for scores 
    cout << "How many test scores would\n"; 
    cout << "you like to process: "; 

    //user input validation 
    if(!(cin >> numOfScores)) 
    { 
     cout << "Invalid input!\n"; 
     cout << "Program termination, please\n"; 
     cout << "restart the program." << endl; 
     exit(0); 
    } 
    else if(numOfScores < 0) 
    { 
     cout << "Invalid input!\n"; 
     cout << "Program termination, please\n"; 
     cout << "restart the program." << endl; 
     exit(0); 
    } 

    //dynammically allocate an arrray to hold the scores 
    testArray = new double[numOfScores]; 

    //get the scores from user 
    for (int count = 0; count < numOfScores; count++) 
    { 
     cout << "Enter Score: "; 

     //user input validation 
     if(!(cin >> testArray[count])) 
     { 
      cout << "Invalid input!\n"; 
      cout << "Program termination, please\n"; 
      cout << "restart the program." << endl; 
      exit(0); 
     } 
     else if(testArray[count] < 0.0) 
     { 
      cout << "Invalid input!\n"; 
      cout << "Program termination, please\n"; 
      cout << "restart the program." << endl; 
      exit(0); 
     } 


    } 

    return testArray; 
} 

//function to calculate the average score 
double averageScore(int &numOfScores) 
{ 
    double* testArray; 

    double total, 
      average, 
      scores[0], 
      lowest; 

    lowest = scores[0]; 

    //calculate total scores entered 
    for(int count = 0; count < numOfScores; count++) 
    { 
     total += testArray[count]; 

     //find lowest score entered 
     for(int count = 1; count < numOfScores; count++) 
     { 
      if (testArray[numOfScores] < lowest) 
       lowest = scores[numOfScores]; 
     } 
    } 

    //average the total amount of scores drop the lowest 
    average = (total - lowest)/numOfScores; 

    cout << "The average test score is: " << average << endl; 
    cout << "Lowest is: " << lowest << endl; 

    return average; 
} 
+0

两条评论:如果可以的话,使用向量而不是动态分配的数组,如果不打算修改它,只需将值传递给'int&numOfScores'即可。 – Bwmat 2012-03-21 18:38:15

+0

你为什么要'double averageScore(int&numOfScores)'而不是'double averageScore(int numOfScores)'? – twain249 2012-03-21 18:38:21

+0

您不需要内部for循环来查找最低值。只需在外循环中将每个'testArray [count]'值与'lowest'进行比较。 – 2012-03-21 18:39:18

回答

1

有很多概率的与您的averageScore函数一起使用,但现在我将介绍最基本的函数。

首先,你应该传递一些数据。现在你正在使用testArray我什至不知道它在哪里分配。我很惊讶你在运行时没有出现分段错误。

但它也没有初始化。在C++中,当你声明一个指针时,它指向的变量就有一个值。它有一个垃圾值,如果你用这个垃圾值执行算术运算,那么你的输出也是垃圾。

你必须让你的averageScore函数可用的分数列表,最好是将它们作为参数传入。

的平均函数的开头如下所示:

double averageScore(int &numOfScores) 
{ 
    double* testArray; 
    ... 

相反,它应该是这样的

double averageScore(double*testArray, int numOfScores) 
{ 
    ... 

当您使用&numOfScores而不是numOfScores,这意味着,如果你改变numOfScores在你的averageScore函数中,它的功能也会改变,你不应该那样做。

现在,在double* testArray;行中,您声明了一个全新的指针,名为“testArray”,并且其中没有任何有意义的数据,尽管它可能充满垃圾。可能还有其他双指针变量,在您的代码中名为“testArray”,但它们都不在您的averageScore函数的范围内。如果您在方法调用中通过testArray,则可以使用它。例如:double someNumber = testArray[i]

请记住,您的数组也被引用传递。如果你宁愿按值传递它,你可以尝试

`double averageScore(double testArray[], int numOfScores)` 

但不要引用我一个

一旦你这样做,你的代码将仍然有一些问题,但输出应该足够有意义,以至于你希望能够自己解决这些问题。

+0

谢谢。我以为我将'scoreScore'作为参数传递给'''numOfScores'参数 – Gmenfan83 2012-03-21 18:57:36

+0

@ Gmenfan83'&numOfScores'只是一个单一整数的参考,它似乎是分数的计数。只让你的功能可用就像问一个人“我有5个号码,你能找到他们的平均值吗?” 我会编辑我的答案,以帮助你沿 – 2012-03-21 19:02:24

+0

比你@Sam我非常花时间向我解释这一点。非常感谢。为了修改需要的东西,我会将其浸润。 – Gmenfan83 2012-03-21 19:28:13

2

我想你想改变这一行:

if(testArray[numOfScores] < lowest) 

这样:

if(testArray[count] < lowest) 

此外,作为@jzworkman指出的,平均的分母应该是(numScores - 1)因为你正在消除分子的最低分数。 (如果适用,您可能希望测试只有一个分数的边缘情况,一旦您消除最低分数,则不会得到平均值。)

2

几个问题。你不应该嵌套这两个for循环(而只是使用if语句检查值是否低于最低值)。

由于这是家庭作业,我会给你的步骤,然后你可以通过修复你的代码

  1. 环路和计算总,同时
  2. 找到最低分数计算平均为(总最低)/(numScores -1)
  3. 返回平均
+0

可以解释为什么我会这样做(总数最低)/(numScores -1)而不是(总数 - 最低)/ numOfScores?谢谢。我只是困惑,想学习。我知道这是家庭作业,但我也想成为一名程序员,所以这很重要。 – Gmenfan83 2012-03-21 19:58:40

+1

因为你正在从你的总数中取出一个值。想象一下,我有以下输入:'1,3,5,'如果我取出最低的(1),那么我的平均值就是'(3 + 5)/ 2',因为我有两个我正在平均的值。你不希望它是'(3 + 5)/ 3',因为在删除最低值后,你只剩下2个值,而不是3 – jzworkman 2012-03-21 20:02:41

+0

我看到了,谢谢你的解释! – Gmenfan83 2012-03-21 20:04:36

3
std::vector<double> scores = {1.2,6.5,3.0,8.3,4.8,6,7.7}; 

// drop lowest score 
scores.erase(min_element(begin(scores),end(scores))); 

double average = accumulate(begin(scores),end(scores),0.0)/scores.size(); 
+1

虽然这可能是正确的,但我的猜测是,这超出了他正在采用的当前课程的范围(注意作业标签),并不是采取这种方法。 – jzworkman 2012-03-21 18:50:27

+0

您应该添加命名空间说明符,但是像这样的答案,更少更多!并赞成std lib – 111111 2012-03-21 19:14:09