我创建了冒泡排序,我想我也创建了平均排序。没有把握。但是我需要将它实现到我的主要功能中,但我不确定如何实现。现在有点卡住了。任何帮助都会很棒。谢谢!
Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores.
Once all the scores are entered, the array should be passed to a function (you create) as a pointer that sorts it in ascending order. This function can be named sortArray and will take two parameters: (double* anArray, int size). The function will return true if the array is sorted, or false otherwise (for example if size <= 0)
Another function should be created that calculates the average score of the items in the array. This function will take in two parameters (double* anyArray, int size) and return the average as a double.
The program should display the sorted array of scores and averages with appropriate headings.
Use pointer notation rather than array notation whenever possible.
#include <iostream>
#include <iomanip>
using namespace std;
bool sortArray(double* anArray, int size);
double averageArray(double* anyArray, int size);
int main()
{
double* anArray;
double total = 0.0;
double average;
int scores;
int count;
cout << "How many test scores are you entering?: ";
cin >> scores;
anArray = new double[scores];
cout << "Enter test scores: ";
for (count = 0; count < scores; count++)
{
cout << "Test Score " << count + 1 << ": ";
cin >> anArray[count];
}
system("pause");
return 0;
}
bool sortArray(double* anArray, int size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (anArray[j] > anArray[j + 1])
{
int temp = anArray[j];
anArray[j] = anArray[j + 1];
anArray[j + 1] = temp;
}
}
}
}
double averageArray(double* anyArray, int size)
{
double count = 0.0;
for (int i = 0; i < size; i++)
{
count += anyArray[i];
}
return (count / size);
}
最佳答案
你在 sortarray 函数中使用了 bool 并且它从不返回任何值所以让它无效并且你在排序数组中使用了 int temp 并且它必须是 double ,我已为您修复了此代码。 顺便说一句,你没有调用函数,而且你忘记在排序后打印数组的值。
#include <iostream>
#include <iomanip>
using namespace std;
bool sortArray(double* anArray, int size);
double averageArray(double* anyArray, int size);
int main()
{
double* anArray;
double total = 0.0;
double average;
int scores;
int count;
cout << "How many test scores are you entering?: ";
cin >> scores;
anArray = new double[scores];
cout << "Enter test scores: ";
for (count = 0; count < scores; count++)
{
cout << "Test Score " << count + 1 << ": ";
cin >> anArray[count];
}
sortArray(anArray, scores);
for (count = 0; count < scores; count++)
{
cout << anArray[count] << "\n";
}
cout << averageArray(anArray, scores);
system("pause");
return 0;
}
bool sortArray(double* anArray, int size)
{
bool sort = false;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (anArray[j] > anArray[j + 1])
{
sort = true;
double temp = anArray[j];
anArray[j] = anArray[j + 1];
anArray[j + 1] = temp;
}
}
}
return sort;
}
double averageArray(double* anyArray, int size)
{
double count = 0.0;
for (int i = 0; i < size; i++)
{
count += anyArray[i];
}
return (count / (double) size);
}
关于C++ 动态分配数组,冒泡排序 + 求平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33426625/