Use two-dimensional array to store students's scores of several courses.
int studentGrades[STUDENTS][COURSES];
We are going to do following four subtasks:
1, print out all scores
2, find out the highest score over all students and all courses
3, find out average score for every student.
4, find out average score for every courses.
We can design a function to acomplish each of these four tasks.
Note: students' scores will be passed by through function parameters: array as parameter.
Program
/* Desc: 用二维数组来存储多个学生的多课成绩单
1,输出全部学生的各科成绩
2,找出最高得分
3,计算每位同学的平均分
4,计算每门课的平均分
Author: Liutong XU
*/
#include<stdio.h>
#define STUDENTS 3
#define COURSES 4
int maximum(int grades[STUDENTS][COURSES],int students,int courses);
void printArray(int grades[STUDENTS][COURSES],int students,int courses);
float avgForStudent (int setOfGrades[],int courses);
float avgForCourse(int grades[STUDENTS][COURSES],int students, int course);
int main()
{
int studentGrades[STUDENTS][COURSES] = {{89,98,67,77},{98,77,65,88},{90,80,89,66}};
int i;
printArray(studentGrades,STUDENTS,COURSES);
printf("\nThe max score is %d\n\n",maximum(studentGrades,STUDENTS,COURSES));
for (i=0;i<STUDENTS;i++)
printf("The avg score of student #%d = %f\n",i,avgForStudent(studentGrades[i],COURSES));
printf("\n");
for (i=0;i<COURSES;i++)
printf("The avg score of course #%d is %f\n",i,avgForCourse(studentGrades,STUDENTS,i));
return 0;
}
//Output all scores
void printArray(int grades[STUDENTS][COURSES],int students,int courses)
{
int i,j;
for(i=0;i<=students-1;i++){
printf("studentGrades[%d] ",i);
for(j=0;j<=courses-1;j++)
printf("%-5d",grades[i][j]);
printf("\n");
}
}
//calculate the highest score among all students
int maximum(int grades[STUDENTS][COURSES],int students, int courses)
{
int i,j,highGrade=0;
for(i=0;i<=students-1;i++)
for(j=0;j<=courses-1;j++)
if(grades[i][j]>highGrade)
highGrade=grades[i][j];
return highGrade;
}
//Calculate average score for each student
float avgForStudent(int setOfGrades[],int courses)
{
int i,total=0;
for(i=0;i<=courses-1;i++)
total+=setOfGrades[i];
return (float)total/courses;
}
float avgForCourse(int grades[STUDENTS][COURSES],int students, int course)
{
int i,total=0;
for(i=0;i<=students-1;i++)
total+=grades[i][course];
return (float)total/students;
}
stdout
studentGrades[0] 89 98 67 77
studentGrades[1] 98 77 65 88
studentGrades[2] 90 80 89 66
The max score is 98
The avg score of student #0 = 82.750000
The avg score of student #1 = 82.000000
The avg score of student #2 = 81.250000
The avg score of course #0 is 92.333333
The avg score of course #1 is 85.000000
The avg score of course #2 is 73.666667
The avg score of course #3 is 77.000000
test