C //习题 7.14 输入10个学生5门课的成绩,分别用函数实现下列功能:1.计算每个学生的平均分;2.计算每门课的平均分;3.找出所有50个分数中最高的分数所对应的学生和课程;4.计算平均分方差。

您所在的位置:网站首页 BTS中输排名 C //习题 7.14 输入10个学生5门课的成绩,分别用函数实现下列功能:1.计算每个学生的平均分;2.计算每门课的平均分;3.找出所有50个分数中最高的分数所对应的学生和课程;4.计算平均分方差。

C //习题 7.14 输入10个学生5门课的成绩,分别用函数实现下列功能:1.计算每个学生的平均分;2.计算每门课的平均分;3.找出所有50个分数中最高的分数所对应的学生和课程;4.计算平均分方差。

2023-12-11 17:21| 来源: 网络整理| 查看: 265

C程序设计 (第四版) 谭浩强 习题7.14 习题 7.14 输入10个学生5门课的成绩,分别用函数实现下列功能: 1. 计算每个学生的平均分; 2. 计算每门课的平均分; 3. 找出所有50个分数中最高的分数所对应的学生和课程; 4. 计算平均分方差:

σ = 1 n ∑ x i 2 − ( ∑ x i n ) 2 \sigma=\frac{1}{n}\sum x_i^2-\Bigg(\frac{\sum x_i}{n}\Bigg)^2 σ=n1​∑xi2​−(n∑xi​​)2

其中, x i x_i xi​为某一学生的平均分。 IDE工具:VS2010 Note: 使用不同的IDE工具可能有部分差异。

 

