用C++语言编写程序:在main函数中建立一个数组存放若干个学生的成绩,并设置变量avgscore(平均分),maxscore(最高分),minscore(最低分)分别用于存放相应的统计数据,将此数组按址传递给sortscore函数,在sortscore函数对这批成绩降序排列,并计算出平均分,最高分,最低分直接修改main函数中这些变量的值但不能直接输出数据,在main函数中输入数据且输出数据。
时间: 2024-02-06 18:09:38 浏览: 227
C语言程序设计-有一个一维数组score,内放10个学生的成绩,用一个函数来求平均成绩;
5星 · 资源好评率100%
以下是代码实现:
```c++
#include <iostream>
using namespace std;
void sortscore(double* scores, int n, double& avgscore, double& maxscore, double& minscore) {
double sum = 0;
maxscore = scores[0];
minscore = scores[0];
for (int i = 0; i < n; i++) {
sum += scores[i];
if (scores[i] > maxscore) {
maxscore = scores[i];
}
else if (scores[i] < minscore) {
minscore = scores[i];
}
}
avgscore = sum / n;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (scores[j] < scores[j + 1]) {
double temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
}
int main() {
const int n = 5;
double scores[n] = {85, 76, 92, 68, 88};
double avgscore, maxscore, minscore;
sortscore(scores, n, avgscore, maxscore, minscore);
cout << "平均分:" << avgscore << endl;
cout << "最高分:" << maxscore << endl;
cout << "最低分:" << minscore << endl;
cout << "成绩降序排列:" << endl;
for (int i = 0; i < n; i++) {
cout << scores[i] << " ";
}
cout << endl;
return 0;
}
```
运行结果:
```
平均分:81.8
最高分:92
最低分:68
成绩降序排列:
92 88 85 76 68
```
阅读全文