Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
cstructCensus
//gcc 7.4.0 //define a structure named census with a character array named city to store name of the citied,a long integer member named popl to store population of the city and a float member named lit to store literacy rate; //then write a program to read details for 5 cities,sort the list alphabetically,sort the list based on population, sort the list based on literacy level, display sorted list //this code is created by Rezaul Hoque on August 13,2021;contact: jewelmrh@yahoo.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; #include <stdio.h> #include <stdlib.h> #include <string.h> const int n=5; struct census{ char city[30]; long int popl; float lit; }; void sortNam(struct census []); void sortPop(struct census []); void sortLit(struct census []); void display(struct census []); void display(struct census cen[5]){ printf("City\tPopulation\tLiteracy:\n"); for(int k=0;k<n;k++) printf("%5s\t%5ld\t%10.2f\n",cen[k].city,cen[k].popl,cen[k].lit); } void sortNam(struct census cen[5]){ char temp[30]; for(int l=1;l<=n;l++) { for(int j=1;j<=n-l;j++) { if(strcmp(cen[j-1].city,cen[j].city)>0){ strcpy(temp,cen[j-1].city); strcpy(cen[j-1].city,cen[j].city); strcpy(cen[j].city,temp); long int temp1=cen[j-1].popl; cen[j-1].popl= cen[j].popl; cen[j].popl = temp1; int temp2 =cen[j-1].lit; cen[j-1].lit= cen[j].lit; cen[j].lit = temp2; } } } printf("After City Sort:\nCity\t Population\t Literacy\n"); for(int k=0;k<n;k++) printf("%5s\t%5ld\t%10.2f\n",cen[k].city,cen[k].popl,cen[k].lit); } void sortPop(struct census cen[5]){ char temp[30]; for(int l=1;l<n;l++) { for(int j=1;j<=n-l;j++) { if(cen[j-1].popl>cen[j].popl) { long int temp1=cen[j-1].popl; cen[j-1].popl= cen[j].popl; cen[j].popl = temp1; int temp2 =cen[j-1].lit; cen[j-1].lit= cen[j].lit; cen[j].lit = temp2; strcpy(temp,cen[j-1].city); strcpy(cen[j-1].city,cen[j].city); strcpy(cen[j].city,temp); } } } printf("After Population Sort:\n"); printf("City\tPopulation\tLiteracy\n"); for(int k=0;k<n;k++){ printf("%5s\t%5ld\t%10.2f\n",cen[k].city,cen[k].popl,cen[k].lit);} } void sortLit(struct census cen[5]){ char temp[30]; for(int l=1;l<n;l++) { for(int j=1;j<=n-l;j++) { if(cen[j-1].lit>cen[j].lit){ strcpy(temp,cen[j-1].city); strcpy(cen[j-1].city,cen[j].city); strcpy(cen[j].city,temp); long int temp1=cen[j-1].popl; cen[j-1].popl= cen[j].popl; cen[j].popl = temp1; int temp2= cen[j-1].lit; cen[j-1].lit= cen[j].lit; cen[j].lit= temp2; } } } printf("After Literacy Sort:\nCity\tPopulation\tLiteracy\n"); for(int k=0;k<n;k++) printf("%5s\t%5ld\t%10.2f\n",cen[k].city,cen[k].popl,cen[k].lit); } int main(){ struct census cen[5]; printf("Enter city name,population and literacy rate:\n"); for(int k=0;k<n;k++){ scanf("%s\t%ld\t%f\n", cen[k].city,&cen[k].popl,&cen[k].lit); } display(cen); sortNam(cen); sortPop(cen); sortLit(cen); return 0; }
run
|
edit
|
history
|
help
0
Assignment 8
RSA Algo
Quadratic Formula
Practice Problem 7
Star 1,3
numeroleidodeordeninverso
Practica 5 ejercicio beneficio empresa
Part 1: Draw Arts
Atleti
MergeSort