Problem 2 (35 points): The file placelist.txt contains the place information (na
ID: 3607351 • Letter: P
Question
Problem 2 (35 points): The file placelist.txt contains the place information (name, country latitude and longitude). Write a C program that a) b) c) Reads places from a file called placelist.txt and stores it in an array of type place Prints, on screen, places in the database that are NOT in USA. and print the sorted Sorts the array of places 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 struct place_s f char name [100]; char country[30]; double latitude; double longitude; place; Note: the above structure can be modified (i.e., to include distance) so that it can be used with sorting. 4 2) The function below should be used to find the distance in miles between 2 locations (latl, longl) and (lat2, long2). Note: (lat, long) (latitude, longitude) 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; long1long1 * (PI / 180); 1at1 lat 1 * (PI / 180); 1at2 !at 2 * (PI / 180); dz -sin(latl) - sin(lat2); dx = (cos (long!) * cos(lat 1)) - cos(1at2); dysin(long1) *cos (lat1); dist mile-(asin(sqrt (dx * dx + dy *dy dz * dz) / 2)*2*R)/1.609344; return dist_ mile; void selection(int x, int size)Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 100
typedef struct place_s{
char name[100];
char country[30];
double latitude;
double longitude;
}place;
void main()
{
struct place_s places[MAX];
char buff[1000];
char *a;
char *b;
char *lat;
char *lon;
char *temp;
int i=0;
FILE *fp = fopen("locations.txt", "r");
while(fgets(buff, 1000, (FILE*)fp) != NULL)
{
if(buff != NULL)
{
strcpy(places[i].name,strtok(buff,","));
strcpy(places[i].country , strtok(NULL,","));
fgets(buff,1000,(FILE*)fp);
a = strtok(buff, "=");
a = strtok(NULL, "=");
b = strtok(a, ",");
temp = strtok(b, "(");
lat = strtok(NULL,"(");
places[i].latitude = atoi(lat);
temp = strtok(NULL, ",");
lon = strtok(temp,")");
places[i].longitude = atoi(lon);
if(strcmp(places[i].country,"USA") != 0)
{
printf("%s,%s ",places[i].name,places[i].country);
printf("(Latitude,Longitude) = (%d,%d) ",places[i].latitude,places[i].longitude);
}
i++;
}
}
fclose(fp);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.