arrays of pointers to function?????
Hi all, I'm new to this board and C++, I'm writing a piece of code that uses arrays of pointers to function. The program contains a menu-driven interface. and there are five options, each them contains a certain function.
I wrote the following code, but it won't work and everytime i compile it ,it keeps telling me missing ")" before int...
Any ideas? Thx!
Code:
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
using std::fixed;
using std::left;
#include <iomanip>
using std::setw;
using std::setprecision;
const int students = 3;
const int exams = 4;
//function prototypes
void printArray(int [][exams],int,int);
void minimum(int [][exams],int,int);
void maximum(int [][exams],int,int);
void average(int [][exams],int,int);
int main()
{
//initialize student grades for three students (rows)
int stduentGrades[students][exams]=
{ {77,68,86,73},
{96,87,89,78},
{70,90,86,81} };
//initialize array of 4 pointers to functions that each
//takes an array and two integers argument and return nothing
void (*processGrades[4])(int [][exams], int, int) = {printArray,minimum,maximum,average};
int choice;
cout<<"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 test for each student\n"
<<"4 End program\n"<<endl;
cin>>choice;
//process user's choice
while ( choice >= 0 && choice <4 ){
//invoke function at location choice in array
//and pass studentgarde array, students and exams as an argument
(*processGrades[choice]) (int studentGrades[students][exams], int students, int exams);
cout<<"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 test for each student\n"
<<"4 End program\n"<<endl;
cin>>choice;
}
cout<<"Program execution completed."<<endl;
return 0; //indicates successful termination
}//end main
void printArray( int studentGrades[students][exams],int students,int exams)
{
cout << left << " [0] [1] [2] [3]";
// output grades in tablual format
for(int i=0; i<students; i++){
//output lable for row
cout << "\nstudentGrade[" <<i <<"]";
//output one grades for one student
for (int j=0; j<exams; j++)
cout << setw(5)<<studentGrades[i][j];
}//end outer for
}//end function printArray
void minimum(int studentGrades[students][exams],int students,int exams)
{
int lowGrade = 100; //initialize to highest possible grade
for ( int i=0; i<students; i++)
for (int j=0; j<exams; j++)
if ( studentGrades[i][j] < lowGrade)
lowGrade = studentGrades[i][j];
cout <<"Lowest Grade is "<< lowGrade<<endl;
}
void maximum(int studentGrades[students][exams],int students,int exams)
{
int highGrade = 0; //initialize to lowest possible grade
for ( int i=0; i<students; i++)
for (int j=0; j<exams; j++)
if ( studentGrades[i][j] > highGrade)
highGrade = studentGrades[i][j];
cout <<"Highest Grade is "<< highGrade<<endl;
}
void average(int studentGrades[students][exams],int students,int exams)
{
int i=0;
while(i<students){
int sum =0;//initialize sum to 0
double avg = 0.00; //initialize avarage to 0
for (int j=0; j<exams; j++)
sum += studentGrades[i][j];
avg = static_cast<double>(sum)/exams;
cout <<"The average grade for student"<<i<<"is"
<<fixed<<setprecision(2)<<avg<<endl;
i++;
}//end while loop
}