Question


Your goal is to print the cities in United States sorted in the increasing order of distance from Charlotte. Download from Ca

(The cities.txt file is just a list of almost 30,000 cities and their distance from Charlotte)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution The C++ code for given problem is shown below //Include libraries # include<iostream> include<fstream> # include<strstring state: //Declare variable double D //call method Distance (); //Define method Distance (string la, string lb, string l//Define method void Distance Calculate () //Assign value double 1DlnlLngi -lclng; //Assign value double 1Dlt1Tti-1cltt; //Asreturn 0; Input file cities Notepad File Edit Format View Help Abbevi11e,AL,31.5951,-85. 2108 Abbeville, GA, 31.9890,-83. 321

code;

//Include libraries

#include<iostream>

#include<fstream>

#include<string>

#include<cmath>

#include<cstdlib>

#include<iomanip>

//Use namespace

using namespace std;

//Declare variable

double lClng =-80.8290;

//Declare variable

double lCltt = 35.2060;

//Declare variable

double lR = 3959;

//Declare variable

const double lPi = 3.14159265358979323846;

//Define class

class Distance

{

//Declare access specifier

private:

//Declare variable

double lTti;

//Declare variable

double lLngi;

//Declare access specifier

public:

//Declare variable

string city;

//Declare variable

string state;

//Declare variable

double D;

//Call method

Distance();

//Define method

Distance(string la, string lb, string lc, string d);

//Define method

void Calculate();

};

//Define method

Distance :: Distance()

{

//Assign value

lTti = 0.0;

//Assign value

lLngi = 0.0;

//Assign value

city = "Unknown";

//Assign value

state = "Unknown";

}

//Define method

Distance :: Distance(string la, string lb, string lc, string d)

{

//Assign value

city = la;

//Assign value

state = lb;

//Assign value

lTti = atof(lc.c_str());

//Assign value

lLngi = atof(d.c_str());

}

//Define method

void Distance :: Calculate()

{

//Assign value

double lDln = lLngi - lClng;

//Assign value

double lDlt = lTti - lCltt;

//Assign value

lDlt = lDlt * (lPi/180);

//Assign value

lDln = lDln * (lPi/180);

//Assign value

lCltt = lCltt * (lPi/180);

//Assign value

lTti = lTti * (lPi/180);

//Assign value

double la = pow(sin(lDlt/2),2) + cos(lCltt)*cos(lTti)*pow(sin(lDln/2),2);

//Assign value

double lc = 2 * atan2(sqrt(la), sqrt(1-la));

//Compute value

D = lR*lc;

}

//Define main

int main()

{

//Open file

ifstream theFile("cities.txt");

//Open file

ofstream test("dist_clt.txt");

//Declare variable

string line;

//Declare variable

string s;

//Declare variable

string lc;

//Declare variable

string lon;

//Declare variable

string lat;

//Loop

while(theFile)

{

//Get value

getline(theFile,lc,',');

//Get value

getline(theFile,s,',');

//Get value

getline(theFile,lat,',');

//Get value

getline(theFile,lon);

//Create instance

Distance test1(lc,s,lat,lon);

//Call method

test1.Calculate();

//Write

test << lc << "," << s << "," << test1.D<<endl;

}

//Pause console window

system("pause");

//Return

return 0;

}

Add a comment
Know the answer?
Add Answer to:
(The cities.txt file is just a list of almost 30,000 cities and their distance from Charlotte)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Programming Hi! Sorry for all the material attached. I simply need help in writing the...

    C++ Programming Hi! Sorry for all the material attached. I simply need help in writing the Facility.cpp file and the other files are included in case they're needed for understanding. I was able to complete the mayday.cpp file but am stuck on Facility. The following link contains a tar file with the files provided by the professor. Thank you so much in advanced! http://web.cs.ucdavis.edu/~fgygi/ecs40/homework/hw4/ Closer.h: #ifndef CLOSER_H #define CLOSER_H #include <string> #include "gcdistance.h" struct Closer { const double latitude, longitude;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT