C language programming> Fourteenth week ⑦ Please write a function fun, its function is: calculate the average score of n courses, the calculation result is returned as the function value.

Example: Please write a function fun. Its function is to calculate the average score of n courses and return the result as a function value.

例如,若有5门课程的成绩是88、92、80、61.5、55;则函数的值为75.30。
请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

代码如下:

#include<stdio.h>
float fun(float*b,int m)
{
    
    
	float ave=0.0;
	int i;
	for(i=0;i<m;i++)
		ave+=b[i];
	ave/=m;
	return ave;
}
main()
{
    
    
	float s[10]={
    
    88,92,80,61.5,55},aver;
	FILE*out;
	aver=fun(s,5);
	printf("\nAverage is:%5.2f\n",aver);
	out=fopen("outfile.dat","w");
	fprintf(out,"%5.2f",aver);
	fclose(out);
}

The output running window is as follows:
Insert picture description here

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

Guess you like

Origin blog.csdn.net/qq_45385706/article/details/112061041
Recommended