Skip to content

Instantly share code, notes, and snippets.

@geekodour
Created February 7, 2016 13:43
Show Gist options
  • Save geekodour/b2fbca7933fd9b933e27 to your computer and use it in GitHub Desktop.
Save geekodour/b2fbca7933fd9b933e27 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
//STRUCTURE GLOBAL DEF
typedef struct{
char name[20];
long int pop;
float lit;
} city;
int count;
//END OF STRUCTURE DEF
//FUNCTION FOR SORTING IN POPULATION
void sortstructpop(city *sample){
int i,j;
long int temp;
for(i=1;i<count;i++){
for(j=0;j<count-i;j++){
if(sample[j].pop<sample[j+1].pop){
temp = sample[j].pop;
sample[j].pop = sample[j+1].pop;
sample[j+1].pop=temp;
}
}
}
}
//FUNCTION FOR SORTING IN LITERACY RATE
void sortstructlit(city *sample){
int i,j;
float temp;
for(i=1;i<count;i++){
for(j=0;j<count-i;j++){
if(sample[j].lit<sample[j+1].lit){
temp = sample[j].lit;
sample[j].lit = sample[j+1].lit;
sample[j+1].lit=temp;
}
}
}
}
//DISPLAY CITY FUNCTION
void displaycity(city *sample){
int i;
for(i=0;i<count;i++){
printf("\n******\n");
printf("City Name: %s\n",sample[i].name);
printf("Population: %ld\n",sample[i].pop);
printf("Literacy Rate: %.2f %% \n",sample[i].lit);
}
}
//MAIN FUNCTION STARTS FROM HERE
main()
{
int i;
printf("Total Number of Cities: ");scanf("%d",&count);
city *arr;
arr = (city *) malloc(sizeof(city)*count);
for(i=0;i<count;i++){
printf("------NEW CITY------\n");
printf("Name of the City: ");scanf("%s",&arr[i].name);
printf("Population: ");scanf("%ld",&arr[i].pop);
printf("Literacy rate: ");scanf("%f",&arr[i].lit);
}
printf("\n\nCITY DATA ENTRY COMPLETE!\nSELECTION MENU:\n ");
printf("*********************\n");
printf("1. Display According to Literacy Rate.\n2. Display According to Population.\n3.Details of a City.\n");
int choice;scanf("%d",&choice);
switch(choice){
case 1: sortstructlit(&arr[0]);
printf("Displaying According to Literacy Rate: \n");
displaycity(&arr[0]);
break;
case 2: sortstructpop(&arr[0]);
printf("Displaying According to population: \n");
displaycity(&arr[0]);
break;
case 3: displaycity(&arr[0]);
break;
}
/* for(i=0;i<count;i++){
printf("------NEW CITY------\n");
printf("City Name: %s\n",arr[i].name);
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment