Solved

# Help with arrays of pointers to functions!!!

Posted on 2003-11-09

Hello all! I have been working on this work a few days now and can't seem to get the exact answer I am looking for. Maybe someone here can help me out. What I am trying to do is this: I am trying to get all the pointers to have the same type. The pointers must be to functions of the same return type that receive arguments of the same type. The minimum and maximum functions print only the minimum and maximum and don't return anything. The average should return nothingand take the same parameters as printArray, minimum, and maximum. The pointers of the four functions should be stored in array processGrades and use the choice made by the user as teh subscript into the array for calling each function. Please HELP me!! Thank you.

Here is my code so far:

#include <iostream>

using std::fixed;

using std::endl;

using std::cout;

using std::left;

using std::cin;

#include <iomanip>

using std::setw;

using std::setprecision;

const int students = 3;

const int exams = 4;

void minimum (int [] [exams], int, int);

void maximum (int [] [exams], int, int);

double average (int [], int);

void printArray (int [] [exams], int, int);

int main()

{

int choice = 0;

int studentGrades [students][exams] =

{{77, 68, 86, 73},

{96, 87, 89, 78},

{70, 90, 86, 81}};

while (choice != 4){

cout << "\nEnter a choice:" << endl;

cout << " 0 Print the array of grades" << endl;

cout << " 1 Find the minimum grade" << endl;

cout << " 2 Find the maximim grade" << endl;

cout << " 3 Print the average on all tests for each student" << endl;

cout << " 4 End program" << endl;

cout << "? ";

cin >> choice;

if (choice == 4) break;

if (choice == 0){

cout << "The is ";

printArray (studentGrades, students, exams); }

if (choice == 1){

cout << "\n\tThe lowest grade is ";

minimum(studentGrades, students, exams);}

if (choice == 2){

cout << "\n\tThe highest grade is ";

maximum(studentGrades, students, exams);}

if (choice == 3){

cout << fixed << setprecision(2);

for (int person = 0; person < students; person++)

cout << "\n\tThe average for student " << person << " is ";

average (studentGrades[person], exams);}

}

return 0;

}

void minimum (int grades[][exams], int pupils, int tests)

{

int lowGrade = 100;

for (int i = 0; i < pupils; i++)

for (int j = 0; j < tests; j++)

if (grades[i][j] < lowGrade)

lowGrade = grades[i][j];

cout << lowGrade << endl;

}

void maximum (int grades[][exams], int pupils, int tests)

{

int highGrade = 0;

for (int i = 0; i < pupils; i++)

for (int j = 0; j < pupils; j++)

if (grades[i][j] > highGrade)

highGrade = grades[i][j];

cout << highGrade << endl;

}

double average (int setOfGrades[], int tests)

{

int total = 0;

for (int i = 0; i < tests; i++)

total += setOfGrades[i];

cout << static_cast<double>(total) / tests << endl;

}

void printArray (int grades[] [exams], int pupils, int tests)

{

cout << left << " [0] [1] [2] [3]";

for (int i = 0; i < pupils; i++){

cout << "\nstudentGrades[" << i << "]";

for (int j = 0; i < tests; j++)

cout << setw(5) << grades[i][j];

}

}