习题 8.15 有一个班4个学生,5门课程。1. 求第1门课程的平均分;2.找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均成绩;3.找出平均成绩在90分以上或全部课程成绩在85分以
C程序设计(第四版) 谭浩强 习题8.15 个人设计
习题 8.15 有一个班4个学生,5门课程。1. 求第1门课程的平均分;2.找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均成绩;3.找出平均成绩在90分以上或全部课程成绩在85分以上的学生。分别编3个函数实现以上3个要求。
代码块:
#include
#include
void aver_fcourse(int *s[4], int n); //定义函数1
void two_fail(int *s[4], int m, int n); //定义函数2
void high_score(int *s[4], int m, int n); //定义函数3
int main()
{int *stu_score[4], i, j;for (i=0; i<4; i++){stu_score[i]=(int *)malloc(3*sizeof(int)); //给学生成绩分配动态空间printf("Please enter No.%d student score: ", i+1); //输入学生成绩for (j=0; j<5; scanf("%d", *(stu_score+i)+j), j++);}aver_fcourse(stu_score, 4); //调用函数1two_fail(stu_score, 4, 5); //调用函数2high_score(stu_score, 4, 5); //调用函数3return 0;
}
//函数1
void aver_fcourse(int *s[4], int n)
{int i;float sum;for (i=0, sum=0; iprintf("The first course average score: %.2f\n", sum/n);
}
//函数2
void two_fail(int *s[4], int m, int n)
{int i, j, k, cc;float sum;for (i=0; ifor (j=0, cc=0; j60 ? cc++, j++ : j++);if (cc>=2){printf("No.%d student is fail. Score: ", i+1);for (k=0, sum=0; kprintf("%d ", *(*(s+i)+k)), sum+=*(*(s+i)+k), k++);printf("\nAverage=%.2f\n", sum/n);}}
}
//函数3
void high_score(int *s[4], int m, int n)
{int i, j, cc;float sum, aver;for (i=0; ifor (j=0, cc=0, sum=0; j85 ? cc++, j++ : j++);aver=sum/n;if (aver>90||cc==5)printf("No.%d student is high score.\n", i+1);}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
