sorting in structure with c

Solutions on MaxInterview for sorting in structure with c by the best coders in the world

showing results for - "sorting in structure with c"
Abdullah
30 Sep 2017
1#include <stdio.h>
2
3struct student
4{
5    int rollno;
6    char name[80];
7    int marks;
8};
9
10void accept(struct student list[], int s);
11void display(struct student list[], int s);
12void bsortDesc(struct student list[], int s);
13
14int main()
15{
16    struct student data[20];
17    int n;
18
19    printf("Number of records you want to enter? : ");
20    scanf("%d", &n);
21
22    accept(data, n);
23    printf("\nBefore sorting");
24    display(data, n);
25    bsortDesc(data, n);
26    printf("\nAfter sorting");
27    display(data, n);
28
29    return 0;
30} 
31
32void accept(struct student list[80], int s)
33{
34    int i;
35    for (i = 0; i < s; i++)
36    {
37        printf("\n\nEnter data for Record #%d", i + 1);
38        
39        printf("\nEnter rollno : ");
40        scanf("%d", &list[i].rollno);
41
42        printf("Enter name : ");
43        gets(list[i].name);
44
45        printf("Enter marks : ");
46        scanf("%d", &list[i].marks);
47    } 
48}
49
50void display(struct student list[80], int s)
51{
52    int i;
53    
54    printf("\n\nRollno\tName\tMarks\n");
55    for (i = 0; i < s; i++)
56    {
57        printf("%d\t%s\t%d\n", list[i].rollno, list[i].name, list[i].marks);
58    } 
59}
60
61void bsortDesc(struct student list[80], int s)
62{
63    int i, j;
64    struct student temp;
65    
66    for (i = 0; i < s - 1; i++)
67    {
68        for (j = 0; j < (s - 1-i); j++)
69        {
70            if (list[j].marks < list[j + 1].marks)
71            {
72                temp = list[j];
73                list[j] = list[j + 1];
74                list[j + 1] = temp;
75            } 
76        }
77    }
78}