C语言编程>第十一周 ⑥ 某学生的记录由学号、5门课程成绩和平均分组成,学号和5门课程的成绩已在主函数中给出。请编写函数fun,它的功能是:求出该学生的平均分,并放在记录的ave成员中。

例题:某学生的记录由学号、5门课程成绩和平均分组成,学号和5门课程的成绩已在主函数中给出。请编写函数fun,它的功能是:求出该学生的平均分,并放在记录的ave成员中。请自己定义正确的形参。。

例如,若学生的成绩是72、83、90、68.5、71.5,则他的平均分应当是77.000。
请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

代码如下:

#include<stdio.h>
#define M 5
typedef struct
{
    
    
	char num[10];
	double s[M];
	double ave;
}SCORE;
void fun(SCORE*p)
{
    
    
	double t=0.0;
	int i;
	for(i=0;i<M;i++)
	t+=p->s[i];
	t/=M;
	p->ave=t;
}
main()
{
    
    
	SCORE s={
    
    "GA005",72,83,90,68.5,71.5};
	int i;
	FILE*out;
	fun(&s);
	printf("The %s's student data:\n",s.num);
	for(i=0;i<M;i++)
		printf("%4.1f\n",s.s[i]);
	printf("\nave=%7.3f\n",s.ave);
	out=fopen("outfile.dat","w");
	fprintf(out,"The %s's student data:\n",s.num);
	for(i=0;i<M;i++)
		fprintf(out,"%4.1f\n",s.s[i]);
	fprintf(out,"\nave=%7.3f\n",s.ave);
	fclose(out);
}

输出运行窗口如下:
在这里插入图片描述

越努力越幸运!
加油,奥力给!!!

猜你喜欢

转载自blog.csdn.net/qq_45385706/article/details/111631915