Question: Need Help writing this code that I have already writen most for I just dont know how I can take the numbers and put it
Need Help writing this code that I have already writen most for I just dont know how I can take the numbers and put it ascending order and keep all the Cities and states
together as well. (I have attached the problem and the code I have which works so far it just doesnt print in ascending order)

#include
#include
#include
#include
#include
#include
using namespace std;
double Clong =-80.8290;
double Clatt = 35.2060;
double R = 3959;
const double pi = 3.14159265358979323846;
class Distance{
private:
double latti;
double longi;
public:
string city;
string state;
double D;
Distance();
Distance(string a, string b, string c, string d);
void Calculate();
};
Distance :: Distance(){
latti = 0.0;
longi = 0.0;
city = "Unknown";
state = "Unknown";
}
Distance :: Distance(string a, string b, string c, string d){
city = a;
state = b;
latti = atof(c.c_str());
longi = atof(d.c_str());
}
void Distance :: Calculate(){
double dlon = longi - Clong;
double dlat = latti - Clatt;
dlat = dlat * (pi/180);
dlon = dlon * (pi/180);
Clatt = Clatt * (pi/180);
latti = latti * (pi/180);
double a = pow(sin(dlat/2),2) + cos(Clatt)*cos(latti)*pow(sin(dlon/2),2);
double c = 2 * atan2(sqrt(a), sqrt(1-a));
D = R*c;
}
int main(){
ifstream theFile("cities.txt");
ofstream test("dist_clt.txt");
string line;
string s;
string c;
string lon;
string lat;
while(theFile){
getline(theFile,c,',');
getline(theFile,s,',');
getline(theFile,lat,',');
getline(theFile,lon);
Distance test1(c,s,lat,lon);
test1.Calculate();
test
}
return 0;
}
Your goal is to print the cities in United States sorted in the increasing order of distance from Charlotte. Download from Canvas the text file (cities txt) containing the latitude and longitude information of nearly 30,000 cities in the United States. Calculate the distance from Charlotte to each of these cities. Sort the cities in ascending order of distance, and print the sorted list along with the distance from Charlotte, to an output file dist C ample entries in your output would look like this Yorktown, IA 867.294 Here, 867.294 is the distance in miles from Charlotte. Do a sanity check to make sure your output is correct! Use the Haversine formula to calculate the great-circle distance between two points that is, the shortest distance over the earth's surface giving an as-the-crow-flies' distance between the points (ignoring any hills they fly over, of Course Haversine formula: delta ph phi (city) phi(Charlotte) delta lambda ambda(city) lambda (Charlotte) a sin (delta phi/2) cos(phi1) cos(phi2)'sin (delta lambda/2) c 2 atan2(sqrt(a), sqrt(1-a)) where phi is latitude, lambda is longitude, Ris earth's radius (mean radius 3,959 miles Note that angles need to be in radians to pass to trig functions! The latitude and longitude of Charlotte is 35.2060, 80.8290 Hint: Carefully think of the operations that you need to do read line from file: split line into city name & state, latitutde, longitude: store these in a data structure: calculate the distance; sort by distance to Charlotte: and print the output. Your goal is to print the cities in United States sorted in the increasing order of distance from Charlotte. Download from Canvas the text file (cities txt) containing the latitude and longitude information of nearly 30,000 cities in the United States. Calculate the distance from Charlotte to each of these cities. Sort the cities in ascending order of distance, and print the sorted list along with the distance from Charlotte, to an output file dist C ample entries in your output would look like this Yorktown, IA 867.294 Here, 867.294 is the distance in miles from Charlotte. Do a sanity check to make sure your output is correct! Use the Haversine formula to calculate the great-circle distance between two points that is, the shortest distance over the earth's surface giving an as-the-crow-flies' distance between the points (ignoring any hills they fly over, of Course Haversine formula: delta ph phi (city) phi(Charlotte) delta lambda ambda(city) lambda (Charlotte) a sin (delta phi/2) cos(phi1) cos(phi2)'sin (delta lambda/2) c 2 atan2(sqrt(a), sqrt(1-a)) where phi is latitude, lambda is longitude, Ris earth's radius (mean radius 3,959 miles Note that angles need to be in radians to pass to trig functions! The latitude and longitude of Charlotte is 35.2060, 80.8290 Hint: Carefully think of the operations that you need to do read line from file: split line into city name & state, latitutde, longitude: store these in a data structure: calculate the distance; sort by distance to Charlotte: and print the output
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
