Write a program in C : (Arrays of Pointers to Functions) Rewrite the program of
ID: 3585252 • Letter: W
Question
Write a program in C :
(Arrays of Pointers to Functions) Rewrite the program of Fig. 6.22 to use a menu-driven
interface. The program should offer the user four options as follows:
One restriction on using arrays of pointers to functions is that all the pointers must have the same
type. The pointers must be to functions of the same return type that receive arguments of the same
type. For this reason, the functions in Fig. 6.22 must be modified so that they each return the same
type and take the same parameters. Modify functions minimum and maximum to print the minimum
or maximum value and return nothing. For option 3, modify function average of Fig. 6.22 to output
the average for each student (not a specific student). Function average should return nothing
and take the same parameters as printArray, minimum and maximum. Store the pointers to the four
functions in array processGrades and use the choice made by the user as the subscript into the
array for calling each function.
Fig. 6.22: fig06_22.c
// Two-dimensional array manipulations.
#include <stdio.h>
#define STUDENTS 3
#define EXAMS 4
// function prototypes
int minimum(const int grades[][EXAMS], size_t pupils, size_t tests);
int maximum(const int grades[][EXAMS], size_t pupils, size_t tests);
double average(const int setOfGrades[], size_t tests);
void printArray(const int grades[][EXAMS], size_t pupils, size_t tests);
// function main begins program execution
int main(void)
{
// initialize student grades for three students (rows)
int studentGrades[STUDENTS][EXAMS] =
{ { 77, 68, 86, 73 },
{ 96, 87, 89, 78 },
{ 70, 90, 86, 81 } };
// output array studentGrades
puts("The array is:");
printArray(studentGrades, STUDENTS, EXAMS);
// determine smallest and largest grade values
printf(" Lowest grade: %d Highest grade: %d ",
minimum(studentGrades, STUDENTS, EXAMS),
maximum(studentGrades, STUDENTS, EXAMS));
// calculate average grade for each student
for (size_t student = 0; student < STUDENTS; ++student) {
printf("The average grade for student %u is %.2f ",
student, average(studentGrades[student], EXAMS));
}
}
// Find the minimum grade
int minimum(const int grades[][EXAMS], size_t pupils, size_t tests)
{
int lowGrade = 100; // initialize to highest possible grade
// loop through rows of grades
for (size_t i = 0; i < pupils; ++i) {
// loop through columns of grades
for (size_t j = 0; j < tests; ++j) {
if (grades[i][j] < lowGrade) {
lowGrade = grades[i][j];
}
}
}
return lowGrade; // return minimum grade
}
// Find the maximum grade
int maximum(const int grades[][EXAMS], size_t pupils, size_t tests)
{
int highGrade = 0; // initialize to lowest possible grade
// loop through rows of grades
for (size_t i = 0; i < pupils; ++i) {
// loop through columns of grades
for (size_t j = 0; j < tests; ++j) {
if (grades[i][j] > highGrade) {
highGrade = grades[i][j];
}
}
}
return highGrade; // return maximum grade
}
// Determine the average grade for a particular student
double average(const int setOfGrades[], size_t tests)
{
int total = 0; // sum of test grades
// total all grades for one student
for (size_t i = 0; i < tests; ++i) {
total += setOfGrades[i];
}
return (double) total / tests; // average
}
// Print the array
void printArray(const int grades[][EXAMS], size_t pupils, size_t tests)
{
// output column heads
printf("%s", " [0] [1] [2] [3]");
// output grades in tabular format
for (size_t i = 0; i < pupils; ++i) {
// output label for row
printf(" studentGrades[%u] ", i);
// output grades for one student
for (size_t j = 0; j < tests; ++j) {
printf("%-5d", grades[i][j]);
}
}
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <limits.h> //for INT_MIN/MAX use
#define STUDENTS 3
#define EXAMS 4
//function prototypes
void printMenu(void);
void printArray(const int (*grades)[][EXAMS], size_t pupils, size_t tests);
void minimum(const int (*grades)[][EXAMS], size_t pupils, size_t tests);
void maximum(const int (*grades)[][EXAMS], size_t pupils, size_t tests);
int main() {
//init grade array
int studentGrades[STUDENTS][EXAMS] =
{ { 77, 68, 86, 73 },
{ 96, 87, 89, 78 },
{ 70, 90, 86, 81 } };
//init pointer array
int *gradePtrs[STUDENTS][EXAMS];
//set grades to pointer array
for (size_t i = 0; i < STUDENTS; ++i) {
for (size_t j = 0; j < EXAMS; ++j) {
gradePtrs[i][j] = &studentGrades[i][j];
}
}
//main program engine
while(1) {
//man menu
printMenu();
int input;
scanf("%d", &input);
switch (input) {
case 0:
printArray(&studentGrades, STUDENTS, EXAMS);
break;
case 1:
minimum(&studentGrades, STUDENTS, EXAMS);
break;
case 2:
maximum(&studentGrades, STUDENTS, EXAMS);
case 3:
//averageGrage();
break;
case 4:
printf(" Thanks for using the grading program! ");
exit(0);
break;
default:
printf(" Input not recognized.");
break;
}
}
}
//print the main menu
void printMenu(void) {
printf("Enter a choice: ");
printf(" 0 Print the array of grades ");
printf(" 1 Find the miniumum grade ");
printf(" 2 Find the maximum grade ");
printf(" 3 Print the average on all text for each ");
printf(" 4 End Program ");
}
void printArray(const int (*gPtrs)[][EXAMS], size_t pupils, size_t tests) {
for (int i = 0; i < pupils; ++i) {
printf(" [");
for (int j = 0; j < tests; ++j) {
printf("%3d", (*gPtrs)[i][j]);
//if not last element in row, print comma
if (j+1 != tests) {
printf(",");
}
}
printf("] ");
}
puts("");
}
void minimum(const int (*gPtrs)[][EXAMS], size_t pupils, size_t tests) {
//init minimum to largest possible value
int min = INT_MAX;
//if array element is smaller, save
for (int i = 0; i < pupils; ++i) {
for (int j = 0; j < tests; ++j) {
if ((*gPtrs)[i][j] < min) {
min = (*gPtrs)[i][j];
}
}
}
printf(" %s%d ", "The lowest grade found is: ", min);
}
void maximum(const int (*gPtrs)[][EXAMS], size_t pupils, size_t tests) {
//init maximum to smallest possible value
int max = INT_MIN;
//if array element is greater, save
for (int i = 0; i < pupils; ++i) {
for (int j = 0; j < tests; ++j) {
if ((*gPtrs)[i][j] > max) {
max = (*gPtrs)[i][j];
}
}
}
printf(" %s%d ", "The highest grade found is: ", max);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.