我找不到最低的平均值

时间:2017-04-10 03:37:08

标签: c++ arrays

我试图在高尔夫比赛中找到最低的学生平均分。但我无法弄清楚如何。我所有的尝试都找到了最高的平均值,这与我想要显示的相反。

* * * * * * * * *

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;


int main()
{
    double total = 0; double totalr = 0;
    const int round = 3; double less=0;
    const int player = 5;
    string name[player]={"Mike Sims  ", "Paula Hill ", "Steve Jones", "Joll Burton", "Lee Smith  "};
    double average=0, avestudent, averound; string dummy;
    int score[round][player] =
    {  78, 71, 72, 73, 74 ,  76, 78, 75, 74, 79 ,  74, 75, 73, 72, 78  };    

    cout << "Player              Round1           Round2           Round3           Ave./Student"<<endl;

    for (int i = 0; i <player ; i++)
    {
        cout << setw(10) << left << name[i]; total = 0;
        for (int j = 0; j <round ; j++)
        {
            cout << setw(16) << right << score[j][i];
            total = total + score[j][i];    avestudent = total / (int)round;
        }

        cout << fixed << setprecision(1);
        cout <<setw(20)<< avestudent ;
        cout << endl;
    }
    cout << endl;
    cout << setw(10) << left << "Ave./Round ";
    for (int i = 0; i <round; i++)
    {
        totalr = 0;
        for (int j = 0; j <player; j++)
            totalr = totalr + score[i][j];
        averound = totalr / player;
        cout << setw(13)<< "     "<<averound;
    }
    cout<< endl;
    double lowest = score[round][player];

    for (int j = 0; j < round; j++)
    {
        lowest = 0;
        for (int i = 0; i < player; i++)
        {

            avestudent = total / (int)round;
            lowest = avestudent;
            if (score[j][i] < lowest)
                lowest = score[j][i];
        }
    }
    cout << lowest << " was the best recorded student average from the rounds of golf." << endl;
    cout << fixed << setprecision(1) << endl;
}

3 个答案:

答案 0 :(得分:0)

以下是我提出的建议:

int sums[player] = {};
int lowest_score = 0;
int lowest_player = 0;
double lowest_avg = 0.0;
for (int r = 0; r < round; r++)
{
    for (int p = 0; p < player; p++)
    {
        sums[p] += score[r][p];
    }
}

for (int p = 0; p < player; p++)
{
    if ((p == 0) || (sums[p] < lowest_score))
    {
        lowest_player = p;
        lowest_score = sums[p];
    }
}
lowest_avg = ((double)lowest_score) / round;

std::cout << "The player with the lowest average score is " << name[lowest_player] << ".  With an average score of " << lowest_avg << std::endl;

答案 1 :(得分:0)

您的代码存在的最大问题是您尝试将最低值初始化为score[round][player]。你不能这样做,因为这些是数组的大小。 如果有数组int array[5];,您将无法访问array[5]。可达的最高索引是array[4],因为数组使用空格0,1,2,3,4来保存五个整数。

答案 2 :(得分:0)

老实说,我并不确定你的逻辑/工作流程在寻找最低平均值时是什么。在这种情况下,我只想创建一个数组来保存不同的平均值。 (所以将avestudent变量更改为数组)

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    double total = 0; 
    double totalr = 0;
    const int round = 3; 
    double less=0;
    const int player = 5;
    string name[player]={"Mike Sims  ", "Paula Hill ", "Steve Jones", "Joll Burton", "Lee Smith  "};
    double average=0, averound; string dummy;
    double avestudent[player];
    int score[round][player] = {  78, 71, 72, 73, 74 ,  76, 78, 75, 74, 79 ,  74, 75, 73, 72, 78  };

    cout << "Player              Round1           Round2           Round3           Ave./Student"<<endl;

    for (int i = 0; i <player ; i++)
    {
        cout << setw(10) << left << name[i]; 
        total = 0;
        for (int j = 0; j <round ; j++)
        {
            cout << setw(16) << right << score[j][i];
            total = total + score[j][i];
            avestudent[i] = total / (int)round;
        }
        cout << fixed << setprecision(1);
        cout <<setw(20)<< avestudent[i] ;
        cout << endl;
    }
    cout << endl;
    cout << setw(10) << left << "Ave./Round ";

    for (int i = 0; i <round; i++)
    {
        totalr = 0;
        for (int j = 0; j <player; j++)
        {
            totalr = totalr + score[i][j];
        }
        averound = totalr / player;
        cout << setw(13)<< "     "<<averound;
    }
    cout<< endl;
    double lowest = score[round][player];

    for (int j = 0; j < round; j++)
    {
        lowest = avestudent[0];

        for (int i = 1; i < player; i++)
        {
            if (avestudent[i] < lowest) 
            {
                lowest = avestudent[i];
            }

        }
    }
    cout << lowest << " was the best recorded student average from the rounds of golf." << endl;
    cout << fixed << setprecision(1);
    cout << endl;
 }