有5个学生,每个学生的数据包括学号、姓名、分数。计算学生的平均成绩和不及格的人数。c语言
时间: 2023-12-15 11:09:40 浏览: 41
好的,以下是 C 语言的代码示例,实现相同的功能:
```c
#include <stdio.h>
struct student {
char id[10];
char name[20];
int score;
};
int main() {
struct student students[5] = {
{"001", "张三", 80},
{"002", "李四", 60},
{"003", "王五", 70},
{"004", "赵六", 45},
{"005", "钱七", 90}
};
int total_score = 0; // 总分
int fail_count = 0; // 不及格人数
int i;
for (i = 0; i < 5; i++) {
total_score += students[i].score; // 累加总分
if (students[i].score < 60) {
fail_count++; // 不及格人数加1
}
}
float avg_score = (float) total_score / 5; // 计算平均分
// 输出结果
printf("平均分:%.2f\n", avg_score);
printf("不及格人数:%d\n", fail_count);
return 0;
}
```
运行上述代码,输出结果为:
```
平均分:69.00
不及格人数:2
```
需要注意的是,C 语言中需要使用结构体来存储学生信息。同时,除法运算得到的结果为整数,需要强制转换为浮点数才能得到正确的结果。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![wps](https://img-home.csdnimg.cn/images/20210720083653.png)