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

For this program you will be given a list of city names and x, y coordinates tha

ID: 3574466 • 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).

he 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

#include<iostream>
#include<fstream>
#include<string>
#include<cmath>

using namespace std;

typedef struct City
{
float x_cor,y_cor;
string Name;
}City;

bool getData(string inputFile, City cities[], int &numCities)
{
std::ifstream myfile(inputFile.c_str());
string city_name;
float x,y;

numCities = 0;

if(!myfile)
return false;

while(myfile >> city_name >> x >>y )
{
cities[numCities].Name = city_name;
cities[numCities].x_cor = x;
cities[numCities].y_cor = y;

numCities++;
}
return true;
}
void printRoute(City cities[], int numCities)
{
for(int i = 0; i < numCities; i++)
{
cout<<cities[i].Name<<endl;
}
}
double getDistance(City city1, City city2)
{
return sqrt(((city2.x_cor - city1.x_cor) * (city2.x_cor - city1.x_cor)) + ((city2.y_cor - city1.y_cor) * (city2.y_cor - city1.y_cor)));
}
double totalDistance(City cities[], int numCities)
{
double total_distance = 0;
for(int i = 1; i < numCities; i++)
{
total_distance += getDistance(cities[0], cities[i]);
}
return total_distance;

}
/*
void improveRoute(City cities[], int numCities)
{
double track[numCities][numCities];

vector<int> temp;
int index=0;
int row=0,col=0;

for(int i = 0; i < numCities ; i++)
{
for(int j = 0; j < numCities ; j++)
track[i][j] = 0.0;
}

for(int i = 0; i < numCities ; i++)
{
for(int j = 0; j < numCities ; j++)
{
if(i != j)
track[i][j] = getDistance(cities[i],cities[j]);
}
}

cout<<"printing distance matrix"<<endl;
for(int i = 0; i < numCities ; i++)
{
for(int j = 0; j < numCities ; j++)
cout<<track[i][j]<<" ";

cout<<endl;
}
temp.push_back(0);

while(index < numCities)
{
for(int i = row; i<numCities; i++)
{

}
}


}
*/
int main()
{
int numCities;
string inputFile;
City *cities;

cout<<"Please enter Name of the input file : ";
cin>>inputFile;

cities = new City[50];

if(!getData(inputFile, cities, numCities))
{
cout<<"Unable to open file"<<endl;
return 0;
}
printRoute(cities, numCities);


double total_distance = totalDistance(cities, numCities);

cout<<" Total distance from first city to last city : "<<total_distance<<endl;

//improveRoute(cities, numCities);
return 0;
}

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