-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstruct_prog2.c
61 lines (54 loc) · 1.28 KB
/
struct_prog2.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
/*
Name : Prakhar Sharma
Branch : SE IT
Roll Num : 53
Program : Write a C program to roll_no, name , percentange of 5 students using array of structure and
display the records in descending order of percentange.
*/
#include<stdio.h>
struct Student
{
int roll_no;
char name[50];
float percentage;
};
int main()
{
struct Student s[5];
struct Student temp;
int i, j;
//Taking Data Input from user
for(i = 0; i < 5; i++)
{
printf("Enter Student%d Roll Number : ", i+1);
scanf("%d", &s[i].roll_no);
printf("Enter Student%d Name : ", i+1);
scanf("%s", s[i].name);
printf("Enter Student%d Percentage : ", i+1);
scanf("%f", &s[i].percentage);
}
// Sorting data in decreasing order of percentage
for(i = 0; i < 4; i++)
{
for(j = i+1; j < 5; j++)
{
if(s[j].percentage > s[i].percentage) // sorting condition
{
// swap
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
// displaying sorted data as output
printf("------------------------------------------\n");
for(i = 0; i < 5; i++)
{
printf("Roll Number : %d\n", s[i].roll_no);
printf("Name : %s\n", s[i].name);
printf("Percentage : %f\n", s[i].percentage);
printf("------------------------------------------\n");
}
return 0;
}