Enter the scores of 10 students in 5 subjects, and use the function to find: ①The average score of each student; ②The average score of each subject; ③Find the student and course corresponding to the highest score; σ=1/n∑xi2-(∑xi/n)2, xi is the average score of a certain student. Requirements: The above functions are placed in the special program file prog_fun.cpp, and the main function is placed in the independent program file prog_main.cpp, and all the above functions are called in the main function for testing.
The first time I wrote a large program file, I learned all the theory before, but it is still not easy to realize it. Record it.
Main function file (prog_main.cpp)
#include <stdio.h>
#include "fun.h"
int main(int argc, char *argv[]){
double a[10][5];//该数组用于储存同学成绩
int i,j;
for(i=0; i<10; i++){
printf("请输入第%d个同学的各科成绩:",i+1);
for(j=0; j<5; j++){
scanf("%lf", &a[i][j]);//读入各科成绩
}
}
//调用各个函数
stu_ave(a);//输出每个同学的各科平均分
gra_ave(a);//输出每个学科的平均分
max_gra(a);//输出最高分和对应的同学
Var(a);//输出平均分方差
return 0;
}
Custom function file (prog_fun.cpp)
#include "fun.h"
void stu_ave(double a[10][5]){
//每个同学的各科平均分
int i,j;
double ave,sum;
for(i=0;i<10;i++){
//第i+1个同学
sum = 0;//sum归零
for(j=0;j<5;j++){
sum += a[i][j];//该同学各科成绩总和
}
ave = sum/5.00;//第i+1个同学的成绩平均分
printf("第%d个同学的成绩平均分%.2f\n",i+1,ave);
}
}
void gra_ave(double a[10][5]){
//每门课的平均分;
int i,j;
double ave,sum;
for(j=0;j<5;j++){
//第i+1门课
sum = 0;//sum归零
for(i=0;i<10;i++){
sum += a[i][j];//本科目所有同学成绩总和
}
ave = sum/10.00;//第i+1门课平均分
printf("第%d门课的平均分分别为%.2f\n",j+1,ave);
}
}
void max_gra(double a[10][5]){
//所有分数中最高分;
double max = a[0][0];//初始化为第一名同学第一门科目
int stu = 0;
int i,j;
for(i=0; i<10; i++){
for(j=0; j<5; ++j){
if(a[i][j]>max){
//判断是否有人某科分数比他高
max = a[i][j];//最高分为该分数
stu = i;//最高分拥有者转为该同学
}
}
}
printf("所有分数中最高分max=%.2f,对应学生为第%d个学生\n",max,stu+1);
}
void Var(double a[10][5]){
int i, j, k, m;
double ave[10], var, x1, x2;//ave数组用于储存平均分
for (i=0, m=0; i<10; i++, m++){
for (j=0, k=0; j<5; j++)
k += a[i][j];//每名学生成绩总和
ave[m]=k/5;//每名学生平均分
}
for (i=m=x1=x2=0; i<10; i++){
x1 += pow(ave[i], 2);//求和每个学生平均分的平方
x2 += ave[i];//求和每个学生平均分
}
var = x1/10-pow(x2/10, 2);//平均分方差
printf("平均分方差是%.2f\n", var);
}
Declare function file (fun.h)
#include <stdio.h>
#include <math.h>
#define _function_
void stu_ave(double a[10][5]);
void gra_ave(double a[10][5]);
void max_gra(double a[10][5]);
void Var(double a[10][5]);