-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructs-sort.c
85 lines (80 loc) · 1.68 KB
/
Structs-sort.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
//Sorting of Marks of students using structs
#include <stdio.h>
#include <stdlib.h>
struct student
{
int roll_no;
char name[30];
float marks;
};
void ascending_sort(struct student s[],int n)
{
struct student temp;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(s[i].marks > s[j].marks)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
void descending_sort(struct student s[],int n)
{
struct student temp;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(s[i].marks < s[j].marks)
{
temp = s[i];
s[i] = s[j] ;
s[j] = temp;
}
}
}
}
int main()
{
int n;
printf("Enter the number of students: ");
scanf("%d",&n);
struct student s[n];
for(int i=0;i<n;i++)
{
printf("Student %d details:\n",i+1);
printf("Enter name of the student: ");
scanf("%s",s[i].name);
printf("Enter roll no of student: ");
scanf("%d",&s[i].roll_no);
printf("Enter marks of student: ");
scanf("%f",&s[i].marks);
}
printf("\n");
int choice;
printf("Do you want to 1)sort in ascending order or 2)sort in descending order: ");
scanf("%d",&choice);
switch (choice)
{
case 1:
ascending_sort(s,n);
for(int i=0;i<n;i++)
{
printf("Name: %s,\n Roll No: %d,\n Marks: %f\n",s[i].name,s[i].roll_no,s[i].marks);
}
break;
case 2:
descending_sort(s,n);
for(int i=0;i<n;i++)
{
printf("Name: %s,\n Roll No: %d,\n Marks: %f\n",s[i].name,s[i].roll_no,s[i].marks);
}
break;
}
return 0;
}