programming-examples/c/Arrays/To sort array of Structure.c

52 lines
1.4 KiB
C
Raw Normal View History

2019-11-15 12:59:38 +01:00
#include<stdio.h>
#define M 50
struct state {
char name[50];
long int population;
float literacyRate;
float income;
} st[M]; /* array of structure */
int main() {
int i, n, ml, mi, maximumLiteracyRate, maximumIncome;
float rate;
ml = mi = -1;
maximumLiteracyRate = maximumIncome = 0;
printf("Enter how many states:");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter state %d details :", i);
printf("\nEnter state name : ");
scanf("%s", &st[i].name);
printf("\nEnter total population : ");
scanf("%ld", &st[i].population);
printf("\nEnter total literary rate : ");
scanf("%f", &rate);
st[i].literacyRate = rate;
printf("\nEnter total income : ");
scanf("%f", &st[i].income);
}
for (i = 0; i < n; i++) {
if (st[i].literacyRate >= maximumLiteracyRate) {
maximumLiteracyRate = st[i].literacyRate;
ml++;
}
if (st[i].income > maximumIncome) {
maximumIncome = st[i].income;
mi++;
}
}
printf("\nState with highest literary rate :%s", st[ml].name);
printf("\nState with highest income :%s", st[mi].name);
return (0);
}