代码拉取完成,页面将自动刷新
#define _CRT_SECURE_NO_WARNINGS
#define M 10 //学生数
#define N 5 //课程数
#include<stdio.h>
float avg_stu[M] = { 0 };
float avg_course[N] = { 0 };
float highest = 0.0;
float variance = 0.0;
int stu_index, course_index;//最高分学生的课程下标
void avg_score_stu(float score [M][N])
{
int i = 0;
int j = 0;
float sum = 0.0;
for (i = 0; i < M; i++)
{
sum = 0.0;
for (j = 0; j < N; j++)
{
sum += score[i][j];
}
avg_stu[i] = sum / N;
}
}
void avg_score_course(float score[M][N])
{
int i = 0;
int j = 0;
float sum = 0.0;
for (i = 0; i < N; i++)
{
sum = 0.0;
for (j = 0; j < M; j++)
{
sum += score[j][i];
}
avg_course[i] = sum / M;
}
}
//计算最高分;
float highest_score(float score[M][N])
{
float highest = 0.0;
int i = 0;
int j = 0;
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
if (score[i][j] > highest)
{
highest = score[i][j];
stu_index = i;
course_index = j;
}
}
}
return highest;
}
//平均分的方差
float variance_avg_score()
{
int i = 0;
int j = 0;
float sum_avg_square = 0.0;//平均分平方和
float sum_avg_score=0.0;//平均分和
for (i = 0; i < M; i++)
{
sum_avg_square += avg_stu[i] * avg_stu[i];
sum_avg_score += avg_stu[i];
}
return sum_avg_square / M - (sum_avg_score / M) * (sum_avg_score / M);
}
void print_result(float score[M][N])
{
printf("N0 cour1 cour2 cour3 cour4 cour5 avger\n");
int i = 0;
int j = 0;
for (i = 0; i < M; i++)
{
printf("N%-5d", i + 1);
for (j = 0; j < N; j++)
{
printf("%-9.1f ", score[i][j]);
}
printf("%-9.1f\n",avg_stu[i]);
}
//显示每一门课的平均分
printf("%-5s ", "avg");
for (i = 0; i < N; i++)
{
printf("%-9.1f", avg_course[i]);
}
printf("\n");
printf("最高分为:%.1f,是第%d位同学的第%d门课程\n", highest, stu_index+1, course_index+1);
printf("平均分的方差:%.1f",variance);
}
int main()
{
float score[M][N] =
{
{1,2,3,4,5},
{2,3,4,5,6},
{3,4,5,6,7},
{4,5,6,7,8},
{5,6,7,8,9},
{6,7,8,9,1},
{7,8,9,1,2},
{8,9,1,2,3},
{9,1,2,3,4},
{1,3,5,7,9}
};
//计算每个学生的平均分
avg_score_stu(score);
//计算每个课程的平均分
avg_score_course(score);
highest = highest_score(score);
variance=variance_avg_score();
print_result(score);
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。