作者cocoyan (锥子)
看板NTUBSE-B-96
标题[计程] 作业11
时间Sun May 18 12:22:39 2008
7.20
(Arrays of Pointers to Functions) Rewrite the program of Fig. 6.22 to use
a menu-driven interface. The program should offer the user four options as
follows:
Enter a choice:
0 Print the array of grades
1 Find the minimum grade
2 Find the maximum grade
3 Print the average on all tests for each student
4 End program
One restriction on using arrays of pointers to functions is that all the
pointers must have the same type. The pointers must be to functions of the
same return type that receive arguments of the same type. For this reason,
the functions in Fig.6.22 must be modified so that they each return the same
type and take the same parameters. Modify functions minimum and maximum to
print the minimum or maximum value and return nothing. For option 3, modify
function average of Fig.6.22 to output the average for each student
(not a specific student).Function average should return nothing and take the
same parameters as printArray,minimum and maximum. Store the pointers to the
four functions in array processGrades and use the choice made by the user as
the subscript into the array for calling each function.
Fig 6.22(无注解版)
#include<stdio.h>
#include<stdlib.h>
#define STUDENTS 3
#define EXAMS 4
int minimum(const int grades[][EXAMS],int pupils,int tests);
int maximum(const int grades[][EXAMS],int pupils,int tests);
double average(const int setOfGrades[],int tests);
void printArray(const int grade[][EXAMS],int pupils,int tests);
int main(void)
{
int student;
const int studentGrades[STUDENTS][EXAMS]=
{{77,68,86,73},{96,87,89,78},{70,90,86,81}};
printf("The array is:\n");
printArray(studentGrades,STUDENTS,EXAMS);
printf("\n\nLowest grade: %d\nHighest grade: %d\n",
minimum(studentGrades,STUDENTS,EXAMS),
maximum(studentGrades,STUDENTS,EXAMS));
for(student=0;student<STUDENTS;student++)
{
printf("The average grade for student %d is %.2f\n",
student,average(studentGrades[student],EXAMS));
}
system("pause");
return 0;
}
int minimum(const int grades[][EXAMS],int pupils,int tests)
{
int i;
int j;
int lowGrade=100;
for(i=0;i<pupils;i++)
{
for(j=0;j<tests;j++)
{
if(grades[i][j]<lowGrade)
{
lowGrade=grades[i][j];
}
}
}
return lowGrade;
}
int maximum(const int grades[][EXAMS],int pupils,int tests)
{
int i;
int j;
int highGrade=100;
for(i=0;i<pupils;i++)
{
for(j=0;j<tests;j++)
{
if(grades[i][j]>highGrade)
{
highGrade=grades[i][j];
}
}
}
return highGrade;
}
double average(const int setOfGrades[],int tests)
{
int i;
int total=0;
for(i=0;i<tests;i++)
{
total+=setOfGrades[i];
}
return (double)total/tests;
}
void printArray(const int grades[][EXAMS],int pupils ,int tests)
{
int i;
int j;
printf(" [0] [1] [2] [3]");
for(i=0;i<pupils;i++)
{
printf("\nstudentGrades[%d]",i);
for(j=0;j<tests;j++)
{
printf(" %-5d",grades[i][j]);
}
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.242.26
1F:推 v900149:int highGrade=100; 应为 int highGrade=0; 才对 05/20 22:06
2F:→ v900149:根本不会有一成绩会大於100的好嘛 05/20 22:06
3F:→ cocoyan:sorry 复制的时候打错了@@ min和max长太像了 复制好快XD 05/20 22:18