在main函数中建立一个数组存放若干个学生的成绩,并设置变量avgscore(平均分),maxscore(最高分),minscore(最低分)分别用于存放相应的统计数据,将此数组按址传递给sortscore函数,在sortscore函数对这批成绩降序排列,并计算出平均分,最高分,最低分直接修改main函数中这些变量的值但不能直接输出数据,在main函数中输入数据且输出数据。
时间: 2024-02-06 12:10:44 浏览: 120
下面是代码实现:
```C++
#include <iostream>
void sortscore(int* scores, int n, int& avgscore, int& maxscore, int& minscore) {
// 计算平均分
int sum = 0;
for (int i = 0; i < n; i++) {
sum += scores[i];
}
avgscore = sum / n;
// 计算最高分和最低分
maxscore = scores[0];
minscore = scores[0];
for (int i = 1; i < n; i++) {
if (scores[i] > maxscore) {
maxscore = scores[i];
}
if (scores[i] < minscore) {
minscore = scores[i];
}
}
// 对成绩降序排列
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (scores[i] < scores[j]) {
int temp = scores[i];
scores[i] = scores[j];
scores[j] = temp;
}
}
}
}
int main() {
const int n = 5;
int scores[n] = { 80, 90, 70, 85, 95 };
int avgscore, maxscore, minscore;
sortscore(scores, n, avgscore, maxscore, minscore);
std::cout << "平均分:" << avgscore << std::endl;
std::cout << "最高分:" << maxscore << std::endl;
std::cout << "最低分:" << minscore << std::endl;
std::cout << "成绩降序排列:" << std::endl;
for (int i = 0; i < n; i++) {
std::cout << scores[i] << " ";
}
std::cout << std::endl;
return 0;
}
```
输出结果:
```
平均分:84
最高分:95
最低分:70
成绩降序排列:
95 90 85 80 70
```
阅读全文