#include "stdio.h" struct STUDENT { long int num; char name[20]; float chinese; float maths; float english; }; float average(struct STUDENT a[], int x)//当x=1,2,3时分别计算语文、数学、英语的平均成绩并返回。 { //*****************在此添加代码********************* return 0; //此语句根据情况修改!!! //************************************************** } void outputstu(struct STUDENT a[])//此函数用于将数组中的结构体元素输出 { //*****************在此添加代码********************* //************************************************** } void main() { struct STUDENT a[8]={7340001,"丁一",65,72,90,7340002,"刘二",62,46,87,7340003,"张三",56,90,77,7340004,"李四",76,67,65,7340005,"王五",44,88,66,7340006,"陈六",98,78,84,7340007,"赵七",39,40,60,7340008,"郑八",99,72,70}; float chineseavg=0,mathsavg=0,englishavg=0; chineseavg=average(a,1); mathsavg=average(a,2); englishavg=average(a,3); outputstu(a); printf("\n语文平均成绩%6.2f\n数学平均成绩%6.2f\n英语平均成绩%6.2f\n",chineseavg,mathsavg,englishavg); }
时间: 2023-07-18 12:50:43 浏览: 26
这是一段 C 语言代码,定义了一个名为 STUDENT 的结构体,包含学生的学号、姓名、语文、数学、英语成绩。代码中还有三个函数,分别为 average、outputstu 和 main。
average 函数用于计算传入的 STUDENT 数组中的语文、数学或英语成绩的平均值,并将平均值返回。该函数的参数为一个 STUDENT 数组和一个整型变量 x,当 x 为 1、2、3 时分别计算语文、数学、英语的平均成绩。
outputstu 函数用于将传入的 STUDENT 数组中的所有元素依次输出。
main 函数中定义了一个包含 8 个 STUDENT 结构体元素的数组 a,调用了 average 函数计算语文、数学、英语的平均成绩,并调用 outputstu 函数输出数组中的所有元素。最后使用 printf 函数输出计算得到的语文、数学、英语的平均成绩。
相关问题
#include <stdio.h> struct student { int num; char name[20]; char gender; int age; float score; }; int main() { struct student s; s.num = 1001; scanf("%s %c %d %f", s.name, &s.gender, &s.age, &s.score); printf("学号:%d\n姓名:%s\n性别:%c\n年龄:%d\n成绩:%.2f\n", s.num, s.name, s.gender, s.age, s.score); return 0; }找错
此代码的问题在于缺少了对结构体类型的定义。需要在代码开头加上以下代码:
```c
struct student {
int num;
char name[20];
char gender;
int age;
float score;
};
```
完整的代码如下:
```c
#include <stdio.h>
struct student {
int num;
char name[20];
char gender;
int age;
float score;
};
int main() {
struct student s;
s.num = 1001;
scanf("%s %c %d %f", s.name, &s.gender, &s.age, &s.score);
printf("学号:%d\n姓名:%s\n性别:%c\n年龄:%d\n成绩:%.2f\n", s.num, s.name, s.gender, s.age, s.score);
return 0;
}
```
运行程序时,输入相应的信息即可输出学生信息。
补充以下代码,要求补充后的代码能用结构体表示8名学生的学号、姓名、语文、数学、英语三门课的成绩,存放在一维数组中(数组元素为结构体),计算每门课的平均成绩,并显示。 #include "stdio.h" struct STUDENT { long int num; char name[20]; float chinese; float maths; float english; }; float average(struct STUDENT a[], int x) { } void outputstu(struct STUDENT a[]) { } void main() { struct STUDENT a[8]={7340001,"丁一",65,72,90,7340002,"刘二",62,46,87,7340003,"张三",56,90,77,7340004,"李四",76,67,65,7340005,"王五",44,88,66,7340006,"陈六",98,78,84,7340007,"赵七",39,40,60,7340008,"郑八",99,72,70}; float chineseavg=0,mathsavg=0,englishavg=0; chineseavg=average(a,1); mathsavg=average(a,2); englishavg=average(a,3); outputstu(a); printf("\n语文平均成绩%6.2f\n数学平均成绩%6.2f\n英语平均成绩%6.2f\n",chineseavg,mathsavg,englishavg); }
#include "stdio.h"
struct STUDENT {
long int num;
char name[20];
float chinese;
float maths;
float english;
};
float average(struct STUDENT a[], int x) {
float sum = 0;
for (int i = 0; i < 8; i++) {
if (x == 1) {
sum += a[i].chinese;
} else if (x == 2) {
sum += a[i].maths;
} else if (x == 3) {
sum += a[i].english;
}
}
return sum / 8;
}
void outputstu(struct STUDENT a[]) {
printf("学号\t\t姓名\t\t语文\t\t数学\t\t英语\n");
for (int i = 0; i < 8; i++) {
printf("%ld\t%s\t\t%.2f\t\t%.2f\t\t%.2f\n", a[i].num, a[i].name, a[i].chinese, a[i].maths, a[i].english);
}
}
void main() {
struct STUDENT a[8] = {
{7340001, "丁一", 65, 72, 90},
{7340002, "刘二", 62, 46, 87},
{7340003, "张三", 56, 90, 77},
{7340004, "李四", 76, 67, 65},
{7340005, "王五", 44, 88, 66},
{7340006, "陈六", 98, 78, 84},
{7340007, "赵七", 39, 40, 60},
{7340008, "郑八", 99, 72, 70},
};
float chineseavg = 0, mathsavg = 0, englishavg = 0;
chineseavg = average(a, 1);
mathsavg = average(a, 2);
englishavg = average(a, 3);
outputstu(a);
printf("\n语文平均成绩%6.2f\n数学平均成绩%6.2f\n英语平均成绩%6.2f\n", chineseavg, mathsavg, englishavg);
}
阅读全文