Friday, March 16, 2012

Arrays of Pointers to Functions

Program below uses pointers to functions to simulate a menu-driven system.
Note that 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.
/*
 *   Title: Pointers 725 : Array of Pointer to Function
 *   Author: aeriqusyairi
 *   Date: Mac10 2012
 */
#include
#define STUDENTS 3
#define EXAMS 4

void minimum( const int grades[][ EXAMS ], int pupils, int tests );
void maximum( const int grades[][ EXAMS ], int pupils, int tests );
void average( const int grades[][ EXAMS ], int pupils, int tests );
void printArray( const int grades[][ EXAMS ], int pupils, int tests );

int main( void ){
   int student, choice = 0;
   
   const int studentGrades[ STUDENTS ][ EXAMS ] = 
      {{ 77, 68, 86, 73 },
       { 96, 87, 89, 78 },
       { 70, 90, 86, 81 }};
       
   void (*processGrades[ 4 ])( const int, int, int ) = { printArray, minimum, maximum, average };
       
   printf("Enter a choice:\n"
          " 0 Print the array of grades\n"
          " 1 Find the minimum grade\n"
          " 2 Find the maximum grade\n"
          " 3 Print the average on all tests for each student\n"
          " 4 End program\n\n"
          "choice: ");
          
   scanf("%d", &choice );
   printf("\n");
   
   while(choice >= 0 && choice < 4){
      (*processGrades[ choice ])( studentGrades, STUDENTS, EXAMS );
      
      printf("\n\n");
      system("pause");
      system("cls");
      printf("Enter a choice:\n"
          " 0 Print the array of grades\n"
          " 1 Find the minimum grade\n"
          " 2 Find the maximum grade\n"
          " 3 Print the average on all tests for each student\n"
          " 4 End program\n\n"
          "choice: ");
          
      scanf("%d", &choice );
      printf("\n");
   }
   
   printf("Program execution completed\n");
   system("pause");
   return 0;
}

void minimum( const int grades[][ EXAMS ], int pupils, int tests ){
  int i, j;
  int lowGrade = 100;
  
  for(i = 0; i < pupils; i++){
     for(j = 0; j  highGrade){
            highGrade = grades[ i ][ j ];           
         }
      }      
   }
   
   printf("Highest grade: %d", highGrade);
}

void average( const int grades[][ EXAMS ], int pupils, int tests ){
   int i, j;
   int total;
   
   for(i = 0; i < pupils; i++){
      
      total = 0;
      
      for(j = 0; j < tests; j++){
          total += grades[ i ][ j ];
      }
      printf("The average grade for student %d is %.2f\n", i, (double) total / tests );
   }
}

void printArray( const int grades[][ EXAMS ], int pupils, int tests ){
   int i, j;
   
   printf("The array is:\n");
   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 ] );
      }
   }
}

No comments:

Post a Comment