Question: #include #include #include #define MAXR 1000 #define MAXC 30 #define MAXR 1000 #define MAXC 30 // Function to read file contents and stores it in
#include
#include
#include
#define MAXR 1000
#define MAXC 30
#define MAXR 1000
#define MAXC 30
// Function to read file contents and stores it in respective arrays
// Returns number of records
int readFile(char citycode[MAXC][MAXR],char cityName[MAXR][MAXC],int order[MAXR], int population[MAXR], float latitude[MAXR], float longitude[MAXR])
{
// File pointer declared
FILE *fp;
// Record counter
int len = 0;
// To store highest player score name
// Opens the file for reading
fp = fopen("worldcities.txt", "r");
// Checks if the file cannot be opened display error message and stop
if(!fp)
{
printf("ERROR: The file cannot be opened");
exit(0);
}// End of if condition
// Loops till end of the file
while(!feof(fp))
{
// Reads data and stores in len index position of respective arrays
fscanf(fp, "%[^\\,.],%[^\\,],%d,%d,%f,%f ",&citycode[len],&cityName[len],&order[len], &population[len], &latitude[len], &longitude[len]);
// Increase the record counter by one
len++;
}// End of while loop
// Close the file
fclose(fp);
// Returns number of records read
return len;
}// End of function
void sortCityName(char cityName[MAXR][MAXC], int population[MAXR], float latitude[MAXR],
double longitude[MAXR], int len)
{
// Displays heading
printf(" **************** Sort by City Name **************** ");
// File pointer declared
FILE * fp;
// Opens the file for reading
fp = fopen ("cities_alpha.txt","w");
// Loops variable
int r, c;
// Temporary variables to swap
char tempC[MAXC];
int tempP;
double tempL;
// Loops till number of records
for(r = 0; r < len; r++)
{
// Loops till number of records minus outer loop variable value minus one
// Because after each iteration of inner loop one record is sorted
for(c = 0; c < len - r - 1; c++)
{
// Checks if current index position city name is greater than the next
// index position record city name
// Then swap
if(strcmp(cityName[c], cityName[c + 1]) > 0)
{
// Swaps city name
strcpy(tempC, cityName[c]);
strcpy(cityName[c], cityName[c + 1]);
strcpy(cityName[c + 1], tempC);
// Swaps city population
tempP = population[c];
population[c] = population[c + 1];
population[c + 1] = tempP;
// Swaps city latitude
tempL = latitude[c];
latitude[c] = latitude[c + 1];
latitude[c + 1] = tempL;
// Swaps city longitude
tempL = longitude[c];
longitude[c] = longitude[c + 1];
longitude[c + 1] = tempL;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
// Loops till number of records
for(c = 0; c < len; c++)
{
// Writes each record data to file
fprintf (fp,"%s, %d, %d, %d", cityName[c], population[c], latitude[c], longitude[c]);
// Displays each record
printf("%s, %d, %d, %d ", cityName[c], population[c], latitude[c], longitude[c]);
}// End of for loop
// Close the file
fclose(fp);
}// End of function
void sortCityPopulation(char cityName[MAXR][MAXC], int population[MAXR], float latitude[MAXR],
float longitude[MAXR], int len)
{
// Displays heading
printf(" **************** Sort by City Population **************** ");
// File pointer declared
FILE * fp;
// Opens the file for reading
fp = fopen ("cities_population.txt","w");
// Loops variable
int r, c;
// Temporary variables to swap
char tempC[MAXC];
int tempP;
float tempL;
// Loops till number of records
for(r = 0; r < len; r++)
{
// Loops till number of records minus outer loop variable value minus one
// Because after each iteration of inner loop one record is sorted
for(c = 0; c < len - r - 1; c++)
{
// Checks if current index position population is greater than the next
// index position record population
// Then swap
if(population[c] > population[c + 1])
{
// Swaps city name
strcpy(tempC, cityName[c]);
strcpy(cityName[c], cityName[c + 1]);
strcpy(cityName[c + 1], tempC);
// Swaps city population
tempP = population[c];
population[c] = population[c + 1];
population[c + 1] = tempP;
// Swaps city latitude
tempL = latitude[c];
latitude[c] = latitude[c + 1];
latitude[c + 1] = tempL;
// Swaps city longitude
tempL = longitude[c];
longitude[c] = longitude[c + 1];
longitude[c + 1] = tempL;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
// Loops till number of records
for(c = 0; c < len; c++)
{
// Writes each record data to file
fprintf (fp, "%s, %d, %f, %f", cityName[c], population[c], latitude[c], longitude[c]);
// Displays each record
printf("%s, %d, %f, %f ", cityName[c], population[c], latitude[c], longitude[c]);
}// End of for loop
// Close the file
fclose(fp);
}// End of function
void sortCityLatitude(char cityName[MAXR][MAXC], int population[MAXR], double latitude[MAXR],
double longitude[MAXR], int len)
{
// Displays heading
printf("**************** Sort by City Latitude **************** ");
// File pointer declared
FILE * fp;
// Opens the file for writing
fp = fopen ("cities_latitude.txt","w");
// Loops variable
int r, c;
// Temporary variables to swap
char tempC[MAXC];
int tempP;
float tempL;
// Loops till number of records
for(r = 0; r < len; r++)
{
// Loops till number of records minus outer loop variable value minus one
// Because after each iteration of inner loop one record is sorted
for(c = 0; c < len - r - 1; c++)
{
// Checks if current index position latitude is greater than the
// next index position record latitude
// Then swap
if(latitude[c] > latitude[c + 1])
{
// Swaps city name
strcpy(tempC, cityName[c]);
strcpy(cityName[c], cityName[c + 1]);
strcpy(cityName[c + 1], tempC);
// Swaps city population
tempP = population[c];
population[c] = population[c + 1];
population[c + 1] = tempP;
// Swaps city latitude
tempL = latitude[c];
latitude[c] = latitude[c + 1];
latitude[c + 1] = tempL;
// Swaps city longitude
tempL = longitude[c];
longitude[c] = longitude[c + 1];
longitude[c + 1] = tempL;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
// Loops till number of records
for(c = 0; c < len; c++)
{
// Writes each record data to file
fprintf (fp,"%s, %d, %f, %f ", cityName[c], population[c], latitude[c], longitude[c]);
// Displays each record
printf("%s, %d, %f, %f ", cityName[c], population[c], latitude[c], longitude[c]);
}// End of for loop
// Close the file
fclose(fp);
}// End of function
// main function definition
int main()
{
// Character matrix to store city name
char cityName[MAXR][MAXC];
// Integer array to store population
int population[MAXR];
// Float array to store latitude
double latitude[MAXR];
// Float array to store longitude
double longitude[MAXR];
// Call the function to read file contents and store it in respective array
char citycode[MAXR][MAXC];
// Stores the return value as number of records
int order[MAXC];
int len = readFile(citycode,cityName,order, population, latitude, longitude);
// Calls the function to sort the records based on city name
sortCityName(cityName, population, latitude, longitude, len);
// Calls the function to sort the records based on population
sortCityPopulation(cityName, population, latitude, longitude, len);
// Calls the function to sort the records based on latitude
sortCityLatitude(cityName, population, latitude, longitude, len);
}// End of main function
Please help me to update this c program using 1D array
int readFile(char citycode[MAXC][MAXR],char cityName[MAXR][MAXC],int order[MAXR], int population[MAXR], float latitude[MAXR], float longitude[MAXR])
Just my main aim is i to read world_cities.txt
ad,Andorra La Vella,07,20430,42.5,1.5166667 ad,Canillo,02,3292,42.5666667,1.6 ad,Encamp,03,11224,42.5333333,1.5833333 ad,La Massana,04,7211,42.55,1.5166667 ad,Les Escaldes,08,15854,42.5,1.5333333 ad,Ordino,05,2553,42.55,1.5333333
.........
The output should be like below
Sort by City Name A,1145,63.966667,10.2 A,1145,63.966667,10.216667 A Coruna,236010,43.366667,-8.383333 A Dos Cunhados,6594,39.15,-9.3 Aabenraa,16344,55.033333,9.433333 Aabybro,4849,57.15,9.75 Aachen,251104,50.770833,6.105278 Aadorf,7100,47.483333,8.9 Aakirkeby,2195,55.066667,14.933333 Aakre,295,58.1013889,26.1944444 ... Zychlin,8844,52.25,19.616667 Zykovo,1059,54.0666667,45.1 Zykovo,5365,55.9519444,93.1461111 Zyrardow,41179,52.066667,20.433333 Zyryanka,3627,65.75,150.85 Zyryanovsk,44939,49.738611,84.271944 Zyryanovskiy,896,57.7333333,61.7 Zyryanskoye,6285,56.8333333,86.6222222 Zyukayka,4556,58.2025,54.7002778 Zyuzelskiy,1311,56.4852778,60.1316667
Sort by City Population
Tokyo,31480498,35.685,139.7513889 Shanghai,14608512,31.005,121.4086111 Bombay,12692717,18.975,72.825833 Karachi,11627378,24.866667,67.05 New Delhi,10928270,28.6,77.2 Delhi,10928270,28.666667,77.216667 Manila,10443877,14.604167,120.982222 Moscow,10381288,55.7522222,37.6155556 Seoul,10323448,37.5663889,126.9997222 Sao Paulo,10021437,-23.533333,-46.616667 ... Girsterklaus,12,49.7777778,6.4994444 Vstrechnyy,12,67.95,165.6 Qallimiut,12,60.7,-45.3333333 Skaelingur,11,62.1,-7.0 Ivittuut,11,61.2,-48.1666667 Crendal,10,50.0577778,5.8980556 El Porvenir,10,9.5652778,-78.9533333 Schleif,8,49.9905556,5.8575 Aliskerovo,7,67.7666667,167.5833333 Neriunaq,7,64.4666667,-50.3166667 Tasiusaq,7,73.3666667,-56.05 Timerliit,7,65.8333333,-53.25
Sort by City Latitude
Ushuaia,58045,-54.8,-68.3 Punta Arenas,117432,-53.15,-70.9166667 Rio Gallegos,93234,-51.6333333,-69.2166667 Port-Aux-Francais,45,-49.35,70.2166667 Bluff,1938,-46.6,168.333333 Owaka,395,-46.45,169.666667 Invercargill,47287,-46.4,168.35 Woodlands,285,-46.366667,168.55 Riverton,1651,-46.35,168.016667 Wyndham,586,-46.333333,168.85 Wallacetown,638,-46.333333,168.266667 ... Kullorsuaq,408,74.5833333,-57.2 Savissivik,82,76.0233333,-65.0813889 Moriusaq,21,76.7561111,-69.8863889 Narsaq,1709,77.3025,-68.8425 Qaanaaq,616,77.4894444,-69.3322222 Qeqertat,18,77.5097222,-66.6477778 Siorapaluk,75,77.7952778,-70.7558333 Barentsburg,576,78.0666667,14.2333333 Longyearbyen,1232,78.2166667,15.6333333 Ny-Alesund,40,78.9333333,11.95
Just i want to manplate this but i prefer if the program is 1D array means that i don't need to assign MAXR and MAXC
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
