Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Placelist: Vancouver International Airport Canada 49.196691 -123.181512 Tucson I

ID: 3606615 • Letter: P

Question

Placelist:

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

Problem 2 (35 points): The file placelist.txt contains the place information (name, country, latitude and longitude). Write a C program that a) Reads places from a file called placelist.txt and stores it in an array of type place b) Prints, on screen, places in the database that are NOT in USA. c) Sorts the array of places in distance from Tucso center and print the sorted array on screen. To do so, you will modify the void selection(int x[], int size) function that sorts ints given on the next page. If you hard-code the list of cities sorted by distance in your code, 0 point for this problem. 1) Your program MUST use the following structure: typedef structplaces f char name [100 ]; char country[30]; double latitude; double longitude; bplace; Note: the above structure can be modified (i.e., to include distance) so that it can be used with sorting.

Explanation / Answer


Given below is the code with output. Please do rate the answer if it helped. thank you.



#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
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);
void swap(place *p1, place *p2);
void selection(place p[], int size);
int readFile(char *filename, place p[]);
void showNotInUSA(place p[], int n);
void showSortedFromTucson(place p[], int n);

int main()
{
int n;
place p[100];
  
n = readFile("placelist.txt", p);
showNotInUSA(p, n);
printf(" ============================================ ");
showSortedFromTucson(p, n);
}

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 *p1, place *p2)
{
place temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void selection(place p[], int size)
{
int i, j;
int min;
for(i = 0; i < size; i++)
{
min = i;
for( j = i; j < size; j++){
if(p[j].distance < p[min].distance)
min = j;
}
swap(&p[i], &p[min]);
}
}
int readFile(char *filename, place p[])
{
int n = 0;
FILE *fp = fopen(filename ,"r");
int len ;
if(fp == NULL)
{
printf("Could not open input file %s ", filename);
exit(1);
}
while(fgets(p[n].name, 100, fp))
{
fgets(p[n].country, 30, fp);
fscanf(fp, "%lf %lf", &p[n].latitude, &p[n].longitude);
fgetc(fp); //get rid of newline before reading a string next iteration
//remove the from both country and place names, fgets() returns the also
len = strlen(p[n].name);
p[n].name[len - 1] = '';
len = strlen(p[n].country);
p[n].country[len - 1] = '';
  
  
n++;
}
fclose(fp);
return n;
}
void showNotInUSA(place p[], int n)
{
int i;
printf("Places in database NOT in USA ");
for(i = 0; i < n; i++)
{
if(strcmp(p[i].country, "USA") != 0) //not USA
{
printf("%s, %s ", p[i].name, p[i].country);
printf("[Latitude, Longitude] = [%lf, %lf] ", p[i].latitude, p[i].longitude);
  
}
}
}
void showSortedFromTucson(place p[], int n)
{
double Tucson_lat = 32.225750;
double Tucson_long = -110.979413;
int i ;
  
//calculate distances for all
for(i = 0; i < n; i++)
p[i].distance = dist(p[i].latitude, p[i].longitude, Tucson_lat, Tucson_long);
  
//sort them
selection(p, n);
  
//display
printf("Places in database sorted by distance from Tucson city center ");
printf("(Its latitude and longitude are %lf and %lf) ", Tucson_lat, Tucson_long);
  
for(i = 0; i < n; i++)
{
printf("%s, %s ", p[i].name, p[i].country);
printf("[Latitude, Longitude] = [%lf, %lf] ", p[i].latitude, p[i].longitude);
printf("Distance is %.3lf miles ", p[i].distance);
}
}



output
======
Places in database NOT in USA
Vancouver International Airport, Canada
[Latitude, Longitude] = [49.196691, -123.181512]
Big Ben, UK
[Latitude, Longitude] = [51.500729, -0.124625]
Forbidden City, China
[Latitude, Longitude] = [39.916345, 116.397155]
Sydney Opera House, Australia
[Latitude, Longitude] = [-33.856784, 151.215297]
Ministro Pistarini International Airport, Argentina
[Latitude, Longitude] = [-34.815004, -58.534828]
Colosseo, Italy
[Latitude, Longitude] = [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
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]
Distance is 2117.048 miles
Big Ben, UK
[Latitude, Longitude] = [51.500729, -0.124625]
Distance is 5300.223 miles
Ministro Pistarini International Airport, Argentina
[Latitude, Longitude] = [-34.815004, -58.534828]
Distance is 5746.655 miles
Colosseo, Italy
[Latitude, Longitude] = [41.890210, 12.492231]
Distance is 6183.815 miles
Forbidden City, China
[Latitude, Longitude] = [39.916345, 116.397155]
Distance is 6603.798 miles
Sydney Opera House, Australia
[Latitude, Longitude] = [-33.856784, 151.215297]
Distance is 7815.125 miles