Friday 3 November 2017

CO4 Assignment 5th Question solution


Define a structure named census with the following three members:
A character array city[] to store names
A long integer to store population of the city
A float member to store the literacy level.
Write a program to read details of 5 cities randomly and sort the list based on literacy level.

#include <stdio.h>
struct census
{
          char city[50];
          long int population;
          float li;
};

main()
{
          struct census temp,c[5];
          int i,p,j;
          for(i=0;i<5;i++)
          {
                    printf("\nenter city name, population and literacy level");
                    scanf("%s%ld%f",&c[i].city,&c[i].population,&c[i].li);    
          }
          for(p=0;p<4;p++)
          {
                    for(j=0;j<5-p-1;j++)
                    {
                              if(c[j].li >c[j+1].li)
                              {
                                        temp=c[j];
                                        c[j]=c[j+1];
                                        c[j+1]=temp;
                              }
                    }
          }
          printf("\nsorted order are\n");
          for(i=0;i<5;i++)
          {
                    printf("%s\t%ld\t%f\n",c[i].city,c[i].population,c[i].li);     
          }
}

No comments:

Post a Comment