计算学生平均成绩并统计不及格人数代码C语言
时间: 2023-08-18 11:25:57 浏览: 141
好的,以下是一个示例代码,可以计算学生平均成绩并统计不及格人数:
```c
#include <stdio.h>
int main() {
float scores[] = {75, 60, 85, 90, 55, 70, 65, 80, 95, 40};
int num_students = sizeof(scores) / sizeof(scores[0]);
float total_score = 0;
for (int i = 0; i < num_students; i++) {
total_score += scores[i];
}
float average_score = total_score / num_students;
printf("学生平均成绩为:%.2f\n", average_score);
int num_failed_students = 0;
for (int i = 0; i < num_students; i++) {
if (scores[i] < 60) {
num_failed_students++;
}
}
printf("不及格人数为:%d\n", num_failed_students);
return 0;
}
```
这个代码假设学生分数存储在一个浮点数数组 `scores` 中,然后使用数组长度计算数组中元素个数,并使用 `for` 循环计算总分数。从而得到平均成绩。接着,使用 `for` 循环和条件语句统计不及格人数,并输出结果。输出结果使用了 `printf()` 函数和格式化占位符 `%d` 和 `%.2f`,分别表示整数和带两位小数的浮点数。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.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)
![](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)