【问题标题】:Finding highest score in one array and matching that index to another array's index在一个数组中找到最高分并将该索引与另一个数组的索引匹配
【发布时间】:2016-05-02 01:07:04
【问题描述】:

想知道是否有人可以查看我的程序。我需要帮助找出如何填充动态数组,其大小是一个在满足条件时递增的计数器,以及如何找到该数组中的最高双精度数。谢谢大家!

问题来了: 编写一个程序,接受 N 个分数并找到每个参赛者的分数
1. 输入 N(代表将给予评分的评委人数)
2.输入参赛者ID
3. 从这些分数中找到平均值(找到最大值和最小值,从总分中减去它们除以 N-2)
程序应该这样做,直到输入的参赛者 ID 无效(负数)。
这是我迄今为止所拥有的。

#include<iostream>
using namespace std;
double average(int N, int scores[])
int main()
{
    int N, id, contestants = 0, return_scores = 0;
    int scores[10];
    int *contestant = new  int[contestants];
    double *avg_scores = new  double[contestants];
    double avg;
    int highest, index;

    cout << "Number of Judges: " << endl;
    cin >> N;
    cout << "Enter ID: " << endl;
    cin >> id;
    if (id >= 0)
    {
        contestants++;
    }
    while (id >=0)
    {
        //populate the contestants array with id if id is valid
        for (int i = 0;i < contestants;i++)
        {
            contestant[i] = id;
        }


        cout << "Enter scores:" << endl;
        for (int i = 0;i < N;i++)
        {
            cin >> scores[i];
        }

        //call function to calculate average
        avg = average(N, scores);
        for (int i = 0;i < contestants;i++)
        {
            avg_scores[i] = avg;
        }

        highest = int(avg_scores[0]);

        cout << "Contestant: " << id << " " << avg << endl;
        cout << "\n";
        cout << "Enter contestant ID: " << endl;
        cin >> id;
    }
    //find the index to the highest score and match it with the contestants array
    for (int i = 0;i < contestants;i++)
    {
        if (highest < avg_scores[i])
        {
            highest = int(avg_scores[i]);
            index = highest;
        }
    }
    cout << "Contestant: " << contestant[index] << " had the highest score.\n";
    return 0;
}

double average(int N, int scores[10])
{
    double total = 0;
    double min = scores[0], max = scores[0];
    double average = 0.0;
    double drop = min + max;
    //find the total/min/max
    for (int i = 0;i < N;i++)
    {
        total += scores[i];
        if (scores[i] < min)
        {
            min = scores[i];
        }

        if (scores[i] > max)
        {
            max = scores[i];
        }
    }
    //average
    average = ((total - min - max) / (N-2));
    return average;
}

【问题讨论】:

  • 你所拥有的“到目前为止”的缩进很差,而且有些不可读。如果您想在您的程序上获得一些帮助,您至少可以正确缩进它,以便其他人可以轻松阅读。
  • 您好,欢迎来到 SO!请阅读this 帖子了解如何提出更好的问题,这将帮助人们为您提供更好的答案。正如 Sam 所提到的,适当的缩进对于提高可读性非常有帮助,而且对于自己的代码来说也是一个非常好的习惯!

标签: c++ arrays dynamic


【解决方案1】:

您的代码中有一个相当严重的问题是在开头:

int N, id, contestants = 0, return_scores = 0;
int scores[10];
int *contestant = new  int[contestants];
double *avg_scores = new  double[contestants];

当你分配你的数组时,上面的代码分配了contestants contestant 数组的整数,contestants 双倍 avg_scores。不幸的是,contestants 为零,这意味着你是 根本不分配内存。更糟糕的是,您永远不会重新分配 这些数组中的任何一个,因此您的程序具有未定义的行为,因为 您将内容存储在零长度数组中。

你还有其他问题,比如:

    for (int i = 0;i < contestants;i++)
    {
        contestant[i] = id;
    }

...这总是会清除您之前存储的每个参赛者 ID。你对平均分数做同样的事情。

另外,您允许用户在N 变量中指定评委的数量,然后将N 分数读入scores 数组。但是由于某种原因,您将 scores 声明为始终具有 10 个元素的固定长度数组。这意味着如果我输入 11 作为评委人数,您将写到数组的末尾。您应该动态分配scores 数组,使其包含N 元素。

对于您的参赛者 ID 和分数,您似乎首先需要分配一些内存,然后 当您读取更多数据时分配额外的内存,因为您 无法提前知道你有多少参赛者 打算读进去。有两种方法:

  1. 提前询问参赛人数,和你一样 裁判。这是最简单的方法。
  2. 增加contestantavg_scores数组的大小 随着您阅读更多数据。

我不知道你的教授需要哪一个,所以我会展示 您选择 2,因为它允许您保留 你的程序。我假设禁止使用std::vector。 那将是真正的简单的方法。

每个数组都需要三个变量:

int *contestant;
size_t contestant_capacity=0;
size_t contestants=0;

额外变量的原因是你要分配更多 内存超出您的需要,然后您将只分配更多如果您需要。 我们需要一个函数来为我们添加值到数组中:

#include <cstring>

int *add_contestant_id(int *array, size_t *capacity, size_t *size, int id)
{
    if(*size == *capacity) {
            *capacity *= 2;
            int *new_array = new int[*capacity];
            memcpy(new_array, array, (*size)*sizeof(int));
            delete[] array;
            array = new_array;
    }
    array[(*size)++] = id;
    return array;
}

