1 Star 0 Fork 0

LC.yulin/C.语言

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test.4.3.c 2.19 KB
一键复制 编辑 原始数据 按行查看 历史
LC.yulin 提交于 2022-04-03 22:07 . C语言基础代码练习
#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;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/lc-yulin/c-language.git
git@gitee.com:lc-yulin/c-language.git
lc-yulin
c-language
C.语言
master

搜索帮助