qfedu-c-level/day6/homework/h3.c

48 lines
1.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 编写程序随机生成10位学生3门课程成绩汇总每位学生的总成绩和每门课程的平均成绩。 【提示】随机生成成绩的范围[0, 100]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
float stuScores[10][3]; // 学生成绩数组
float sumScores[10]; // 每位学生总成绩
float avgScores[3]; // 每门课程平均成绩
printf("生成随机成绩");
srand(time(NULL)); // 随机种子
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 3; j++)
{
float tempRandScore = rand() % 101;
stuScores[i][j] = tempRandScore;
}
}
printf("\n");
printf("每位学生的总成绩为: ");
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 3; j++)
{
sumScores[i] += stuScores[i][j];
}
printf("%.1f ", sumScores[i]);
}
printf("\n");
printf("每门课程的平均成绩为: ");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 10; j++)
{
avgScores[i] += stuScores[j][i] / 10.0;
}
printf("%.1f ", avgScores[i]);
}
printf("\n");
return 0;
}