代码块 方法1:使用数组 #include #include void inputScore(float stuScore[][5], int stu, int course){ printf("Enter the scores of %d students in %d courses:\n", stu, course); for(int i = 0; i printf("Enter No.%d course score: ", j+1); scanf("%f", &stuScore[i][j]); while(stuScore[i][j] 100){ printf("Score Error! Retry!\nEnter No.%d course score: ", j+1); scanf("%f", &stuScore[i][j]); } } } printf("\n"); } void stuScoreAverage(float stuScore[][5], int stu, int course, float *stuAver){ float sum = 0.0; int k = 0; for(int i = 0; i sum += stuScore[i][j]; } stuAver[k++] = sum / course; printf("The average score of No.%d student is %.2f\n", i+1, sum/course); sum = 0.0; } printf("\n"); } void courseAverage(float stuScore[][5], int stu, int course){ float sum = 0.0; for(int i = 0; i sum += stuScore[j][i]; } printf("The average score of No.%d course is %.2f\n", i+1, sum/stu); sum = 0.0; } printf("\n"); } void highestScore(float stuScore[][5], int stu, int course){ float highScore = stuScore[0][0]; int highStu, highCourse; for(int i = 0; i if(stuScore[i][j] > highScore){ highScore = stuScore[i][j]; highStu = i + 1; highCourse= j + 1; } } } printf("The highest score is %.2f, No.%d Student, No.%d Course\n\n", highScore, highStu, highCourse); } void meanVariance(float stuScore[][5], int stu, int course, float *stuAver){ float sum1 = 0.0; float sum2 = 0.0; for(int i = 0; i float stuScore[10][5]; float stuAver[10]; inputScore(stuScore, 10, 5); stuScoreAverage(stuScore, 10, 5, stuAver); courseAverage(stuScore, 10, 5); highestScore(stuScore, 10, 5); meanVariance(stuScore, 10, 5, stuAver); system("pause"); return 0; } 方法2:使用指针、函数指针、动态分配内存 #include #include #define S 10 //定义学生数 #define C 5 //定义课程数 //初始化参数 void initialStuScore(float ***stuScore, int stu, int course, float **stuAver){ *stuScore = (float**)malloc(stu * sizeof(float*)); for(int i = 0; i printf("Enter the scores of %d students in %d courses:\n", stu, course); for(int i = 0; i printf("Enter No.%d course score: ", j+1); scanf("%f", &stuScore[i][j]); //成绩不合理报错,重新输入 while(stuScore[i][j] 100){ printf("Score Error! Retry!\nEnter No.%d course score: ", j+1); scanf("%f", &stuScore[i][j]); } } } printf("\n"); } void stuScoreAverage(float **stuScore, int stu, int course, float *stuAver){ float sum = 0.0; int k = 0; for(int i = 0; i sum += stuScore[i][j]; } stuAver[k++] = sum / course; printf("The average score of No.%d student is %.2f\n", i+1, sum/course); sum = 0.0; } printf("\n"); } void courseAverage(float **stuScore, int stu, int course, float *stuAver){ float sum = 0.0; for(int i = 0; i sum += stuScore[j][i]; } printf("The average score of No.%d course is %.2f\n", i+1, sum/stu); sum = 0.0; } printf("\n"); } void highestScore(float **stuScore, int stu, int course, float *stuAver){ float highScore = stuScore[0][0]; int highStu, highCourse; for(int i = 0; i if(stuScore[i][j] > highScore){ highScore = stuScore[i][j]; highStu = i + 1; highCourse= j + 1; } } } printf("The highest score is %.2f, No.%d Student, No.%d Course\n\n", highScore, highStu, highCourse); } void meanVariance(float **stuScore, int stu, int course, float *stuAver){ float sum1 = 0.0; float sum2 = 0.0; for(int i = 0; i for(int i = 0; i for(int i = 0; i float **stuScore = NULL; float *stuAver = NULL; void (*fun[4])(float**, int, int, float*) = {stuScoreAverage, courseAverage, highestScore, meanVariance}; initialStuScore(&stuScore, S, C, &stuAver); inputScore(stuScore, S, C); function(stuScore, S, C, stuAver, fun); freeStuScore(&stuScore, S, C, &stuAver); system("pause"); return 0; } 方法3:使用结构体 #include #include #define S 10 #define C 5 struct Student{ float score[C]; void inputScore(int s, int c); float courseAverage(int c); float highestScore(int c); int courseHighScore; }; void Student::inputScore(int s, int c){ printf("Enter No.%d student scores in %d courses:\n", s, c); for(int i = 0; i printf("Score Error! Retry!\nEnter No.%d course score: ", i+1); scanf("%f", &score[i]); } } printf("\n"); } float Student::courseAverage(int c){ float sum = 0.0; for(int i = 0; i float high = 0; for(int i = 0; i high = score[i]; courseHighScore = i + 1; } } return high; } void initialStu(Student **stu, int s){ *stu = (Student*)malloc(s * sizeof(Student)); } void inputStu(Student *stu, int s, int c){ printf("Enter the scores of %d students in %d courses:\n", s, c); for(int i = 0; i for(int i = 0; i float sum = 0.0; for(int i = 0; i sum += stu[j].score[i]; } printf("The average score of No.%d course is %.2f\n", i+1, sum/s); } printf("\n"); } void highestScore(Student *stu, int s, int c){ float high = stu[0].highestScore(c); int n; for(int i = 0; i high = stu[i].highestScore(c); n = i; } } printf("The highest score is %.2f, No.%d student, No.%d Course\n\n", high, n+1, stu[n].courseHighScore); } void meanVariance(Student *stu, int s, int c){ float sum1 = 0.0; float sum2 = 0.0; for(int i = 0; i for(int i = 0; i free(*stu); } int main(){ Student *stu = NULL; void (*fun[5])(Student*, int, int) = {inputStu, stuScoreAverage, stuCourseAverage, highestScore, meanVariance}; initialStu(&stu, S); function(stu, S, C, fun); freeStu(&stu); system("pause"); return 0; } 解答评论1 #include #include float average1(float a[][5], int n); float average2(float a[][5], int b); int main(){ int i,j; float a[10][5]; for(i = 0; i int c; float k; float sum = 0; for(c = 0; c


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3