题目描述
期末考试快到了,为了下学期开始的评优,院长给老师下达了任务--做一个统计学生成绩的程序,给他老人家省省事。任务内容是:
编写一个函数void calcscore(int n),在函数中输入n个人的成绩,计算最高分,最低分,总分和平均分,要求在主函数中调用函数calcscore计算各种成绩,并在主函数中输出各种计算结果。(使用全局变量在函数之间传递多个数据)
当然,老师不能把如此重大的任务全交给你做,他只是为了考考你,改了一个C语言版的小题目,由你来完成喽~
#include <stdio.h>
double HighScore; /*全局变量,最高分*/
double LowScore; /*全局变量,最低分*/
double SumScore; /*全局变量,总分*/
double AverageScore; /*全局变量,平均分*/
void calcscore(int n); /*函数声明*/
int main()
{
int n;
scanf("%d",&n);
calcscore(n);
printf("%g %g %g %g\n",HighScore,LowScore,SumScore,AverageScore);
return 0;
}
主程序已给出,请完成calcscore函数并提交
输入
学生人数n和n个学生的成绩。
输出
n个人的最高分,最低分,总分和平均分
样例输入
5
80 90 100 70 50
样例输出
100 50 390 78
解答:
#include <stdio.h> double HighScore; double LowScore; double SumScore; double AverageScore; void calcscore(int n); int main() { int n; scanf("%d",&n); calcscore(n); printf("%g %g %g %g\n",HighScore,LowScore,SumScore,AverageScore); return 0; } void calcscore(int n) { int i; double s; HighScore=-1; LowScore=1000; SumScore=0; for (i=0;i<n;i++) { scanf("%lf", &s); if(s>HighScore) HighScore=s; if(s<LowScore) LowScore=s; SumScore+=s; } AverageScore=SumScore/n; return; }