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

59 lines
1.9 KiB
C
Raw Permalink 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.

// 编写程序扩展第3题为10位学生输入姓名和课程名输出学生名和总成绩、课程名和平均成绩 。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
const int stuNum = 10; // 定义学生数
const int courseNum = 3; // 定义课程数
int main()
{
char stuNames[stuNum][20]; // 学生姓名数组
char courseNames[courseNum][20]; // 课程名数组
float stuScores[stuNum][courseNum]; // 学生成绩数组
float sumScores[stuNum]; // 每位学生总成绩
float avgScores[courseNum]; // 每门课程平均成绩
// printf("生成随机成绩");
srand(time(NULL)); // 随机种子
for (int i = 0; i < stuNum; i++)
{
printf("请输入第 %d 位学生的姓名: ", i + 1);
fgets(stuNames[i], sizeof(stuNames[i]), stdin);
stuNames[i][strlen(stuNames[i]) - 1] = '\0'; // 消除结尾的换行
for (int j = 0; j < courseNum; j++)
{
printf("请输入课程名: ");
fgets(courseNames[j], sizeof(courseNames[j]), stdin);
courseNames[j][strlen(courseNames[j]) - 1] = '\0';
float tempRandScore = rand() % 101;
stuScores[i][j] = tempRandScore;
}
}
printf("\n");
// printf("每位学生的总成绩为: ");
for (int i = 0; i < stuNum; i++)
{
for (int j = 0; j < courseNum; j++)
{
sumScores[i] += stuScores[i][j];
}
printf("%s 的总成绩为: %.1f\n", stuNames[i], sumScores[i]);
}
printf("\n");
// printf("每门课程的平均成绩为: ");
for (int i = 0; i < courseNum; i++)
{
for (int j = 0; j < stuNum; j++)
{
avgScores[i] += stuScores[j][i] / stuNum;
}
printf("%s 课的平均成绩为: %.1f\n", courseNames[i], avgScores[i]);
}
printf("\n");
return 0;
}