Question
My code is working but not for sort places by distance. Some place is not showing in my following need. help me fix it. I
My code is working but not for sort places by distance. Some place is not showing in my following need. help me fix it. I need to you not changing any userdifine fuction but only change code in my main function thank you. And the text file is like this
Vancouver International Airport Canada 49.196691 -123.181512 Tucson International Airport USA 32.114510 -110.939227 University of Arizona USA 32.231885 -110.950109 Statue of Liberty USA 40.689249 -74.044500 Big Ben UK 51.500729 -0.124625 Forbidden City China 39.916345 116.397155 Sydney Opera House Australia -33.856784 151.215297 Ministro Pistarini International Airport Argentina -34.815004 -58.534828 Colosseo Italy 41.890210 12.492231 Golden Gate Bridge USA 37.819929 -122.478255 Tucson City Center USA 32.225750 -110.950109
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#define MAX 100
typedef struct place_s
{
char name[100];
char country[30];
double latitude;
double longitude;
double distance;
}place;
double dist(double lat1, double long1, double lat2, double long2)
{
double R = 6371;
double PI = 3.1415926536;
double dx, dy, dz;
double dist_mile;
long1 = long1 - long2;
long1 = long1 * (PI / 180);
lat1 = lat1 * (PI / 180);
lat2 = lat2 * (PI / 180);
dz = sin(lat1) - sin(lat2);
dx = (cos(long1) * cos(lat1)) - cos(lat2);
dy = sin(long1) * cos(lat1);
dist_mile = (asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R) / 1.609344;
return dist_mile;
}
void swap(place *x, place *y)
{
place temp;
temp = *x;
*x = *y;
*y = temp;
}
void selection(place x[], int size)
{
int i, j;
int max;
for (i = 0; i
{
max = i; // start searching currently unsorted
for (j = i; j
{
if (x[j].distance > x[max].distance) // if found a larger element
max = j; // move it to the front
}
swap(&x[i], &x[max]);
}
}
int main()
{ FILE *fp; int i; int iIndex; char temp[100]; double lat1 = 0, long1 = 0; double lat2 = 0, long2 = 0;
place arrplas[MAX];
int iContinue = 1; iIndex = 0; fp = fopen("placelist.txt", "r");
if (fp==NULL)
{
printf("placelist.txt File not found");
return -1;
}
while (iContinue = 1)
{ if (fgets(arrplas[iIndex].name, 100, fp)==NULL)
{
break;
}
//remove newline
arrplas[iIndex].name[strcspn(arrplas[iIndex].name, " ")] = 0;
//read country
if (fgets(arrplas[iIndex].country, 30, fp)==NULL)
{
break;
}
//remove newline
arrplas[iIndex].country[strcspn(arrplas[iIndex].country, " ")] = 0;
//read latitude
memset(temp, 0, sizeof(temp));
if (fgets(temp, 100, fp)==NULL)
{
break;
}
arrplas[iIndex].latitude = atof(temp);
//read longitude
memset(temp, 0, sizeof(temp));
if (fgets(temp, 100, fp)==NULL)
{
break;
}
arrplas[iIndex].longitude = atof(temp);
iIndex++;
}
fclose(fp);
printf(" Places in database NOT in USA: ");
for (i = 0; i
{
if (strcmp("USA", arrplas[i].country)!=0)
{
printf("%s, %s ", arrplas[i].name, arrplas[i].country);
printf("(latitude,longitude) = (%f,%f) ", arrplas[i].latitude, arrplas[i].longitude);
}
}
//find longitud and latitude of TucSon city.
for (i = 0; i
{
if (strcmp("Tucson City Center", arrplas[i].name)!=0)
{
lat1 = arrplas[i].latitude;
long1 = arrplas[i].longitude;
break;
}
}
//find distance from TucSon city.
for (i = 0; i
{
lat2 = arrplas[i].latitude;
long2 = arrplas[i].longitude;
arrplas[i].distance = dist(lat1, long1, lat2, long2);
}
//Display record based on distance from TucSon city.
printf(" Places in database sorted by distance from Tucson city center: ");
selection(arrplas, iIndex);
for (i = 0; i
{
printf("%s, %s ", arrplas[i].name, arrplas[i].country);
printf("(latitude,longitude) = (%f,%f) ", arrplas[i].latitude, arrplas[i].longitude);
printf("Distance is %f miles ", arrplas[i].distance);
}
return 0;
}
Sample Code Execution: Places in Database NOT in USA Vancouver International Airport, Canada (Latitude, Longtitude) = (49.196691,-123.181512) Big Ben, UK (Latitude, Longtitude) = (51.500729,-0.124625) Forbidden City, China (Latitude, Longtitude) = (39.916345, 116.397155) Sydney Opera House, Australia (Latitude, Longtitude) - (-33.856784,151.215297) Ministro Pistarini International Airport, Argentina (Latitude, Longtitude)-(-34.815004,-58.534828) Colosseo, Italy (Latitude, Longtitude) (41.890210, 12.492231) Places in database sorted by distance from Tucson city center (its latitude and longitude are 32.225750 and -110.979413) University of Arizona, USA (Latitude, Longitude) (32.231885, -110.950109) Distance is 1.764 miles Note that the distance is displayed Tucson International Airport, USA (Latitude, Longitude) (32.114510,-110.939227) Distance is 8.037 miles Golden Gate Bridge, USA (Latitude, Longitude) (37.819929, -122.478255) Distance is 755.920 miles Vancouver International Airport, Canada (Latitude, Longitude) - (49.196691,-123.181512) Distance is 1331.545 miles Statue of Liberty, USA (Latitude, Longitude)-(40.689249,-74.044500)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started