所以程序需要动态分配一个足够大的数组来保存用户=定义的测试分数。输入所有分数后,应将数组传递给按升序对其进行排序的函数。应该调用另一个函数来计算平均分数。程序应显示带有适当标题的分数和平均值的排序列表。尽可能使用指针表示法而不是数组表示法。
我遇到的问题是让程序不接受测试分数的负数。
这是代码。
source.cpp
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void arrSelectSort(float *, int);
void showArrPtr(float *, int);
void showAverage(float, int);
int main()
{
float *scores, //To dynamically allocate an array
total = 0.0, //Accumulator
average; //To hold the averge scores
int numScores; //To hold the number of test scores
//Get the number of test scores.
cout << "How many test scores would you like to process?";
cin >> numScores;
//Dynamically allocate an array large enough to hold that many
//test scores
scores = new float[numScores];
if (scores == NULL)
return 0;
//Get the test score for each test
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Test score #" << (count + 1) << ": ";
cin >> scores[count];
while (scores <= 0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >> scores[count];
}
}
//Calculate the total scores
for (int count = 0; count < numScores; count++)
{
total += scores[count];
}
//sort the elements of the array pointers
arrSelectSort(scores, numScores);
//Will display them in sorted order.
cout << "The test scores in ascending order are: \n";
showArrPtr(scores, numScores);
showAverage(total, numScores);
//Free memory.
delete[] scores;
return 0;
}
void arrSelectSort(float *array, int size)
{
int startScan, minIndex;
float minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (array[index] < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
void showArrPtr(float *array, int size)
{
for (int count = 0; count< size; count++)
cout << array[count] << " ";
cout << endl;
}
void showAverage(float total, int numScores)
{
float average;
//Calculate the average
average = total / numScores;
//Display the results.
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;
system("pause");
}
修改source.cpp以使用double而不是float
#include <iostream>
#include <iomanip>
using namespace std;
void arrSelectSort(double *, int);
void showArrPtr(double *, int);
double showAverage(double, int);
int main()
{
double *scores, //To dynamically allocate an array
total = 0.0, //Accumulator
average; //To hold the averge scores
int numScores; //To hold the number of test scores
//Get the number of test scores.
cout << "How many test scores would you like to process?";
cin >> numScores;
//Dynamically allocate an array large enough to hold that many
//test scores
scores = new double[numScores];
if (scores == NULL)
return 0;
//Get the test score for each test
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Test score #" << (count + 1) << ": ";
cin >> scores[count];
while (scores[count] <= 0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >> scores[count];
}
}
//Calculate the total scores
for (int count = 0; count < numScores; count++)
{
total += scores[count];
}
//sort the elements of the array pointers
arrSelectSort(scores, numScores);
cout << "The test scores in ascending order are: \n";
showArrPtr(scores, numScores);
showAverage(total, numScores);
delete[] scores;
return 0;
}
void arrSelectSort(double *array, int size)
{
int startScan, minIndex;
double minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (array[index] < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
void showArrPtr(double *array, int size)
{
for (int count = 0; count< size; count++)
cout << array[count] << " ";
cout << endl;
}
double showAverage(double total, int numScores)
{
double average;
//Calculate the average
average = total / numScores;
//Display the results.
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;
system("pause");
}
错误 -
严重性代码描述项目文件行抑制状态
错误C4716&#39; showAverage&#39;:必须返回值ConsoleApplication9 c:\ users \ kenny \ desktop \ kenny_fepc1.cpp 85
严重性代码描述项目文件行抑制状态
警告C4101&#39;平均&#39;:未引用的本地变量ConsoleApplication9 c:\ users \ kenny \ desktop \ kenny_fepc1.cpp 12
请帮忙解决。谢谢。
答案 0 :(得分:0)
您没有检查输入的值:
while (scores <= 0)
应该是
while (scores[count] <= 0)
我想这是作业,你被要求使用c风格的数组。但请注意,使用std::vector
会使任务变得非常简单,因为它具有动态大小(您不必处理&#34;手动&#34;)并且对矢量进行排序是微不足道的。
顺便说一下,我建议你把循环改为
for (int count = 0; count < numScores; count++) {
scores[count] = -1;
while (scores[count] <= 0) {
cout << "Test Score #" << (count + 1) << "(has to be >0) : ";
cin >> scores[count];
}
}
当您想要对代码进行更改时,重复代码总是很麻烦。尽量避免使用它(即使它只有两个小行)。
出现您的其他错误,因为您宣布showAverage
返回double
但没有return
。声明它不返回任何内容:
void showAverage(double total, int numScores)
或在函数末尾添加return
语句:
return average;