Index « Previous Next »

Question

Define a structure, student, to store the following data about a student: rollno (integer), name (string) and marks(integer) Suppose that the class has 20 students. Use an array of 20 elements of type Student. Your program must contain the following functions:

Source Code

#include <stdio.h>

struct student
{
    int rollno;
    char name[80];
    int marks;
};

void accept(struct student list[], int s);
void display(struct student list[], int s);
void bsortDesc(struct student list[], int s);

int main()
{
    struct student data[20];
    int n;

    printf("Number of records you want to enter? : ");
    scanf("%d", &n);

    accept(data, n);
    printf("\nBefore sorting");
    display(data, n);
    bsortDesc(data, n);
    printf("\nAfter sorting");
    display(data, n);

    return 0;
} 

void accept(struct student list[80], int s)
{
    int i;
    for (i = 0; i < s; i++)
    {
        printf("\n\nEnter data for Record #%d", i + 1);
        
        printf("\nEnter rollno : ");
        scanf("%d", &list[i].rollno);

        printf("Enter name : ");
        gets(list[i].name);

        printf("Enter marks : ");
        scanf("%d", &list[i].marks);
    } 
}

void display(struct student list[80], int s)
{
    int i;
    
    printf("\n\nRollno\tName\tMarks\n");
    for (i = 0; i < s; i++)
    {
        printf("%d\t%s\t%d\n", list[i].rollno, list[i].name, list[i].marks);
    } 
}

void bsortDesc(struct student list[80], int s)
{
    int i, j;
    struct student temp;
    
    for (i = 0; i < s - 1; i++)
    {
        for (j = 0; j < (s - 1-i); j++)
        {
            if (list[j].marks < list[j + 1].marks)
            {
                temp = list[j];
                list[j] = list[j + 1];
                list[j + 1] = temp;
            } 
        }
    }
}

Output

Number of records you want to enter? : 3


Enter data for Record #1
Enter rollno : 100
Enter name : Alex
Enter marks : 75


Enter data for Record #2
Enter rollno : 101
Enter name : Vinay
Enter marks : 94


Enter data for Record #3
Enter rollno : 102
Enter name : Javed
Enter marks : 56

Before sorting

Rollno  Name    Marks
100      Alex      75
101     Vinay     94
102    Javed     56

After sorting

Rollno  Name    Marks
101     Vinay     94
100      Alex      75
102    Javed     56