To sort array of Structure


Levels of difficulty: / perform operation:

Write a C program to accept records of the different states using array of structures. The structure should contain char state, population, literacy rate, and income. Display the state whose literacy rate is highest and whose income is highest.

Problem to display the highest literate rate and the highest income of a state using array of structures

C Programm:

#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);
}

Output:

Enter how many states:3

Enter state 0 details :
Enter state name : Maharashtra
Enter total population : 1000
Enter total literary rate : 90
Enter total income : 10000

Enter state 1 details :
Enter state name : Goa
Enter total population : 300
Enter total literary rate : 94
Enter total income : 5000

Enter state 2 details :
Enter state name : Gujrat
Enter total population : 900
Enter total literary rate : 78
Enter total income : 7000

The state whose literary rate is the highest : Goa
The state whose income is the highest : Maharashtra





Quantitative Aptitude
Reasoning
Programming
Interview