Thread: arrays of pointers to function?????

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    11

    Unhappy 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
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    		//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);
    Strip away all the declaration fluff - it's no different from calling any other function
    Code:
    	//invoke function at location choice in array 
    	//and pass studentgarde array, students and exams as an argument
    	processGrades[choice](studentGrades, students, exams);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    Thx salem . I followed your instruction, but the error comes again.
    error C2065: 'studentGrades' : undeclared identifier



  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    I have declared "studentGrades" in Main, what's wrong with my code, ANY ONE? HELP!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > stduentGrades
    Try spelling your variables consistently (or correctly) then
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    It appears you have a few bugs in there.


    I've debugged it for you. Mostly, what I did was changed your main a bit and put *int* instead of void for your function definitions.

    Also your the title for your array was fubar. lol


    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
    int printArray(int [][exams],int,int);
    int minimum(int [][exams],int,int);
    int maximum(int [][exams],int,int);
    int average(int [][exams],int,int);
    
    int main()
    {
    	//initialize student grades for three students (rows)
    	int studentGrades[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;
    	if(choice==0)
    	{
    	    printArray(studentGrades,students,exams);
    	}   
        if(choice==1)
        {
            minimum(studentGrades,students,exams);
        }
        if(choice==2)
        {
            maximum(studentGrades,students,exams);
        }
        if(choice==3)
        {
            average(studentGrades,students,exams);
        }             
    	
    
    
    	cout<<"Program execution completed."<<endl;
    	int stop;
    	cin>>stop;
    
    	return 0; //indicates successful termination
    
    }//end main
    
    int 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
    
    int 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;
    }
    
    int 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;
    }
    
    int 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
    
    }

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    11

    Unhappy

    Hi treenef, Thanks for your help. (it works when i still use void as return type)
    The problem is why (*processGrades[choice]) (int studentGrades[students][exams], int students, int exams);won't work?
    Anyone who has experience with "arrays of pointers to function" can help. Please...
    BTW, thanks salem, I have checked all "studentGrades" but no miss spelling, i tried you way, just won't work. Anyway thanks for you help.
    Can anyone take a look at my code again please...
    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 number between 0 and 3, 4 to end: ";
    		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
    
    }
    Last edited by andyzjk; 04-16-2005 at 04:42 PM.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    any one please help

  9. #9
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    The problem is why (*processGrades[choice]) (int studentGrades[students][exams], int students, int exams);won't work?

    The reason why it doesn't work is because your syntax is all wrong.
    When passing arrays into functions don't really need to use the '*' notation. As shown in my example.

    As far as I can tell the rest of program appears to be ok. The only problem I think you have is actually *calling the function*. Read up on this!!

    Secondly,

    BTW, thanks salem, I have checked all "studentGrades" but no miss spelling
    I don't think you have, notice anything wrong here?


    Code:
    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}	};
    Oh and another thing, it's wise to put brackets in your *for loops* to clarify their parenthesis..


  10. #10
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Does this do what you want? I've added the while loop, but I thought that was fairly simple?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Oh please, I fixed the two things I previously mentioned
    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 studentGrades[students][exams]=  //!! FIXED!!! (as per previous comments)
    	{	{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);
    		processGrades[choice](studentGrades,students,exams); //!! FIXED!!! (as per previous comments)
    
    
    		cout<<"\nEnter a number between 0 and 3, 4 to end: "; //!! FIXED MINOR FORMATTING
    		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
    
    }
    And got these results.
    Code:
    $ g++ hello.cpp
    $ ./a.out
    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 test for each student
    4  End program
    
    0
                     [0]  [1]   [2]   [3]
    studentGrade[0]77   68   86   73
    studentGrade[1]96   87   89   78
    studentGrade[2]70   90   86   81
    Enter a number between 0 and 3, 4 to end: 1
    Lowest Grade is 68
    
    Enter a number between 0 and 3, 4 to end: 2
    Highest Grade is 96
    
    Enter a number between 0 and 3, 4 to end: 3
    The average grade for student0is76.00
    The average grade for student1is87.50
    The average grade for student2is81.75
    
    Enter a number between 0 and 3, 4 to end: 4
    Program execution completed.
    The rest of the formatting you can fix yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    to both treenef and salem You guys are so great! Problem solved!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM