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 < stu; i++){
printf("Enter No.%d student scores in %d courses:\n", i+1, course);
for(int j = 0; j < course; j++){
printf("Enter No.%d course score: ", j+1);
scanf("%f", &stuScore[i][j]);
while(stuScore[i][j] < 0 || 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 < stu; i++){
for(int j = 0; j < course; j++){
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 < course; i++){
for(int j = 0; j < stu; j++){
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 < stu; i++){
for(int j = 0; j < course; j++){
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 < stu; i++){
sum1 += stuAver[i] * stuAver[i];
sum2 += stuAver[i];
}
float sigma = sum1 / stu - (sum2 / stu) * (sum2 / stu);
printf("The mean variance is %.2f\n", sigma);
}
int main(){
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 < stu; i++){
(*stuScore)[i] = (float*)malloc(course * sizeof(float));
}
*stuAver = (float*)malloc(stu * sizeof(float));
}
//输入课程成绩
void inputScore(float **stuScore, int stu, int course){
printf("Enter the scores of %d students in %d courses:\n", stu, course);
for(int i = 0; i < stu; i++){
printf("Enter No.%d student scores in %d courses:\n", i+1, course);
for(int j = 0; j < course; j++){
printf("Enter No.%d course score: ", j+1);
scanf("%f", &stuScore[i][j]);
//成绩不合理报错,重新输入
while(stuScore[i][j] < 0 || 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 < stu; i++){
for(int j = 0; j < course; j++){
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 < course; i++){
for(int j = 0; j < stu; j++){
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 < stu; i++){
for(int j = 0; j < course; j++){
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 < stu; i++){
sum1 += stuAver[i] * stuAver[i];
sum2 += stuAver[i];
}
float sigma = sum1 / stu - (sum2 / stu) * (sum2 / stu);
printf("The mean variance is %.2f\n", sigma);
}
//调用函数的函数
void function(float **stuScore, int stu, int course, float *stuAver, void (*fun[])(float**, int, int, float*)){
for(int i = 0; i < 4; i++){
fun[i](stuScore, stu, course, stuAver);
}
}
//释放内存
void freeStuScore(float ***stuScore, int stu, int course, float **stuAver){
for(int i = 0; i < stu; i++){
free((*stuScore)[i]);
}
free(*stuScore);
free(*stuAver);
}
int main(){
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 < c; i++){
printf("Enter No.%d course score: ", i+1);
scanf("%f", &score[i]);
while(score[i] < 0 || score[i] > 100){
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 < c; i++){
sum += score[i];
}
return sum / c;
}
float Student::highestScore(int c){
float high = 0;
for(int i = 0; i < c; i++){
if(score[i] > high){
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 < s; i++){
stu[i].inputScore(i+1, c);
}
}
void stuScoreAverage(Student *stu, int s, int c){
for(int i = 0; i < s; i++){
printf("The average score of No.%d student is %.2f\n", i+1, stu[i].courseAverage(c));
}
}
void stuCourseAverage(Student *stu, int s, int c){
float sum = 0.0;
for(int i = 0; i < c; i++){
for(int j = 0; j < s; j++){
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 < s; i++){
if(stu[i].highestScore(c) > high){
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 < s; i++){
sum1 += stu[i].courseAverage(c) * stu[i].courseAverage(c);
sum2 += stu[i].courseAverage(c);
}
float sigma = sum1 / s - (sum2 / s) * (sum2 / s);
printf("The mean variance is %.2f\n", sigma);
}
void function(Student *stu, int s, int c, void (*fun[])(Student*, int, int)){
for(int i = 0; i < 5; i++){
fun[i](stu, s, c);
}
}
void freeStu(Student **stu){
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 < 10; i++)
for(j = 0; j < 5; j++)
scanf("%f",&a[i][j]);
for(i = 0; i < 10; i++)
printf("第%d名同学平均分为%f\n",i, average1(a, i));
for(j = 0; j < 5; j++)
printf("第%d门课程平均分为%f\n",j,average2(a, j));
system("pause");
return 0;
}
float average1(float a[][5],int n){
int c;
float d;
float sum = 0;
for(c = 0; c < 5; c++)
sum = sum + a[n][c];
d = sum / 5;
return d;
}
float average2(float a[][5],int b){
int c;
float k;
float sum = 0;
for(c = 0; c < 10; c++)
sum = sum + a[c][b];
k = sum / 10;
return k;
}