-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCorrected student management
113 lines (97 loc) · 2.82 KB
/
Corrected student management
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
#include <stdio.h>
#define MAX_NAME_LENGTH 50
#define MAX_STUDENTS 3
typedef enum {
A,
B,
C,
D,
F
} Score;
typedef struct {
char name[MAX_NAME_LENGTH];
int age;
Score score;
} Student;
void addStudent(Student students[], int *count) {
if (*count >= MAX_STUDENTS) {
printf("Maximum number of students reached.\n");
return;
}
Student newStudent;
printf("Enter name: ");
scanf("%s", newStudent.name);
printf("Enter age: ");
scanf("%d", &newStudent.age);
printf("Enter score (A=0, B=1, C=2, D=3, F=4): ");
scanf("%d", &newStudent.score);
students[*count] = newStudent;
(*count)++;
printf("Student added successfully.\n");
}
void displayStudents(const Student students[], int count) {
if (count == 0) {
printf("No students to display.\n");
return;
}
for (int i = 0; i < count; i++) {
printf("Student %d:\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
printf("Score: %d\n", students[i].score);
printf("\n");
}
}
int findHighestScoringStudent(const Student students[], int count) {
if (count == 0) {
return -1;
}
int highestIndex = 0;
for (int i = 1; i < count; i++) {
if (students[i].score > students[highestIndex].score)
highestIndex = i;
}
return highestIndex;
}
int main() {
Student students[MAX_STUDENTS];
int choice;
int count = 0;
do {
printf("Menu:\n");
printf("1. Add Student\n");
printf("2. Display Students\n");
printf("3. Find Highest-Scoring Student\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent(students, &count);
break;
case 2:
displayStudents(students, count);
break;
case 3: {
int highestIndex = findHighestScoringStudent(students, count);
if (highestIndex == -1) {
printf("No students to find the highest-scoring student.\n");
} else {
printf("The highest-scoring student is:\n");
printf("Name: %s\n", students[highestIndex].name);
printf("Age: %d\n", students[highestIndex].age);
printf("Score: %d\n", students[highestIndex].score);
}
break;
}
case 4:
printf("Exiting the program. Thank you for using our system!\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
printf("\n");
} while (choice != 4);
return 0;
}