在一个数组中查找最高分并将该索引与另一个数组的索引相匹配

时间:2016-05-02 01:07:04

标签: c++ arrays dynamic

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

问题在于: 编写一个获得N分数的程序,找出每个参赛者的分数 1.输入N(表示将给出分数的评委人数)
2.输入参赛者编号
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;
}

1 个答案:

答案 0 :(得分:0)

代码中一个相当严重的问题是:

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数组的大小 当你阅读更多数据时。
  3. 我不知道你的教授需要哪一个,所以我会表明 你选择2,因为它允许你保留基本结构 你的计划。我假设禁止使用std::vector。 这将是真正的简单方法。

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

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

    额外变量的原因是你要分配更多 内存比你需要的多,然后你只需要分配更多 if 。 我们需要一个函数来为我们添加数组值:

    #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;
    }
    

    养成将代码拆分为函数的习惯是一件好事 理念。将代码分解成小块使得包装更容易 解决问题。当您阅读参赛者的ID时,应该是 它自己的功能:

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

    然后,您的while(id >= 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;
    }