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

Code to be written in C++ Input data contains a list of cities with their coordi

ID: 3802524 • Letter: C

Question

Code to be written in C++

Input data contains a list of cities with their coordinates. The cities in the list are neither guaranteed to be alphabetically sorted, nor unique (that is, the same city with the same coordinates can be listed multiple times in the input file)

Given a list of cities and their geographic coordinates (decimal), compute the lengths of two paths connecting all of the input cities in their West-East (North-South, respectively) directions, both in kilometers and miles. The decimal geographic coordinates rounded to four decimal places should be used, the output should be rounded to integers, and the harversine formula should be used to compute distances between cities. Use 0.621371 multiplier to convert from kilometers to miles. Input consist of a number of lines; the number of lines is between 2 and 10,000.

Each line contains a city (string) and two numbers, geographic coordinates in the decimal notation. Output consists of six lines: A pair of westmost - eastmost cities, followed by two lines listing the length of the path (that includes all of the input cities between the westmost and eastmost) in kilometers and miles. Similarly, output the information for the cities between the northmost-southmost pair. Note that only the names of the extremes cities on each path are listed, but (typically a very zig-zagging) path includes all of the input cities. See example below for details. Nothing else is included in the output.

***MUST USE HAVERSINE FORMULA:

lat = lat2 - lat1; lng = lng2 - lng1

d = sin( lat 0 . 5 ) 2 + cos( lat 1 ) cos( lat2 ) sin(lng 0 . 5 ) 2

Example: For the test set: Chicago , Naperville , Lisle

Input

Chicago 36.8297222 -84.8491667

Naperville 37.9886111 -84.4777778

Lisle 37.0833333 -88.6000000

Output

WE: Lisle - Naperville

467 km

290 miles

NS: Naperville - Chicago

711 km

442 miles

Explanation / Answer

#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
//Read data from file and returns number of records
int readFile(string city[], double lat[], double lon[])
{
//Creates an object of ifstream
ifstream readf;
//Opens the file for reading
readf.open ("Cities.txt");
float la,lo;
int c = 0;
//Loops till end of file
while(!readf.eof())
{
//Reads data and stores in respective array
readf>>city[c];
readf>>lat[c];
readf>>lon[c];
//Increase the record counter
c++;
}//End of while
//Displays the file contents
for(int x = 0; x < c; x++)
{
cout<<" City name: "<<city[x]<<" Latitude: "<<lat[x]<<" Longitude: "<<lon[x];
}//End of for loop
//Close file
readf.close();
//Returns record counter
return c;
}//End of function
//Calculation function
void calculate(string city[], double lat[], double lon[], int c)
{
//Loops till end of record
for(int x = 0; x < c - 1; x++)
{
double la = lat[x+1] - lat[x];
double lo = lon[x+1] - lat[x];
double d = sin(la * 0.5) * 2 + cos(lat[x]) * cos(lat[x+1]) * sin(lo * 0.5) * 2;
d = d * 0.621371;
int i = d;
int r = (d - i)*1000;
if(x %2 == 0)
cout<<" WE: "<<city[x+1]<<" - "<<city[x]<<endl;
else
cout<<" NS: "<<city[x+1]<<" - "<<city[x]<<endl;
cout<<i<<"km"<<" "<<r<<"miles";
}//End of loop
}//End of function
//Main function
int main()
{
string city[100];
double latitude[100], longigude[100];
int len;
//Call function to read file
len = readFile(city, latitude, longigude);
//calls function to calculate
calculate(city, latitude, longigude, len);
}//End of main

Output:

City name: Chicago Latitude: 36.8297 Longitude: -84.8492
City name: Naperville Latitude: 47.9886 Longitude: -84.4778
City name: Lisle Latitude: 27.0833 Longitude: -88.6
WE: Naperville - Chicago
-1km
-231miles
NS: Lisle - Naperville
1km
282miles

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote