-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.c
More file actions
101 lines (81 loc) · 2.47 KB
/
Student.c
File metadata and controls
101 lines (81 loc) · 2.47 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
//2
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char name[25];
int reg_no;
float m1,m2,m3;
float avg;
}st;
void read(st*p,int n){
for(int i=0;i<n;i++){
printf("FOR STUDENT-%d\n",i+1,"___________\n");
printf("Enter name: ");
getchar();
scanf("%[^\n]",(p+i)->name);
printf("Enter register number: ");
scanf("%d",&(p+i)->reg_no);
printf("Enter marks in subject 1: ");
scanf("%f",&(p+i)->m1);
printf("Enter marks in subject 2: ");
scanf("%f",&(p+i)->m2);
printf("Enter marks in subject 3: ");
scanf("%f",&(p+i)->m3);
}
}
void display(st*p,int n){
printf("%-15s%-15s%-15s%-15s%-15s%-15s\n","NAME","REGISTER NO.","SUB-1","SUB-2","SUB-3","AVG OF BEST TWO MARKS");
for(int i=0;i<n;i++){
printf("%-15s%-15d%-15.2f%-15.2f%-15.2f%-15.2f\n",(p+i)->name,(p+i)->reg_no,(p+i)->m1,(p+i)->m2,(p+i)->m3,(p+i)->avg);
}
}
void average(st*p,int n){
for(int i=0;i<n;i++){
float min=(p+i)->m1<(p+i)->m2?((p+i)->m1<(p+i)->m3?(p+i)->m1:(p+i)->m3):((p+i)->m2<(p+i)->m3?(p+i)->m2:(p+i)->m3);
(p+i)->avg=(p+i)->m1+(p+i)->m2+(p+i)->m3-min;
(p+i)->avg/=2;
}
printf("Calculation Complete...\n");
}
void sort(st*p,int n){
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1-i;j++){
if((p+j)->reg_no>(p+j+1)->reg_no){
st temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}
}
}
printf("Sorted...\n");
}
void main(){
int n,ch;
printf("Enter the total number of students: ");
scanf("%d",&n);
st*p=(st*)calloc(n,sizeof(st));
printf("ALL MARKS TO BE ENTERED OUT OF 100 ONLY\n");
for(;;){
printf("\nENTER:\n1. To read\n2. To display\n3. To calculate the average of best two marks of each student\n4. To sort students based on reg no\n0.To EXIT\nEnter your choice: ");
scanf("%d",&ch);
switch(ch){
case 1:
read(p,n);
break;
case 2:
display(p,n);
break;
case 3:
average(p,n);
break;
case 4:
sort(p,n);
break;
case 0:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid Choice!Please Enter Again...\n");
}
}
}