For this program you will be given a list of city names and x, y coordinates tha
ID: 3574478 • Letter: F
Question
For this program you will be given a list of city names and x, y coordinates that signify the location of the city. You will store the list of cities in an array of structures and then use that array to write a series of functions.The data will be in a text file named "route_data.txt" and will be in the following format:
city x_coord y_coord
e.g.
Houston 0.5 1.6
Denver -1.5 4.2
etc.
The cities are in order. The first city is your home city. You will figure out how far it is from your home city to the last city in the list by moving from city to city in the list and calculating the total distance (we are assuming that we have cars that fly through the air and can use a straight line distance). Then you will write a function to improve the ability to visit each city once (the last city you visit may change).
You will write four functions for this program:
//reads the initial data from the input file
//returns false if the file is not opened correctly
bool getData(string inputFile, City cities[], int &numCities);
//prints the cities in order, one per line
void printRoute(City cities[], int numCities);
//calculates the distance from one city to the next
double getDistance(City city1, City city2);
//calculates the total distance from the first city to the last city
double totalDistance(City cities[], int numCities);
//uses an algorithm to make the total distance shorter than the initial distance (if possible)
void improveRoute(City cities[], int numCities);
Your main program should have the following algorithm:
get the data
print the route
show the total distance
improve the route
print the route
show the total distance
display the percentage improvement
We will discuss the details of the assignment in class, including a simple route improving algorithm (although you may come up with something better on your own).
The program must be modular, with significant work done by functions. Each function should perform a single, well-defined task. When possible, create re-usable functions.
We have provided the prototypes for the functions you will need.
There will be a maximum of 100 cities.
The input file is named "route_data.txt" consisting of the following coordinates:
San_Marcos 0.0 0.0
Omaha 1.5 6.3
Austin 0.5 1.2
Chicago 3.1 8.2
Dallas 0.8 3.4
Denver -1.5 7.4
Houston 1.8 -0.7
McGregor 1.2 10.6
Explanation / Answer
Program code:
#include <iostream>
#include <fstream>
#include<math.h>
using namespace std;
class City
{
public:
string cname[10];
double x[10],y[10];
int i;
double dist1[10];
bool getdata()
{i=0;
ifstream myfile ("route_data.txt");
if (myfile.is_open())
{
while (!myfile.eof())
{
i=i+1;
myfile>>cname[i]>>x[i]>>y[i];
}
return true;
}
else
{
return false;
}
myfile.close();
}
void printRoute()
{
for(int k=1;k<=i;k++)
{
cout<<cname[k]<<x[k]<<y[k];
cout<<" ";
}
}
void distance()
{
double x1 ;
double y1 ,dist;
int l=0;
for(int k=1;k<=i-1;k++)
{
for(int m=k+1;m<=k+1;m++)
{
l=l+1;
x1 = x[m]-x[k];
y1 = y[m]-y[k];
dist = pow(x1,2)+pow(y1,2);
dist1[l]=sqrt(dist);
cout<<" distance from "<<k<<" to "<<m<<" is "<<dist1[l];
}
}
cout<<" "<<l;
double sum=0;
for(int k=1;k<=l;k++)
{
sum=sum+dist1[k];
}
cout<<" The total distance = "<<sum;
}
};
int main()
{
City c;
if(c.getdata()==1)
{
cout<<"file read successfull!!!!";
}
else
{
cout<<"sorry";
}
cout<<" ";
cout<<"Display data in order:";
cout<<" ";
c.printRoute();
cout<<" ";
cout<<"Distance form one city to another:";
cout<<" ";
c.distance();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.