您的代码中有一个相当严重的问题是在开头:
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 和分数,您似乎首先需要分配一些内存,然后
当您读取更多数据时分配额外的内存,因为您
无法提前知道你有多少参赛者
打算读进去。有两种方法:
- 提前询问参赛人数,和你一样
裁判。这是最简单的方法。
- 增加
contestant和avg_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 >= 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;
}