-
Notifications
You must be signed in to change notification settings - Fork 0
/
(07.10.23) Practice 7
64 lines (63 loc) · 2.11 KB
/
(07.10.23) Practice 7
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
#include<stdio.h>
#define MAX_NAME_LENGTH 50
#define MAX_STUDENTS 3
typedef enum { A, B, C, D, F
}Score;
typedef struct {
char name[50];
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;
scanf("%s", newStudent.name);
scanf("%d", &newStudent.age);
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); }
}
int findHighestScoringStudent(Student students[], int count) {
if (count == 0) {
return; }
int highestIndex = 0;
for (int i = 1; i < count; i++) {
if (students[i].score > students[highestIndex].score)
highestIndex = i; }
return highestIndex;}
do{int main(){
Student students[MAX_STUDENTS];
int choice;
int count=0;
scanf("%d",&choice);
switch(choice)
{case 1: addStudent(students, count);
count ++;
break;
case 2:
displayStudents(students, count);
break;
case 3: { int highestIndex = findHighestScoringStudent(students, count);}
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);}