Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

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];

}

}

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)

}

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];

}

}

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

void ( * processGrades[4] ) (int grades[][], int, int ) ;

The declaration of the grades parameter in this shouldn't compile. It should be

void ( * processGrades[4] ) (int grades[][exams], int, int ) ;

You will also, of course, have to change the average function so it matches the others.

--efn

My Function Pointer declaration does compile

Actually what u said is related to while declaring a function with multi-dimension array argument

When u are passing multi-dimension araay, u can omit the size of ONLY THE FIRST dimension. the size of rest all dimensions need to be given

But this is NOT TRUE when u are declaring a pointer to that function. Your function pointer need not mention the size of any of the array dimensions.

I couldn't find a definitive ruling on this in the standard, but Microsoft Visual C++ 6.0, Borland C++Builder 4, and gcc 3.2 all rejected the line.

VC++:

error C2087: '<Unknown>' : missing subscript

BCB:

E2453 Size of the type 'int[]' is unknown or zero.

gcc:

declaration of `grades' as multidimensional array must have bounds

The message from gcc appeared to be labeled as a warning, but was severe enough to keep the program from linking. This was with dev-c++.

So what compiler do you use that compiles this?

--efn

--efn

I'm not sure how you were able to compile that since in addition, it also has an extra variable name that doesn't belong there.

void ( * processGrades[4] ) (int grades[][], int, int ) ; //This should not compile

void ( * processGrades[4] ) (int [][exams], int, int ) ; // This is good

You can declare and initialize it at the same time via following method:

void ( * processGrades[4] ) (int [][exams], int, int ) = {minimum, maximum, printArray} ;

Before you can use the average function in this declaration, you need to change the return type and the arguments to match that of the other functions.

void ( * processGrades[4] ) (int grades[][], int, int ) ; //This should not compile

void ( * processGrades[4] ) (int [][], int, int ) ; //This should not compile

The VC++ 7.1 compiler is considered more compliant to the C++ standard then the GNU 3.2.x compiler.

>>But this is NOT TRUE when u are declaring a pointer to that function. Your function pointer need not mention the size of any of

>>the array dimensions.

Can you reffer to the part of the standard that specifically states that this is allowed?

Please copy and paste the code, so we can be sure we're talking about the same thing.

int main(int, char**)

{

void ( * processGrades[4] ) (int grades[][], int, int ) = {minimum, maximum, printArray} ;

return 0;

}

The above code does not compile on any C++ compliant compiler that I'm aware of.

I just tried to compile it on VC++ 6.0 and, it fails.

I also used Dev-C++ which uses the same compiler that mingw32 uses, and it also fails to compile.

I don't think you're testing the exact same code.

I'm not implying that VC++ 6.0 is a compliant C++ compiler.

On the other hand, VC++ 7.1 is considered to be a very good C++ compliant compilers.

But I do not have access to VC++ 7.0 compiler.

We can may be try on some UNIX based compiler, but it works with GNU compiler

-- Amit

Please post the EXACT code you're using for testing, so we can be sure we're talking about the same thing.

7.0 is also not a very good C++ compliant compiler. It's better then 6.0, but it still has too many compliance issues.

With 7.1 Microsoft finally got it right.

I am testing exactly the same code what u tried.

Only thing is that I corrected the signature for average in the questioner's code.

--Amit

#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);

void average (int [] [exams], 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, students, 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;

}

void average (int setOfGrades[], int tests)

{

int total = 0;

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

total += setOfGrades[i];

cout << static_cast<double>(total)

}

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];

}

}

--efn

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

One of the integer parameters can tell the function how many tests there are. The other one can tell the function which student to use, rather than how many students there are as in the other functions. If you make this design change, you will also have to change the call to the average function correspondingly.

--efn

#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);

void average (int [] [exams], 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, students, 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;

}

void average (int [] [exams], int, int)

{

int total = 0;

int tests;

int setOfGrades;

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

total += setOfGrades[i];

cout << static_cast<double>(total)

}

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];

}

}

Please give details.

Exactly what did you change, and why do you feel something is wrong?

>>Only thing is that I corrected the signature for average in the questioner's code.

Then it's not exactly the same.

You can't be testing the same thing that we're testing, because it fails to compile on VC++ 6.0 and on the GNU compiler.

So you have to be testing some thing different from what we're testing.

Don't be shy.... Post you got.

void average (int setOfGrades[] [exams], int index, int tests)

{

int total = 0;

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

total += setOfGrades[index][i];

cout << static_cast<double>(total)

}

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

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

average (studentGrades, person, exams);}

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; j < tests; ++j)

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

}

}

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

{

cout << fixed << setprecision(2);

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

{

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

int total = 0;

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

total += grades[person][i];

cout << static_cast<double>(total)

}

}

Then you can declare your function pointer via the following method:

void ( * processGrades[4] ) (int [][exams], int, int ) = {printArray, minimum, maximum, average} ;

You can then call it like this:

if (choice == 4) break;

processGrades[choice](stud

const int students = 3;

const int exams = 4;

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

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

void average (int [] [exams], 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}};

void ( * processGrades[4] ) (int [][exams], int, int ) = {printArray, minimum, maximum, average} ;

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;

processGrades[choice](stud

}

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;

}

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

{

cout << fixed << setprecision(2);

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

{

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

int total = 0;

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

total += grades[person][i];

cout << static_cast<double>(total)

}

}

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; j < tests; ++j)

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

}

}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
C++

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

void ( * processGrades[4] ) (int grades[][], int, int ) ;

The above declaration says that procesGrades is an array (of size 4) of pointers to functions which return void and take 3 parameters - a 2-dimensional int array, int, int

Now, before your while loop, let the array locations point to your functions by saying

processGrades [0] = printArray ;

processGrades [1] = minimum;

processGrades [2] = maximum ;

processGrades [3] = average ;

Altenatively, you can also use the following statements to do the same

processGrades [0] = &printArray ;

processGrades [1] = &minimum;

processGrades [2] = &maximum ;

processGrades [3] = &average ;

Now in your while loop, after accepting the user's choice, just call the respctive function in the respcetive array locations as below

( * processGrades[choice] ) (studentGrades, students, exams) ;