when i am running the below code to find max literacy rate and max income, the program is taking inputs properly but finally when displaying output for last two printf statements, i am getting the following error "segmentation fault.core dumped." Please explain what is wrong..Thanks in advance.
#include<stdio.h>
#include<stdlib.h>
#define MAX 50
struct state_det {
char name[50];
long population;
float literacy;
long income;
}s[MAX];
int main()
{
int n,i,max_lit = 0, max_income = 0;
int t, p;
printf("enter number of states\n");
scanf("%d",&n);
for(i = 0; i < n; i++)
{
printf("enter the name of the state %d\n",i);
scanf("%s",&s[i].name);
printf("enter the population of the state %d\n",i);
scanf("%ld",&s[i].population);
printf("enter the literacy rate of the state %d\n",i);
scanf("%f",&s[i].literacy);
printf("enter the average income of the state %d\n",i);
scanf("%ld",&s[i].income);
}
max_lit = s[0].literacy;
max_income = s[0].income;
for(i = 1; i < n; i++)
{
if(max_lit < s[i].literacy) {
max_lit = s[i].literacy;
t = i;
}
if(max_income < s[i].income) {
max_income = s[i].income;
p = i;
}
}
printf("\nthe state with highest literacy is %s and rate = %f\n",s[t].name, s[t].literacy);
printf("\nthe state with highest income is %s and rate = %ld\n",s[p].name, s[p].income);
return 0;
}