 # C Programming Code Examples

## C > Beginners Lab Assignments Code Examples

### Program to Sort array of Structure

``` Program to Sort array of Structure 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 Firstly accept total number of states so that we can accept all the details using the for loop. Now we are checking for the maximum value. Inside the for loop we are checking the the literacy value with the maximum value. If current literacy rate is greater than the current maximum value then we are modifying the current maximum value. #include<stdio.h> #define M 50 struct state { char name; long int population; float literacyRate; float income; } st[M]; /* array of structure */ int main() { int j, n, ml, mj, maximumLiteracyRate, maximumIncome; float rate; ml = mj = -1; maximumLiteracyRate = maximumIncome = 0; printf("Enter how many states:"); scanf("%d", &n); for (j = 0; j < n; j++) { printf("\nEnter state %d details :", j); printf("\nEnter state name : "); scanf("%s", &st[j].name); printf("\nEnter total population : "); scanf("%ld", &st[j].population); printf("\nEnter total literary rate : "); scanf("%f", &rate); st[j].literacyRate = rate; printf("\nEnter total income : "); scanf("%f", &st[j].income); } for (j = 0; j < n; j++) { if (st[j].literacyRate >= maximumLiteracyRate) { maximumLiteracyRate = st[j].literacyRate; ml++; } if (st[j].income > maximumIncome) { maximumIncome = st[j].income; mj++; } } printf("\nState with highest literary rate :%s", st[ml].name); printf("\nState with highest income :%s", st[mj].name); return (0); } ``` 