Write a menu-driven program, which uses a 3 x 4 array, and offers 4 choices as f
ID: 3910345 • Letter: W
Question
Write a menu-driven program, which uses a 3 x 4 array, and offers 4 choices as follows:
using arrays and pointers to functions.
cout << " Enter a choice: "
<< " 0 Print the array of grades "
<< " 1 Print sum of each row "
<< " 2 Print sum of each column "
<< " 3 Print average of all grades "
<< " 4 End program ? ";
Each choice should represent a function and functions are selected through an array of pointers to functions as shown below:
void ( *processGrades[ 4 ] )( int [][ EXAMS ], int, int )
= { printArray,rowSum,colSum,average };
Declaring grades:
int choice = 0,
studentGrades[ STUDENTS ][ EXAMS ] = { { 77, 68, 86, 73 },
{ 96, 87, 89, 78 },
Making choice: { 70, 90, 86, 81 } };
while ( choice != 4 ) {
do {
printMenu();
cin >> choice;
} while ( choice < 0 || choice > 4 );
if ( choice != 4 )
( *processGrades[ choice ] )( studentGrades, STUDENTS, EXAMS )
else
cout << "Program Ended. ";
Write the above-described program
" Enter aExplanation / Answer
#include<iostream>
using namespace std;
void printMenu()
{
cout << " Enter a choice: "
<< " 0 Print the array of grades "
<< " 1 Print sum of each row "
<< " 2 Print sum of each column "
<< " 3 Print average of all grades "
<< " 4 End program ? "<<" ";
}
void printArray (int a[3][4],int s,int e)
{
int i=0,j=0;
for (i=0;i<s;i++)
{
for (j=0;j<e;j++)
cout<<a[i][j]<<" ";
cout<<" ";
}
}
void rowSum (int a[3][4],int s,int e)
{
int i=0,j=0,sum=0;
for (i=0;i<s;i++)
{ sum=0;
for (j=0;j<e;j++)
sum+=a[i][j];
cout<<i<<"th row sum="<<sum<<" ";
}
}
void colSum (int a[3][4],int s,int e)
{
int i=0,j=0,sum=0;
for (i=0;i<e;i++)
{ sum=0;
for (j=0;j<s;j++)
sum+=a[j][i];
cout<<i<<"th col sum="<<sum<<" ";
}
}
void average (int a[3][4],int s,int e)
{
int i=0,j=0,sum=0;
for (i=0;i<e;i++)
{
for (j=0;j<s;j++)
sum+=a[j][i];
}
cout<<i+1<<"average="<<(float)sum/(s*e)<<" ";
}
int main()
{
int choice,STUDENTS=3,EXAMS=4;
int studentGrades[ 3 ][ 4 ] = { { 77, 68, 86, 73 },
{ 96, 87, 89, 78 },
{ 70, 90, 86, 81 } };
void ( *processGrades[ 4 ] )( int [3][ 4 ], int, int )= { printArray,rowSum,colSum,average };
do
{
printMenu();
cin>>choice;
if ( choice != 4 )
( *processGrades[ choice ] )( studentGrades, STUDENTS, EXAMS );
else
cout << "Program Ended. ";
}
while (choice>=0 && choice<4);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.