please give me detailed explanation for me to study. this is C++ computer scienc
ID: 3827079 • Letter: P
Question
please give me detailed explanation for me to study. this is C++ computer science cource with the program codeblock.
this program deals with swap? (according to class).
thank you.!!!
Are we there yet?
CS 1428
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
Explanation / Answer
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
struct City
{
string city;
double x_coord;
double y_coord;
};
//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)
{
ifstream fin;
fin.open(inputFile.c_str());
if(!fin)
return false;
while(!fin.eof())
{
fin >> cities[numCities].city;
fin >> cities[numCities].x_coord;
fin >> cities[numCities].y_coord;
numCities++;
}
return true;
}
//prints the cities in order, one per line
void printRoute(City cities[], int numCities)
{
cout << endl;
for(int i=0; i<numCities; i++)
cout << cities[i].city << " (" << cities[i].x_coord << "," << cities[i].y_coord << ") ";
}
//calculates the distance from one city to the next
double getDistance(City city1, City city2)
{
return sqrt((city1.x_coord-city2.x_coord)*(city1.x_coord-city2.x_coord) + (city1.y_coord-city2.y_coord)*(city1.y_coord-city2.y_coord));
}
//calculates the total distance from the first city to the last city
double totalDistance(City cities[], int numCities)
{
double distance = 0;
for(int i=1; i<numCities; i++)
distance += getDistance(cities[i], cities[i-1]);
return distance;
}
//uses an algorithm to make the total distance shorter than the initial distance (if possible)
void improveRoute(City cities[], int numCities)
{
for(int i=0; i<numCities-1; i++)
{
int index = i+1;
double mindistance = getDistance(cities[i+1], cities[i]);
for(int j=i+1; j<numCities; j++)
{
if(mindistance > getDistance(cities[i], cities[j]))
{
index = j;
mindistance = getDistance(cities[i], cities[j]);
}
}
City temp = cities[i+1];
cities[i+1] = cities[index];
cities[index] = temp;
}
}
//Main function to test the above functions
int main()
{
string inputFile;
cout << "Enter the Input File Name: ";
cin >> inputFile;
City cities[100];
int numCities = 0;
if(getData(inputFile, cities, numCities) == false)
{
cerr << "Error in opening file.";
}
else
{
printRoute(cities, numCities);
double distance1 = totalDistance(cities, numCities);
cout << endl << distance1 << endl;
improveRoute(cities, numCities);
printRoute(cities, numCities);
double distance2 = totalDistance(cities, numCities);
cout << endl << distance2 << endl;
cout << endl << "The Percentage Improvement in the Total Distance is: " << (distance1-distance2)*100/distance1 << "% ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.