-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path54_Student_Record.c
261 lines (244 loc) · 10.3 KB
/
54_Student_Record.c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
Name : Prabhat Kiran
Date : 05th September 2022
Description : WAP to Implement the student record using array of structures.
Sample Input : Enter no.of students : 2
Enter no.of subjects : 2
Enter the name of subject 1 : Maths
Enter the name of subject 2 : Science
----------Enter the student datails-------------
Enter the student Roll no. : 1
Enter the student 2 name : Bindhu
Enter Maths mark : 88
Enter Science mark : 78
Sample Output: ----Display Menu----
1. All student details
2. Particular student details
Enter your choice : 2
----Menu for Particular student----
1. Name.
2. Roll no.
Enter you choice : 1
Enter the name of the student : Nandhu
Roll No. Name Maths Science Average Grade
1 Nandhu 99 91 95 A
Do you want to continue to display(Y/y) : n
*/
#include <stdio.h>
#include <stdlib.h>
struct Student //The Structure to store the various attrributes for the Student.
{
int roll;
char name [50];
int* marks;
float average;
char grade;
};
char grade_calc (float); //The function to calculate the grade received by the student based on the Marks.
void display_all (struct Student [], char* [], int, int); //To display all the details of all the Students present in the Record.
void name_check (struct Student [], char* [], int, int); //To display the details of the particular Student based on the input Name of the Student.
void roll_check (struct Student [], char* [], int, int); //To display the details of the particular Student based on the input Roll no. of the Student.
int main()
{
int stu_count, sub_count;
printf ("Enter the number of Students : ");
scanf ("%d", &stu_count);
printf ("Enter the number of Subjects : ");
scanf ("%d", &sub_count);
getchar (); //To absorb the unwanted '\n' from the Input Buffer.
struct Student s [stu_count]; //The Structure array must have elements equal to the number of Students.
char* sub [sub_count]; //The Array of Strings for Subjects must have elements equal to the number of Subjects.
for (int i = 0; i < sub_count; i++) //Memory allocation for the Subject array of strings.
{
sub [i] = (char*) malloc (50 * sizeof (char));
}
for (int i = 0; i < sub_count; i++) //To read the names of the Subjects.
{
printf ("Enter the name of subject %d: ", (i + 1));
scanf ("%[^\n]", sub [i]);
getchar ();
}
for (int i = 0; i < stu_count; i++) //To read each Student details in the Structure array.
{
printf ("\n----------Enter the student datails-------------\n");
s [i]. marks = (int*) malloc (sub_count * sizeof (int)); //The 'marks' array in the Structure must be allocated space dynamically as per the number of Subjects.
printf ("Enter the student Roll no. : " );
scanf ("%d", &s [i].roll);
getchar ();
printf ("Enter the student %d Name : ", s [i].roll);
scanf ("%[^\n]", s [i].name);
int sum = 0;
for (int j = 0; j < sub_count; j++) //To read the marks obtained by the particular Student in each Subject.
{
printf ("Enter the %s marks : ", sub [j]);
scanf ("%d", &s [i].marks [j]);
sum += s [i].marks [j];
}
s [i].average = (float) sum / (float) sub_count; //To calculate the Average marks obtained by the particular Student.
s [i].grade = grade_calc (s [i].average); //The function call returns the Grade received by the particular Student.
}
char choice; //To store the User's choice of continuing or terminating the Operation.
do
{
char ch;
printf ("\n----------Display Menu----------\n1. All student details\n2. Particular student details\n");
printf ("Enter your choice : ");
scanf ("%hhu", &ch); //To read the choice of printing the details based on the menu displayed.
switch (ch)
{
case 1 :
{
display_all (s, sub, stu_count, sub_count); //To display all the details of all the Students present in the Record.
}
break;
case 2 :
{
printf ("\n------Menu for Particular student------\n1. Name\n2. Roll no.\n");
char c;
printf ("Enter your choice : ");
scanf ("%hhu", &c); //To read the choice of whether to search the details of the Student based on the Name or Roll no.
switch (c)
{
case 1 :
{
name_check (s, sub, stu_count, sub_count); //To search the details of the Student based on the Name of the Student.
}
break;
case 2 :
{
roll_check (s, sub, stu_count, sub_count); //To search the details of the Student based on the Roll no. of the Student.
}
break;
default : printf ("Enter the choice as 1 or 2.\n");
}
}
break;
default : printf ("Enter the choice as 1 or 2.\n");
}
printf ("---------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf ("Do you want to continue to display (Y/y) : ");
scanf (" %c", &choice);
} while ((choice == 'y') || (choice == 'Y'));
for (int i = 0; i < sub_count; i++) //Free the memory allocated to Subject array.
{
free (sub [i]);
}
for (int i = 0; i < stu_count; i++)
{
free (s [i].marks); //Free the memory allocated to Marks array of each Student.
}
return 0;
}
char grade_calc (float m) //To calculate and return the Grade received based on the marks obtained by the Student.
{
if (m < 40.0f)
return 'F';
else if (m < 50.0f)
return 'P';
else if (m < 60.0f)
return 'E';
else if (m < 70.0f)
return 'D';
else if (m < 80.0f)
return 'C';
else if (m < 90.0f)
return 'B';
else
return 'A';
}
void display_all (struct Student s[], char* sub[], int c1, int c2) //To display all the details of the Students stored in the Record.
{
printf ("\nThe details of all the Students present in the Record are:\n"); //To prepare the format in which the details needs to be printed.
printf ("%-20s%-50s", "Roll no.", "Name");
for (int i = 0; i < c2; i++)
{
printf ("%-20s", sub [i]);
}
printf ("%-20s%-20s\n", "Average", "Grade");
for (int i = 0; i < c1; i++)
{
printf ("%-20d", s [i].roll);
printf ("%-50s", s [i].name);
for (int j = 0; j < c2; j++)
{
printf ("%-20d", s [i].marks [j]);
}
printf ("%-20.2f", s [i].average);
printf ("%-20c\n", s [i].grade);
}
return;
}
void roll_check (struct Student s[], char* sub[], int c1, int c2) //To display the details of a particular Student based on the Roll no. read.
{
int r;
char flag = 0; //To store the status of whether the Student details has been found in the Record or not.
printf ("Enter the Roll no. of the Student: ");
scanf ("%d", &r);
for (int i = 0; i < c1; i++)
{
if (s [i].roll == r) //If the Roll no. read matches with the Roll no. of the Student in the Record, print the details in a particular format.
{
printf ("%-20s%-50s", "Roll no.", "Name");
for (int i = 0; i < c2; i++)
{
printf ("%-20s", sub [i]);
}
printf ("%-20s%-20s\n", "Average", "Grade");
printf ("%-20d", s [i].roll);
printf ("%-50s", s [i].name);
for (int j = 0; j < c2; j++)
{
printf ("%-20d", s [i].marks [j]);
}
printf ("%-20.2f", s [i].average);
printf ("%-20c\n", s [i].grade);
flag = 1; //Operation successful.
break;
}
}
if (flag == 0) //If the Operation fails, print an error message.
{
printf ("No Student record found with the Roll number: %d\n", r);
}
return;
}
void name_check (struct Student s[], char* sub[], int c1, int c2) //To display the details of a particular Student based on the Roll no. read.
{
char n [50];
char flag = 0; //To store the status of whether the Student details has been found in the Record or not.
printf ("Enter the Name of the Student: ");
getchar ();
scanf ("%[^\n]", n);
for (int i = 0; i < c1; i++)
{
int j = 0;
while ((s [i].name [j] == n [j]) && (s [i].name [j] != '\0')) //If the Name read matches with the Name of the Student in the Record, print the details in a particular format.
{
j++;
}
if (s [i].name [j] == n [j])
{
printf ("%-20s%-50s", "Roll no.", "Name");
for (int i = 0; i < c2; i++)
{
printf ("%-20s", sub [i]);
}
printf ("%-20s%-20s\n", "Average", "Grade");
printf ("%-20d", s [i].roll);
printf ("%-50s", s [i].name);
for (int j = 0; j < c2; j++)
{
printf ("%-20d", s [i].marks [j]);
}
printf ("%-20.2f", s [i].average);
printf ("%-20c\n", s [i].grade);
flag = 1; //Operation successful.
break;
}
}
if (flag == 0) //If the Operation fails, print an error message.
{
printf ("No Student record found with the Name: %s\n", n);
}
return;
}