编写程序,将输入的20名学生的成绩保存到数组中,求出其最高分、最低分及平均分C语言代码
时间: 2024-06-06 22:09:04 浏览: 21
#include <stdio.h>
int main() {
int scores[20];
int sum = 0;
int max = 0;
int min = 100;
// 输入成绩
for (int i = 0; i < 20; i++) {
printf("请输入第%d个学生的成绩:", i+1);
scanf("%d", &scores[i]);
// 求和
sum += scores[i];
// 求最高分
if (scores[i] > max) {
max = scores[i];
}
// 求最低分
if (scores[i] < min) {
min = scores[i];
}
}
// 求平均分
float average = (float)sum / 20.0;
// 输出结果
printf("最高分:%d\n", max);
printf("最低分:%d\n", min);
printf("平均分:%f\n", average);
return 0;
}
相关问题
编写程序,将输入的10名学生的成绩保存到数组中,求出其最高分、最低分及平均分C语言代码
可以参考以下代码:
```c
#include <stdio.h>
int main() {
int scores[10]; // 定义包含10个元素的整型数组
int max_score = 0, min_score = 100, sum_score = 0;
// 输入10名学生的成绩
printf("请依次输入10名学生的成绩:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &scores[i]);
if (scores[i] > max_score) max_score = scores[i]; // 更新最高分
if (scores[i] < min_score) min_score = scores[i]; // 更新最低分
sum_score += scores[i]; // 累加成绩
}
// 计算平均分
float avg_score = (float)sum_score / 10;
// 输出结果
printf("最高分为:%d\n", max_score);
printf("最低分为:%d\n", min_score);
printf("平均分为:%.2f\n", avg_score);
return 0;
}
```
这段代码用一个包含10个元素的整型数组来保存学生的成绩。程序会提示用户逐一输入成绩,并在输入过程中同时更新最高分和最低分,以及累加成绩用于求平均分。最后通过除以10求平均分,并输出结果。
c语言编写程序,将输入的20名学生的成绩保存到数组中,求出其最高分、最低分及平均分。 输入格式: 11 12 23 36 48 56 72 78 89 96 78 89 90 100 78 89 56 67 45 98 输出格式: max=100.0 min=11.0 avg=65.6
以下是一个用C语言编写的程序,实现了输入20名学生的成绩并计算最高分、最低分和平均分的功能。代码如下:
```c
#include <stdio.h>
int main() {
float scores[20];
float max_score = 0, min_score = 100, sum_score = 0;
printf("请输入20名学生的成绩:\n");
for (int i = 0; i < 20; i++) {
scanf("%f", &scores[i]);
if (scores[i] > max_score) {
max_score = scores[i];
}
if (scores[i] < min_score) {
min_score = scores[i];
}
sum_score += scores[i];
}
printf("max=%.1f min=%.1f avg=%.1f\n", max_score, min_score, sum_score / 20);
return 0;
}
```
程序中,我们使用了一个长度为20的数组 `scores` 来保存输入的学生成绩。在循环输入学生成绩的过程中,同时计算出最高分、最低分和总分,以便后面输出平均分。最后,根据计算好的最高分、最低分和平均分,输出结果。
注意,程序中使用了浮点数类型 `float` 来保存学生成绩和计算平均分。同时,在输出结果时,我们使用了格式化输出 `%f` 来控制输出的小数位数。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)