Q. Write a C program to sort the records of n students on the basis of their height (descending order) if age of two or more employees are same then sort them on the basis of their weight (descanding order). Use the array of the following structure to have the records of students struct student { int rollno; float height,weight; char name[50]; };

#include<stdio.h>

struct student
{
    int rollno;
    char name[50];
    float height,weight;

};

main()
{
    struct student s[100],temp;
    int n,i,j;
    printf("\nEnter number of students=");
    scanf("%d",&n);
    printf("\nEnter details of the students\n");
    for(i=0;i<n;i++)
    {
        printf("\nEnter roll number of student %d=",i+1);
        scanf("%d",&s[i].rollno);
        printf("\nEnter name of student %d=",i+1);
        scanf("%s",s[i].name);
        printf("\nEnter height of student %d=",i+1);
        scanf("%f",&s[i].height);
        printf("\nEnter weight of student %d=",i+1);
        scanf("%f",&s[i].weight);
    }

    printf("\n\nDetails of the students before sorting\n");
    printf("\n\nRoll number\tName\t\tHeight\t\tWeight\n\n");
    for(i=0;i<n;i++)
    {
        printf("\n%-10d%-20s%10.2f%10.2f\n",s[i].rollno,s[i].name,s[i].height,s[i].weight);
    }

    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(s[i].height<s[j].height)
            {
                temp=s[i];
                s[i]=s[j];
                s[j]=temp;
            }
            else
            {
                if(s[i].height==s[j].height)
                {
                    if(s[i].weight<s[j].weight)
                    {
                        temp=s[i];
                        s[i]=s[j];
                        s[j]=temp;
                    }
                }
            }
        }
    }
    printf("\n\nDetails of the students after sorting\n");
    printf("\n\nRoll number\tName\t\tHeight\t\tWeight\n\n");
    for(i=0;i<n;i++)
    {
        printf("\n%-10d%-20s%10.2f%10.2f\n",s[i].rollno,s[i].name,s[i].height,s[i].weight);
    }
}

No comments:

Post a Comment