查错void average(STUDENT * stu_inf,int n,float * ave_course) { int i,j; int s; for(i=0;i<3;i++) { s=0;//每次算一门课的均分都要重置为0 for(j=0;j<10;j++) {s+=stu_inf->score[i]; stu_inf++;} *ave_course=s/3.0; ave_course++; } }
时间: 2024-03-16 22:45:25 浏览: 19
这段代码存在以下问题:
1. 函数声明中的 `STUDENT` 类型需要定义,否则编译器无法识别。
2. 函数中的 `stu_inf->score[i]` 应该改为 `stu_inf[j].score[i]`,因为 `stu_inf` 是一个指向结构体数组的指针,需要通过下标 `j` 访问数组中的元素。
3. 计算平均分的代码应该在内层循环结束后再执行,因为在内层循环中计算的是每个学生的分数总和,需要在外层循环中将每门课程的分数总和累加后再除以学生数量才能得到平均分。
4. 函数中的 `ave_course++` 应该在计算完平均分后再执行,因为它指向下一个课程的平均分存储位置。
修改后的代码如下所示:
```c
typedef struct {
int score[3];
} STUDENT;
void average(STUDENT *stu_inf, int n, float *ave_course) {
int i, j;
int s;
for (i = 0; i < 3; i++) {
s = 0;
for (j = 0; j < n; j++) {
s += stu_inf[j].score[i];
}
ave_course[i] = s / (float)n;
}
}
```
相关问题
1.有10个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入10个学生数据,要求打印出求3门课每门课程的平均分, 以及最高分的学生的数据(包括学号、姓名、3门课的成绩、平均分数)。 该题要求使用结构体,设计如下: typedef struct student { long studentID[20]; char studentName[20]; float score[3]; }STUDENT; (1)使用 void input(STUDENT *stu_inf, int n);函数实现输入n个学生的结构体数组,参数n是数组中学生信息的人数; (2)使用 void average (STUDENT * stu_inf, int n, int * ave_course);函数求3门课每门课程的平均分; (3)使用STUDENT max(STUDENT*stu_inf, int n);用max函数找出最高分的学生数据; (4)总平均分和最高分学生的数据都在主函数中输出。
好的,这是一个关于结构体的问题,需要实现三个功能,分别是输入n个学生的结构体数组、求3门课每门课程的平均分、用max函数找出最高分的学生数据。下面是一种可能的解决方案:
```c
#include <stdio.h>
#include <string.h>
typedef struct student {
long studentID[20];
char studentName[20];
float score[3];
} STUDENT;
void input(STUDENT *stu_inf, int n) {
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的信息:\n", i+1);
printf("学号:");
scanf("%ld", &(stu_inf+i)->studentID);
printf("姓名:");
scanf("%s", (stu_inf+i)->studentName);
printf("成绩:");
for (int j = 0; j < 3; j++) {
scanf("%f", &(stu_inf+i)->score[j]);
}
}
}
void average(STUDENT *stu_inf, int n, float *ave_course) {
for (int i = 0; i < 3; i++) {
float sum = 0;
for (int j = 0; j < n; j++) {
sum += (stu_inf+j)->score[i];
}
ave_course[i] = sum / n;
}
}
STUDENT max(STUDENT *stu_inf, int n) {
STUDENT max_stu;
float max_score = 0;
for (int i = 0; i < n; i++) {
float sum = 0;
for (int j = 0; j < 3; j++) {
sum += (stu_inf+i)->score[j];
}
float ave_score = sum / 3;
if (ave_score > max_score) {
max_score = ave_score;
max_stu = *(stu_inf+i);
}
}
return max_stu;
}
int main() {
STUDENT stu_inf[10];
float ave_course[3];
input(stu_inf, 10);
average(stu_inf, 10, ave_course);
printf("3门课每门课程的平均分为:\n");
for (int i = 0; i < 3; i++) {
printf("课程%d:%.2f\n", i+1, ave_course[i]);
}
STUDENT max_stu = max(stu_inf, 10);
printf("最高分学生的信息为:\n");
printf("学号: %ld\n", max_stu.studentID);
printf("姓名: %s\n", max_stu.studentName);
printf("成绩: %.2f %.2f %.2f\n", max_stu.score[0], max_stu.score[1], max_stu.score[2]);
float max_score = (max_stu.score[0] + max_stu.score[1] + max_stu.score[2]) / 3;
printf("平均分: %.2f\n", max_score);
return 0;
}
```
运行示例:
```
请输入第1个学生的信息:
学号:1001
姓名:张三
成绩:90 85 95
请输入第2个学生的信息:
学号:1002
姓名:李四
成绩:80 75 70
请输入第3个学生的信息:
学号:1003
姓名:王五
成绩:60 65 70
请输入第4个学生的信息:
学号:1004
姓名:赵六
成绩:88 92 90
请输入第5个学生的信息:
学号:1005
姓名:钱七
成绩:75 80 85
请输入第6个学生的信息:
学号:1006
姓名:孙八
成绩:60 70 80
请输入第7个学生的信息:
学号:1007
姓名:周九
成绩:85 90 95
请输入第8个学生的信息:
学号:1008
姓名:吴十
成绩:80 85 90
请输入第9个学生的信息:
学号:1009
姓名:郑一
成绩:70 75 80
请输入第10个学生的信息:
学号:1010
姓名:王二
成绩:90 95 100
3门课每门课程的平均分为:
课程1:78.80
课程2:81.20
课程3:85.50
最高分学生的信息为:
学号: 1010
姓名: 王二
成绩: 90.00 95.00 100.00
平均分: 95.00
```
C语言查错。#include <stdio.h> #include <stdlib.h> #define N 10 typedef struct student { long studentID; char studentName[20]; float score[3]; }STUDENT; void input (STUDENT * stu_inf,int n) { int j; for(j=0;j<n;j++) { scanf("%ld",stu_inf->studentID); scanf("%s",stu_inf->studentName); for(int k=0;k<3;k++) scanf("%f",stu_inf->score[k]); stu_inf++; } } void average(STUDENT * stu_inf,int n,int * ave_course) { int i,j,s; for(i=0;i<N;i++) { s=0; for(j=0;j<3;j++) s+=stu_inf->score[j]; *ave_course=s/3; ave_course++; } } STUDENT max(STUDENT * stu_inf,int n) { STUDENT *top; int i,j,k; float sumscore[10]={0}; for(i=0;i<N;i++) { for(j=0;j<3;j++) sumscore[i]+=stu_inf->score[j]; } for(i=0,k=0;i<9;i++) { if(sumscore[i]>sumscore[k]) k=i; } for(j=0;j<10;j++) { if(j==k) top=stu_inf; break; } return *top; } int main() { int i; float av[10]; STUDENT *inf; STUDENT *ave; ave=av; for(i=0;i<N;i++) {input(inf,N);} average(inf,N,ave); STUDENT m=max(inf,N); printf("总平均分是%f\n最高分学生数据如下:\n",*ave); printf("%ld %s ",m.studentID,m.studentName); int su=0; for(i=0;i<3;i++) { printf("%f ",m.score[i]); su+=m.score[i]; } printf("%f",su/3); return 0; }
这段代码有几个错误:
1.在input函数中,scanf函数的参数应该是&stu_inf->studentID和stu_inf->studentName,因为它们是指向long和char数组的指针,而不是实际的变量。
2.在average函数中,变量i应该从0开始,而不是从N开始。另外,在计算平均分时应该使用float类型的变量,而不是int类型的指针。
3.在max函数中,变量i应该从0开始,而不是从N开始。在查找最高分时,变量k应该初始化为0,而不是为i。
4.在max函数中,返回的是指向STUDENT结构体的指针,而不是STUDENT结构体本身。因此,在main函数中,应该使用*号来解引用指针。
5.在main函数中,变量inf没有分配内存空间,因此无法使用。应该使用malloc函数来分配内存空间,并将返回的指针赋值给inf。另外,变量ave应该是float类型的数组,而不是指针。
以下是修改后的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define N 10
typedef struct student {
long studentID;
char studentName[20];
float score[3];
} STUDENT;
void input(STUDENT* stu_inf, int n) {
int j;
for (j = 0; j < n; j++) {
scanf("%ld", &stu_inf->studentID);
scanf("%s", stu_inf->studentName);
for (int k = 0; k < 3; k++)
scanf("%f", &stu_inf->score[k]);
stu_inf++;
}
}
void average(STUDENT* stu_inf, int n, float* ave_course) {
int i, j;
for (i = 0; i < N; i++) {
float s = 0;
for (j = 0; j < 3; j++)
s += stu_inf->score[j];
*ave_course = s / 3;
ave_course++;
stu_inf++;
}
}
STUDENT* max(STUDENT* stu_inf, int n) {
STUDENT* top;
int i, j, k = 0;
float sumscore[10] = { 0 };
for (i = 0; i < N; i++) {
for (j = 0; j < 3; j++)
sumscore[i] += stu_inf->score[j];
stu_inf++;
}
for (i = 0; i < 9; i++) {
if (sumscore[i] > sumscore[k])
k = i;
}
stu_inf -= N;
for (j = 0; j < 10; j++) {
if (j == k) {
top = stu_inf;
break;
}
stu_inf++;
}
return top;
}
int main() {
int i;
float av[10];
STUDENT* inf = (STUDENT*)malloc(N * sizeof(STUDENT));
float* ave = av;
for (i = 0; i < N; i++) {
input(inf, N);
}
average(inf, N, ave);
STUDENT* m = max(inf, N);
printf("总平均分是%f\n最高分学生数据如下:\n", *ave);
printf("%ld %s ", m->studentID, m->studentName);
int su = 0;
for (i = 0; i < 3; i++) {
printf("%f ", m->score[i]);
su += m->score[i];
}
printf("%f", su / 3.0);
free(inf);
return 0;
}
```
相关推荐
![](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)