将它放在一个单独的函数中可以避免所有这些簿记 main 函数,使主循环更简单,因此 更容易理解。

你需要另一个和它一样的函数,除了浮点数来添加 avg_scores 数组的新分数。我通常会使用模板来 避免两次编写相同的函数但使用不同的类型,但您可能不被允许这样做。但如果您被允许,代码将如下所示:

template <typename T>
int *add_value(T *array, size_t *capacity, size_t *size, T value)
{
    if(*size == *capacity) {
            *capacity *= 2;
            T *new_array = new T[*capacity];
            memcpy(new_array, array, (*size)*sizeof(T));
            delete[] array;
            array = new_array;
    }
    array[(*size)++] = value;
    return array;
}

养成将代码拆分为函数的习惯是一件好事 主意。将代码分成小块可以更容易地包装你的 解决问题。当您阅读参赛者的身份证时,应该是 自己的功能:

int read_contestant_id()
{
    int id;
    cout << "Enter ID: " << endl;
    cin >> id;
    return id;
}

然后,您的while(id &gt;= 0) 循环可以重写为:

for(int id=read_contestant_id(); id >= 0; id=read_contestant_id()) {
    ...
}

阅读单个玩家的平均分数也是一个很好的候选者 单独的函数(也是动态分配scores 数组的好地方):

double read_avg_score(int nJudges)
{
    int *scores = new int[nJudges];
    for(int i=0; i<nJudges; i++) {
            cout << "Enter scores:" << endl;
            cin >> scores[i];
    }

    double return_val = average(nJudges, scores);
    delete[] scores;
    return return_val;
}

重写读取循环以使用这些函数后,代码 看起来像我下面的。请注意整个 main 函数现在如何立即显示在屏幕上。

修复了所有严重的错误,只剩下唯一的错误 修复是找出最高分和 它在数组中的索引,现在应该更容易了,因为程序的其余部分实际上做了你之前认为它在做的事情:

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

double average(int N, int *scores);
double read_avg_score(int nJudges);
int read_contestant_id();
double *add_average(double *array, size_t *capacity, size_t *size, double avg);
int *add_contestant_id(int *array, size_t *capacity, size_t *size, int id);

int main()
{
    int id;
    size_t N=0, contestants = 0, contestants_capacity=1; 
    size_t avg_scores_capacity = 1, avg_scores_size=0;
    int *contestant = new  int[contestants_capacity];
    double *avg_scores = new  double[avg_scores_capacity];
    double avg;
    int highest, index;

    while(N == 0) {
        cout << "Number of Judges: " << endl;
        cin >> N;
        if(N == 0) {
             cout << "You can't have ZERO judges." << endl;
        }
    }
    for(int id=read_contestant_id(); id >= 0; id=read_contestant_id())
    {
        // populate the contestants array with id, which is ensured to be
        // valid by the for-loop invariant.
        contestant = add_contestant_id(contestant, &contestants_capacity, &contestants, id);

        // Read the scores, calculate the average, and populate the avg_scores array.

        avg_scores = add_average(avg_scores, &avg_scores_capacity, &avg_scores_size,
                                 read_avg_score(N));
    }
    //find the index to the highest score and match it with the contestants array
    for (int i = 0; i < contestants; i++)
    {
        if (highest < avg_scores[i])
        {
            highest = int(avg_scores[i]); // <-- Maybe highest should be a double.
            index = highest; // <-- Here's where you made the mistake.
        }
    }
    cout << "Contestant: " << contestant[index] << " had the highest score.\n";
    return 0;
}

double average(int N, int *scores)
{
    double total = 0;
    double min = scores[0], max = scores[0];
    double average = 0.0;
    double drop = min + max;
    //find the total/min/max
    for (int i = 0;i < N;i++)
    {
        total += scores[i];
        if (scores[i] < min)
        {
            min = scores[i];
        }

        if (scores[i] > max)
        {
            max = scores[i];
        }
    }
    //average
    average = ((total - min - max) / (N-2));
    return average;
}


double read_avg_score(int nJudges)
{
        int *scores = new int[nJudges];
        for(int i=0; i<nJudges; i++) {
                cout << "Enter scores:" << endl;
                cin >> scores[i];
        }

        double return_val = average(nJudges, scores);
        delete[] scores;
        return return_val;
}

int read_contestant_id()
{
    int id;
    cout << "Enter ID: " << endl;
    cin >> id;
    return id;
}


int *add_contestant_id(int *array, size_t *capacity, size_t *size, int id)
{
        if(*size == *capacity) {
                *capacity *= 2;
                int *new_array = new int[*capacity];
                memcpy(new_array, array, (*size)*sizeof(int));
                delete[] array;
                array = new_array;
        }
        array[(*size)++] = id;
        return array;
}

double *add_average(double *array, size_t *capacity, size_t *size, double avg)
{
        if(*size == *capacity) {
                *capacity *= 2;
                double *new_array = new double[*capacity];
                memcpy(new_array, array, (*size)*sizeof(double));
                delete[] array;
                array = new_array;
        }
        array[(*size)++] = avg;
        return array;
}

【讨论】:

    猜你喜欢
    • 2016-02-14
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多