funcptr.c (3874B)
1 /* Exercise 7.28 */ 2 3 /* Fig. 6.22: fig06_22.c 4 Double-subscripted array example */ 5 #include <stdio.h> 6 #define STUDENTS 3 7 #define EXAMS 4 8 9 /* function prototypes */ 10 void minimum( const int grades[][ EXAMS ], int pupils, int tests ); 11 void maximum( const int grades[][ EXAMS ], int pupils, int tests ); 12 void average( const int setOfGrades[][ EXAMS ], int pupils, int tests ); 13 void printArray( const int grades[][ EXAMS ], int pupils, int tests ); 14 15 /* function main begins program execution */ 16 int main(void) 17 { 18 /* initialize student grades for three students (rows) */ 19 const int studentGrades[ STUDENTS ][ EXAMS ] = 20 { { 77, 68, 86, 73 }, 21 { 96, 87, 89, 78 }, 22 { 70, 90, 86, 81 } }; 23 24 void (*processGrades[4])(const int [][EXAMS], int, int) = { 25 printArray, minimum, maximum, average 26 }; 27 int sel; 28 29 while(1) { 30 printf("Enter a choice:\n" 31 " 0 Print the array of grades\n" 32 " 1 Find the minimum grade\n" 33 " 2 Find the maximum grade\n" 34 " 3 Print the average on all tests for each student\n" 35 " 4 End program\n"); 36 printf("Selection: "); 37 scanf("%d", &sel); 38 39 if(sel > 4 || sel < 0) { 40 printf("\nInvalid choice: choose another option\n\n"); 41 } 42 else if(sel == 4) 43 return 0; 44 else 45 break; 46 } 47 48 (*processGrades[sel])( studentGrades, STUDENTS, EXAMS ); 49 50 return 0; /* indicates successful termination */ 51 52 } /* end main */ 53 54 /* Find the minimum grade */ 55 void minimum( const int grades[][ EXAMS ], int pupils, int tests ) 56 { 57 int i; /* student counter */ 58 int j; /* exam counter */ 59 int lowGrade = 100; /* initialize to highest possible grade */ 60 61 /* loop through rows of grades */ 62 for ( i = 0; i < pupils; i++ ) { 63 64 /* loop through columns of grades */ 65 for ( j = 0; j < tests; j++ ) { 66 67 if ( grades[ i ][ j ] < lowGrade ) { 68 lowGrade = grades[ i ][ j ]; 69 } /* end if */ 70 71 } /* end inner for */ 72 73 } /* end outer for */ 74 75 printf("Lowest grade: %d\n", lowGrade); 76 77 } /* end function minimum */ 78 79 /* Find the maximum grade */ 80 void maximum( const int grades[][ EXAMS ], int pupils, int tests ) 81 { 82 int i; /* student counter */ 83 int j; /* exam counter */ 84 int highGrade = 0; /* initialize to lowest possible grade */ 85 86 /* loop through rows of grades */ 87 for ( i = 0; i < pupils; i++ ) { 88 89 /* loop through columns of grades */ 90 for ( j = 0; j < tests; j++ ) { 91 92 if ( grades[ i ][ j ] > highGrade ) { 93 highGrade = grades[ i ][ j ]; 94 } /* end if */ 95 96 } /* end inner for */ 97 98 } /* end outer for */ 99 100 printf("Highest grade: %d\n", highGrade); 101 102 } /* end function maximum */ 103 104 /* Determine the average grade for a particular student */ 105 void average( const int setOfGrades[][ EXAMS ], int pupils, int tests ) 106 { 107 int i, j; /* exam counters */ 108 int total = 0; /* sum of test grades */ 109 110 /* total all grades for one student */ 111 for ( i = 0; i < pupils; i++ ) { 112 for( j = 0; j < tests; j++) { 113 total += setOfGrades[ i ][ j ]; 114 } 115 116 printf("The average grade for student %d is %.2f\n", 117 i, (double) total / j); 118 total = 0; 119 } /* end for */ 120 121 } /* end function average */ 122 123 /* Print the array */ 124 void printArray( const int grades[][ EXAMS ], int pupils, int tests ) 125 { 126 int i; /* student counter */ 127 int j; /* exam counter */ 128 129 printf( "The array is:\n" ); 130 131 /* output column heads */ 132 printf( " [0] [1] [2] [3]" ); 133 134 /* output grades in tabular format */ 135 for ( i = 0; i < pupils; i++ ) { 136 137 /* output label for row */ 138 printf( "\nstudentGrades[%d] ", i ); 139 140 /* output grades for one student */ 141 for ( j = 0; j < tests; j++ ) { 142 printf( "%-5d", grades[ i ][ j ] ); 143 } /* end inner for */ 144 145 } /* end outer for */ 146 printf("\n"); 147 148 } /* end function printArray */ 149