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.