【发布时间】:2021-04-10 15:17:42
【问题描述】:
目的是对用户输入的分数进行排序、显示和平均。 我还没有弄清楚。 如果你已经解决了这个问题,请帮忙。 到目前为止,我已经尝试过这段代码,但它不起作用。
#include<iostream>
using namespace std;
void sortArr(int [], int);
void getAvg(const int [], int);
int main()
{
int *scorePtr = nullptr;
int size;
cout << "Enter the number of test scores: ";
cin >> size;
while(size < 0)
{
cout << "Invalid entry. Enter the number of scores: ";
cin >> size;
}
if(size == 0)
{
cout << "No scores. Terminating program...";
return 0;
}
scorePtr = new int[size];
cout << "Enter the scores earned on each test: ";
for (int index = 0; index < size; index++)
{
cin >> scorePtr[index];
while(scorePtr[index] < 0)
{
cout << "Entry not valid. Enter score #" << (index + 1) << ": ";
cin >> scorePtr[index];
}
}
sortArr(scorePtr, size);
getAvg(scorePtr, size);
return 0;
}
void sortArr(int *arr[], int SIZE)
{
int startScan, minIndex;
int *minElem;
for(startScan = 0; startScan < (SIZE - 1); startScan++)
{
minIndex = startScan;
minElem = arr[startScan];
for(int index = startScan + 1; index < SIZE; index++)
{
if(*(arr[index]) < *minElem)
{
minElem = arr[index];
minIndex = index;
}
}
arr[minIndex] = arr[startScan];
arr[startScan] = minElem;
}
cout << "Here are the scores you entered, sorted in ascending order: " << endl;
for(int count = 0; count < SIZE; count++)
cout << *(arr[count]) << " ";
cout << endl;
}
void getAvg(const int arr[], int Size)
{
double total = 0.0;
double avg = 0.0;
for (int index = 0; index < Size; index++)
{
total += arr[index];
}
avg = (total/Size);
cout << "Your average score is: " << avg << endl;
}
我从这段代码中得到的只是编译器错误。 有没有人有什么建议?请和谢谢。
【问题讨论】:
-
什么是编译器错误?
-
我试着四处搜索,但找不到我需要的东西。
-
您需要提取 minimal reproducible example 并将其与错误消息一起包含在您的问题中。作为这里的新用户,也可以使用tour 并阅读How to Ask。