I have to use quicksort to sort the states by percentage of population whose ages are equal or greater than 65 years old. I can't figure out on how to use it in the function.
I cannot seem to change the compare function. I also have to make a new file to called output file. I used selection sort to sort the file.
struct state {
char state_name[150];
int population_2020;
int population_2010;
double ages_under_5;
double ages_under_18;
double ages_65_or_greater;
};
void sort_states(struct state list[], int n)
{
int i, j, p;
struct state temp;
for (i = 0; i < n; i++) {
p = i;
for (j = i + 1; j < n; j++) {
if (list[p].ages_65_or_greater < list[j].ages_65_or_greater) {
p = j;
}
}
temp = list[p];
list[p] = list[i];
list[i] = temp;
}
}
int main()
{
char input_file_open[100]; /* initializing the variables */
char output_file_name[110];
printf("Enter the file name: \n");
scanf("%s", input_file_open);
FILE *input_file = fopen(input_file_open, "r");
FILE *output_file;
struct state list[100];
int i, n = 0;
do {
fscanf(input_file, "%[^,], %d, %d, %lf, %lf, %lf\n",
list[n].state_name, &list[n].population_2020,
&list[n].population_2010, &list[n].ages_under_5,
&list[n].ages_under_18, &list[n].ages_65_or_greater);
n++;
} while (!feof(input_file));
fclose(input_file);
for (i = 0; i < n; i++) {
qsort(input_file, n, sizeof(int), sort_states);
}
return 0;
}