#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int a[40];
printf("Average score is %d\n", Average(ReadScore(a), a));
return 0;
}
int ReadScore(int a[])
{
int i, x;
for (i = 0; i < 40; i++)
{
printf("Input score:");
scanf("%d", &x);
if (x < 0)
{
break;
}
else
{
a[i] = x;
}
}
return i;
}
int Average(int n, int a[])
{
int i, sum = 0, aver;
for (i = 0; i < n; i++)
{
sum = sum + a[i];
}
printf("Total students are %d\n", n);
return sum/n;
}
用C语言用函数编程统计平均成绩。要求在主函数中输入学生成绩,输入负数时,结束输入,调用子函数Average()计算平均成绩,并输出结果。
于 2024-05-14 23:58:04 首次发布