用c语言建立一个学生的结构记录,包含学号、姓名和成绩,并让用户输入整数n(n<=10),再输入n个学生的基本信息,计算并输出他们的平均成绩。
时间: 2024-03-01 19:53:37 浏览: 27
好的,我来帮你写一个示例代码。请注意,由于我们是在对话框中进行交互,所以我会将代码分成多段,逐步展示给你。首先是结构体的定义和变量的声明:
```c
#include <stdio.h>
// 定义一个学生的结构体
struct student {
int id;
char name[20];
int score;
};
int main() {
int n, i;
float avg_score = 0.0;
// 让用户输入学生的个数
printf("请输入学生的个数(不超过10个):");
scanf("%d", &n);
// 声明一个学生数组
struct student students[n];
```
接下来是让用户输入每个学生的信息,并计算平均成绩的代码段:
```c
// 让用户输入每个学生的信息
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的学号、姓名和成绩:", i+1);
scanf("%d %s %d", &students[i].id, students[i].name, &students[i].score);
avg_score += students[i].score;
}
// 计算平均成绩
avg_score /= n;
// 输出平均成绩
printf("这%d个学生的平均成绩为:%.2f\n", n, avg_score);
```
最后别忘了结束程序:
```c
return 0;
}
```
完整代码如下:
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)