作者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
推 v900149:int highGrade=100; 應為 int highGrade=0; 才對 05/20 22:06
→ v900149:根本不會有一成績會大於100的好嘛 05/20 22:06
→ cocoyan:sorry 複製的時候打錯了@@ min和max長太像了 複製好快XD 05/20 22:18