用c语言编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据记录,每个记录包括num,name,score[3],用主函数输入这些记录,用print函数输出这些记录
时间: 2023-02-21 16:13:04 浏览: 85
#include<stdio.h>
struct student
{
int num;
char name[20];
int score[3];
};
void print(struct student stu[], int n)
{
int i;
printf("学号\t姓名\t语文\t数学\t外语\n");
for (i = 0; i < n; i++)
{
printf("%d\t%s\t%d\t%d\t%d\n", stu[i].num, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2]);
}
}
int main()
{
struct student stu[5];
int i;
for (i = 0; i < 5; i++)
{
printf("输入第%d个学生的学号、姓名和三科成绩:\n", i + 1);
scanf("%d %s %d %d %d", &stu[i].num, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
}
print(stu, 5);
return 0;
}
相关问题
C语言编写一个函数 print,打印一个学生的成绩数组,该数组中有 5个学生的数据记录, 每个记录包括num,name,score[3],用主函数输入这些记录,用print 函数输出这些记录
以下是C语言编写的函数print,可以打印一个学生的成绩数组,该数组中有5个学生的数据记录,每个记录包括num,name,score。主函数可以输入这些记录,用print函数输出这些记录。
```c
#include <stdio.h>
struct student {
int num;
char name[20];
int score[3];
}stu[5];
void print(struct student stu[]) {
int i, j;
printf("num\tname\tscore1\tscore2\tscore3\n");
for (i = 0; i < 5; i++) {
printf("%d\t%s", stu[i].num, stu[i].name);
for (j = 0; j < 3; j++) {
printf("\t%d", stu[i].score[j]);
}
printf("\n");
}
}
int main() {
int i, j;
for (i = 0; i < 5; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
printf("num: ");
scanf("%d", &stu[i].num);
printf("name: ");
scanf("%s", stu[i].name);
for (j = 0; j < 3; j++) {
printf("score%d: ", j + 1);
scanf("%d", &stu[i].score[j]);
}
}
print(stu);
return 0;
}
```
用c语言编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据记录,每个记录包括 num,name,score[3],用主函数输入这些 记录,Hprim函数输出这些记录。
好的,这是一个简单的实现:
```c
#include <stdio.h>
struct student {
int num;
char name[20];
int score[3];
};
void print(struct student stu[], int n) {
printf("num\tname\tscore1\tscore2\tscore3\n");
for (int i = 0; i < n; i++) {
printf("%d\t%s\t%d\t%d\t%d\n", stu[i].num, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2]);
}
}
int main() {
struct student stu[5];
for (int i = 0; i < 5; i++) {
printf("请输入第%d个学生的num, name, score1, score2, score3: ", i + 1);
scanf("%d%s%d%d%d", &stu[i].num, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
}
print(stu, 5);
return 0;
}
```
这个程序从主函数中输入5个学生的记录,保存在一个结构体数组中,然后调用print函数打印这些记录。print函数接受一个结构体数组和数组的大小作为参数,遍历数组,按照格式打印每个学生的记录。注意,我们使用了结构体来保存每个学生的记录,这样可以方便地管理每个学生的信息。
阅读全文
相关推荐














