This is in C language, I'm trying to sort the data from the 2015 population in the text file in ascending order using a function and pointers to the array that I stored the state in. I want to try use a swapping algorithm within this function.
How do I sort the data using my ascensionOrder function from the 2015 population then output it in acending order?
Here is my code: `
#include <stdio.h>
#define FILENAME "PopulationData.txt"
int main(void)
{
void acensionOrder(int *populationData);
void replace(char*s, char a, char b);
char header[3][30];
char state[51][32];
int census[51][2];
FILE *myfile;
myfile = fopen(FILENAME,"r");
int x;
if (myfile== NULL)
{
printf("Errror opening file. \n");
}
else
{
fscanf(myfile, "%s%s%s", header[0], header[1], header[2]);
for (x = 0; x < 51; x++)
{
//fscanf(myfile, "%31s%d%d", state[x], &census[x][0], &census[x][1]); //testing
fscanf(myfile, "%*2c%s %d %d", state[x], &census[x][0], &census[x][1]); //Stores the text in the data file into array
replace(state[x], '_', ' '); //Replaces the lines
replace(state[x], '.', ' '); //Replaces the periods
//printf("%s\t%d\t%d\n", state[x], census[x][0], census[x][1]);
//printf("%2d %31s %10d %10d\n", x, state[x], census[x][0], census[x][1]); //Testing of sort
}
}
acensionOrder(&census[x][1]);
fclose(myfile);
//getchar();
//getchar();
return 0;
}
void acensionOrder(int *populationData) //This function sorts the 2015 data into ascending order
{
int j,k,m;
int sorted2015;
for(k=0; k<m; k++)
{
//m=k;
for(j=0; j<(m-1); j++)
{
if(*(populationData+j)<*(populationData+m))
//m=j;
sorted2015=*(populationData+m);
*(populationData+m)=*(populationData+k);
*(populationData+k)=sorted2015;
}
}
printf("%d\n", sorted2015);
}
void replace(char*s, char a, char b) //This function uses pointers to find characters
{
for(;*s; s++)
{
if(*s==a)*s = b;
}
}
`
Here is the text file the program reads from: Text file for US population
qsortbefore? I also suggest storing your data in some sort ofstruct.mis not initialized inint j,k,m; ...for(k=0; k<m; k++)