-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraysofPointers.c
More file actions
116 lines (104 loc) · 3.37 KB
/
Copy pathArraysofPointers.c
File metadata and controls
116 lines (104 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Name: Arwin Jasper Johnson
Date: 10/05/2024
Course: CIS 2107 Section 003
Lab 6: Arrays of Pointers to functions
Project: Processing arrays using pointers. Using an arrays of pointers
to point to the specific function.
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void printArray(int students, int exams, int grades[students][exams]);
void minimum(int students, int exams, int grades[students][exams]);
void maximum(int students, int exams, int grades[students][exams]);
void average(int students, int exams, int grades[students][exams]);
int main() {
unsigned int Choice;
int students = 0;
int exams = 0;
void (*processGrades[])(int,int,int (*)[exams]) = {printArray,minimum,maximum,average};
printf("Let's build an 2D array for the exam\n");
printf("Enter the number of students: ");
scanf("%d",&students);
printf("Enter the number of exams each students took: ");
scanf("%d",&exams);
printf("\n");
int grades[students][exams];
for(int i = 0; i < students; i++){
for(int j = 0; j < exams; j++){
printf("Enter for grades for grades[%d][%d]: ",i+1,j+1);
scanf("%d",&grades[i][j]);
}
}
while(Choice != 4){
printf("\nEnter a choice:\n");
printf("\t0 Print the array of grades\n");
printf("\t1 Find the minimum grades\n");
printf("\t2 Find the maximum grades\n");
printf("\t3 Print the average on all tests for each student\n");
printf("\t4 End Program\n");
printf("INPUT: ");
scanf("%u", &Choice);
printf("\n");
if(Choice < 4 && Choice >= 0){
(*processGrades[Choice])(students,exams,grades);
}
else if(Choice == 4){
printf("Thank you!\n");
}
}
}
// Prints the implemented array
void printArray(int students, int exams, int grades[students][exams]) {
printf("Here is the array:\n");
for(int i = 0;i < students; i++){
printf("[");
for(int j = 0;j < exams; j++){
if(j < exams-1){
printf("%d, ",grades[i][j]);
}
else{
printf("%d",grades[i][j]);
}
}
printf("]\n");
}
}
//Finds the minimum value in the array
void minimum(int students, int exams, int grades[students][exams]) {
int min = INT_MAX;
for(int i = 0; i < students; i++){
for(int j = 0; j < exams; j++){
if(grades[i][j] < min){
min = grades[i][j];
}
}
}
printf("The minimum grade of all student is: %d\n",min);
}
//Finds the maximum value in the array
void maximum(int students, int exams, int grades[students][exams]) {
int max = INT_MIN;
for(int i = 0; i < students; i++){
for(int j = 0; j < exams; j++){
if(grades[i][j] > max){
max = grades[i][j];
}
}
}
printf("The maximum grade of all students is: %d\n",max);
}
//Find the average of each student
void average(int students, int exams, int grades[students][exams]) {
double average = 0;
int total_exams = students*exams;
for(int i = 0; i < students; i++){
int sum = 0;
for(int j = 0; j < exams; j++){
sum += grades[i][j];
}
average = (double)sum/exams;
printf("The average of student %d: %.2f\n",i+1,average);
}
}