c语言:有2个学生,学生数据包括学号、姓名、3门课的成绩,求每门课2个学生的平均成绩。。。

#include<stdio.h> #define N 2 struct student { int num; char name[20]; float score[3]; }stu[N]={{1001,"chen",98,87,67},{1002,"jian",78,90,56}}; void main() {float f(float *p); int j; float t; for(j=0;j<3;j++) { t=f(&stu[0].score[j]); printf("%5.3f\n",t/N); } } float f(float *p) {float temp=0; int i; for(i=0;i<N;i++,p++) temp=temp+*p; printf("%-5.0f ",temp); return(temp); }

我个人认为这个程序还有错误的地方啊:
for(j=0;j<3;j++)
{
t=f(&stu[0].score[j]);
printf("%5.3f\n",t/N);
}
虽然用了调用函数,但是只是求出第一个同学三门课的平均成绩,而不是两个同学每门课的成绩,应改为双循环,如下:
for(i=0;i<3;i++)
for(j=0;j<2;j++)
{
t=f(&stu[j].score[i]); //*将i和j换了,因为每次调用应保证学生换而科目不换
printf("%5.3f\n",t/N);
}
你试试吧,我感觉这样对啊!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-07-20
#include<stdio.h>
#define N 2
struct student
{
int num;
char name[20];
float score[3];
}stu[N]={{1001,"chen",98,87,67},{1002,"jian",78,90,56}};
void main()
{
float f(float *p);
int j;
float t;
for(j=0;j<3;j++)
{
t=f(&stu[0].score[j]);
printf("%5.3f\n",t/N);
}
}
float f(float *p)
{
float temp=0;
int i;
for(i=0;i<N;i++,p++) temp=temp+*p;
printf("%-5.0f ",temp);
return(temp);
}

这个是这样的本回答被网友采纳
第2个回答  2012-07-20
#include<stdio.h>
#define N 2
struct student
{
int num;
char name[20];
float score[3];
}stu[N]={{1001,"chen",98,87,67},{1002,"jian",78,90,56}};
void main()
{
float f(float *p);
int j;
float t;
for(j=0;j<3;j++)
{
t=f(&stu[0].score[j]);
printf("%5.3f\n",t/N);
}
}
float f(float *p)
{
float temp=0;
int i;
for(i=0;i<N;i++,p++) temp=temp+*p;
printf("%-5.0f ",temp);
return(temp);
}
第3个回答  2012-07-20
下面的代码是什么意思?
是有bug 还是要别人给你写呢?
相似回答