2. (Arrays of Pointers to Functions) You will modify the above program #1- “Stud
ID: 3830134 • Letter: 2
Question
2. (Arrays of Pointers to Functions) You will modify the above program #1- “Student Grade Calculation Program” as follows:
1. You will use arrays of pointers to functions to create a menu-driven interface to call 6 functions: 1) Print the array of grades 2) Find/Print the lowese test scores for all tests 3) Find/Print the highest test scores for all tests 4) Find/Print the average before dropping the lowest for each student 5) Find/Print the new average after dropping the lowest for each student 6) Find/Print the final letter grade using new average for each student (see output screen and the restriction below) 2. You need to write at least two funtions with pointers among above six functions. (for example, void minimum(unsigned int (*grades)[EXAMS], size_t pupils, size_t tests); 3. Write a printMenu function to call “Menu Option screen”. Note: 1) You might want to use an initialized array with data requested to test your program to save time. 2) One restriction on using arrays of pointers to functions is that all the pointers must have the sametype. The pointers must be to functions of the same return type that receive arguments of the sametype. For this reason, the functions in prog.#1 must be modified if they are not so that they return the same type and take the same parameters.
code:
#include <stdio.h>
#define STUDENTS 3
#define SCORE 4
//function prototypes
int minimum(int grades[][SCORE], size_t pupils, size_t tests);
int maximum(int grades[][SCORE], size_t pupils, size_t tests);
double average(const int setOfGrades[], size_t tests);
void printArray(int grades[][SCORE], size_t pupils, size_t tests);
void getGrades(int[][SCORE]);
double newAverage(const int setOfGrades[], int grades[][SCORE], size_t pupils, size_t tests);
//function main begins
int main(void) {
void(*processGrades[1])(int[][SCORE], size_t, size_t) =
{ printArray };
size_t student;
//initialize student grades for three students
int studentGrades[STUDENTS][SCORE] = {0};
getGrades(studentGrades);
//output array studentGrades
puts("The array is:");
printArray(studentGrades, STUDENTS, SCORE);
//determine smallest and largest grade values
printf(" Lowest grade: %d Highest grade: %d ",
minimum(studentGrades, STUDENTS, SCORE),
maximum(studentGrades, STUDENTS, SCORE));
// calculate average grade for each student
for (student = 0; student < STUDENTS; ++student) {
printf(" The average grade for student %u is %.2f ",
student, average(studentGrades[student], SCORE));
} // end for
for (student = 0; student < STUDENTS; ++student ) {
printf(" The new Average for student %u is: %.2f ", student, newAverage);
}
return 0;
} // end main
void getGrades(int studentGrades[][SCORE]) {
int student = 0;
int grade = 0;
int testScore = 0;
while (student != -1) {
printf(" Enter the student, test number and test score ");
printf("or enter -1 for the student to end input for the Student: ");
printf(" StudentNumber: ");
scanf_s("%d", &student);
studentGrades[student - 1][grade - 1] = testScore;
printf(" Test number: ");
scanf_s("%d", &grade);
printf("Student Test Score: ");
scanf_s("%d", &testScore);
}
}
//find minimum grade
int minimum(int grades[][SCORE], size_t pupils, size_t tests) {
size_t i;
size_t j;
int lowGrade = 100;
//loop through rows of grades
for (i = 0; i < pupils; ++i) {
// loop through columns of grades
for (j = 0; j < tests; ++j) {
if (grades[i][j] < lowGrade) {
lowGrade = grades[i][j];
} // end if
} // end inner for
} // end outer for
return lowGrade;
} // end function minimum
// find the maximum grade
int maximum(int grades[][SCORE], size_t pupils, size_t tests) {
size_t i;
size_t j;
int highGrade = 0;
//loop through rows of grades
for (i = 0; i < pupils; ++i) {
// loop through columns of grades
for (j = 0; j < tests; ++j) {
if (grades[i][j] > highGrade) {
highGrade = grades[i][j];
} // end if
} // end inner for
} // end outer for
return highGrade;
} // end function maximum
//determine the average grade for a particular student
double average(const int setOfGrades[], size_t tests) {
size_t i;
int total = 0;
//total all grades for one student
for (i = 0; i < tests; ++i) {
total += setOfGrades[i];
} // end for
return (double)total / tests;
} // end function average
// print the array
void printArray(int grades[][SCORE], size_t pupils, size_t tests) {
size_t i;
size_t j;
//output column heads
printf(" [0] [1] [2] [3]");
//output grades in tabular form
for (i = 0; i < pupils; i++) {
//output label for row
printf(" studentGrades[%d]", i);
//output grades for one student
for (j = 0; j < tests; j++) {
printf("%-5d", grades[i][j]);
} // end inne for
} // end outer for
} // end function printArray
double newAverage(const int setOfGrades[], int grades[][SCORE], size_t pupils, size_t tests) {
size_t i;
size_t j;
float avg2 = 0;
int lowGrade = 100;
//loop through rows of grades
for (i = 0; i < pupils; ++i) {
// loop through columns of grades
for (j = 0; j < tests; ++j) {
if (grades[i][j] < lowGrade) {
lowGrade = grades[i][j];
} // end if
} // end inner for
} // end outer for
for (i = 0; i < tests; ++i) {
avg2 += setOfGrades[i];
avg2 -= lowGrade;
}
return double(avg2) / 3;
}
Explanation / Answer
#include <stdio.h>
#define STUDENTS 3
#define SCORE 4
//function prototypes
int minimum(int grades[][SCORE], size_t pupils, size_t tests);
int maximum(int grades[][SCORE], size_t pupils, size_t tests);
double average(const int setOfGrades[], size_t tests);
void printArray(int grades[][SCORE], size_t pupils, size_t tests);
void getGrades(int[][SCORE]);
double newAverage(const int setOfGrades[], int grades[][SCORE], size_t pupils, size_t tests);
//function main begins
int main(void) {
void(*processGrades[1])(int[][SCORE], size_t, size_t) =
{ printArray };
size_t student;
//initialize student grades for three students
int studentGrades[STUDENTS][SCORE] = {0};
getGrades(studentGrades);
//output array studentGrades
puts("The array is:");
printArray(studentGrades, STUDENTS, SCORE);
//determine smallest and largest grade values
printf(" Lowest grade: %d Highest grade: %d ",
minimum(studentGrades, STUDENTS, SCORE),
maximum(studentGrades, STUDENTS, SCORE));
// calculate average grade for each student
for (student = 0; student < STUDENTS; ++student) {
printf(" The average grade for student %u is %.2f ",
student, average(studentGrades[student], SCORE));
} // end for
for (student = 0; student < STUDENTS; ++student ) {
printf(" The new Average for student %u is: %.2f ", student, newAverage);
}
return 0;
} // end main
void getGrades(int studentGrades[][SCORE]) {
int student = 0;
int grade = 0;
int testScore = 0;
while (student != -1) {
printf(" Enter the student, test number and test score ");
printf("or enter -1 for the student to end input for the Student: ");
printf(" StudentNumber: ");
scanf_s("%d", &student);
studentGrades[student - 1][grade - 1] = testScore;
printf(" Test number: ");
scanf_s("%d", &grade);
printf("Student Test Score: ");
scanf_s("%d", &testScore);
}
}
//find minimum grade
int minimum(int grades[][SCORE], size_t pupils, size_t tests) {
size_t i;
size_t j;
int lowGrade = 100;
//loop through rows of grades
for (i = 0; i < pupils; ++i) {
// loop through columns of grades
for (j = 0; j < tests; ++j) {
if (grades[i][j] < lowGrade) {
lowGrade = grades[i][j];
} // end if
} // end inner for
} // end outer for
return lowGrade;
} // end function minimum
// find the maximum grade
int maximum(int grades[][SCORE], size_t pupils, size_t tests) {
size_t i;
size_t j;
int highGrade = 0;
//loop through rows of grades
for (i = 0; i < pupils; ++i) {
// loop through columns of grades
for (j = 0; j < tests; ++j) {
if (grades[i][j] > highGrade) {
highGrade = grades[i][j];
} // end if
} // end inner for
} // end outer for
return highGrade;
} // end function maximum
//determine the average grade for a particular student
double average(const int setOfGrades[], size_t tests) {
size_t i;
int total = 0;
//total all grades for one student
for (i = 0; i < tests; ++i) {
total += setOfGrades[i];
} // end for
return (double)total / tests;
} // end function average
// print the array
void printArray(int grades[][SCORE], size_t pupils, size_t tests) {
size_t i;
size_t j;
//output column heads
printf(" [0] [1] [2] [3]");
//output grades in tabular form
for (i = 0; i < pupils; i++) {
//output label for row
printf(" studentGrades[%d]", i);
//output grades for one student
for (j = 0; j < tests; j++) {
printf("%-5d", grades[i][j]);
} // end inne for
} // end outer for
} // end function printArray
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.