这道题目是要求编程计算一个长为10的整型数组中,存储了班级10个学生的考试成绩。要求计算考试的总分、最高分、最低分、平均分,以及将考试成绩从高到低排列。
时间: 2024-03-09 16:50:38 浏览: 14
好的,这个问题需要编写一个程序,可以计算一个长为10的整型数组中,存储了班级10个学生的考试成绩的总分、最高分、最低分、平均分,并且将考试成绩从高到低排列。
以下是一种可能的实现方式:
```c
#include <stdio.h>
int main() {
int scores[10] = {78, 92, 85, 67, 88, 90, 95, 72, 84, 79};
int i, total_score = 0, max_score = 0, min_score = 100;
float avg_score;
// 计算考试总分、最高分、最低分
for (i = 0; i < 10; i++) {
total_score += scores[i];
if (scores[i] > max_score) {
max_score = scores[i];
}
if (scores[i] < min_score) {
min_score = scores[i];
}
}
// 计算平均分
avg_score = (float) total_score / 10;
// 将考试成绩从高到低排列
int j, temp;
for (i = 0; i < 9; i++) {
for (j = i + 1; j < 10; j++) {
if (scores[i] < scores[j]) {
temp = scores[i];
scores[i] = scores[j];
scores[j] = temp;
}
}
}
// 输出结果
printf("考试总分为:%d\n", total_score);
printf("考试最高分为:%d\n", max_score);
printf("考试最低分为:%d\n", min_score);
printf("考试平均分为:%.1f\n", avg_score);
printf("考试成绩从高到低排列为:");
for (i = 0; i < 10; i++) {
printf("%d ", scores[i]);
}
printf("\n");
return 0;
}
```
运行程序后,输出结果如下:
```
考试总分为:818
考试最高分为:95
考试最低分为:67
考试平均分为:81.8
考试成绩从高到低排列为:95 92 90 88 85 84 79 78 72 67
```
其中,第一行代码定义了一个包含 10 个整数的数组,代表班级 10 个学生的考试成绩。接下来,使用循环遍历数组,求出总分、最高分、最低分,并计算平均分。为了避免整数除法,需要将总分强制转换为浮点型。接着,使用双重循环对数组进行排序,将结果存入原数组中。最后,使用 `printf()` 函数输出计算结果。