#include <iostream> #include <fstream> #include <cstdlib> #include <ctime> #incl
ID: 3827350 • Letter: #
Question
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
struct City
{
string name;
double x;
double y;
};
//reads the initial data from the input file
//returns false if the file is not opened correctly
bool getData(ifstream &inFile, string fileName, City cities[], int &numCities)
{
string name;
while (inFile >> name)
{
cities[numCities].name = name;
inFile >> cities[numCities].x;
inFile >> cities[numCities].y;
numCities++;
}
}
//prints the cities in order, one per line
void printRoute(City cities[], int numCities)
{
for (int i = 0; i < numCities; i++)
{
cout << cities[i].name << " "
<< cities[i].x << " "
<< cities[i].y << endl;
}
}
//calculates the distance from one city to the next
double getDistance(City city1, City city2)
{
double diffSquared = (pow((city2.x - city1.x), 2) + pow((city2.y - city1.y), 2));
double distance = sqrt(diffSquared);
return distance;
}
//used in the improveRoute and totalDistance function
void swap(City &c1, City &c2)
{
City temp= c1;
c1= c2;
c2 = temp;
}
//uses an algorithm to make the total distance shorter than the initial distance (if possible)
void improveRoute(City cities[], int numCities)
{
if(getDistance(cities[i], cities[j])<distance)
{
closest=j;
distance = getDistance(cities[i], cities[j]);
}
}
//calculates the total distance from the first city to the last city
double totalDistance(City cities[], int numCities)
{
for (int i=0; i<numCities-1; i++)
{
int closest = i+1;
double distance = getDistance(cities[i], cities[closest]);
for (int j=i+2; j<numCities; j++)
{
improveRoute(cities, numCities);
}
swap(cities[i+1], cities[closest]);
}
}
int main()
{
const int MAX_CITIES = 100;
City cities[MAX_CITIES];
int numCities = 0;
string fileName = "route_data.txt";
ifstream inFile;
inFile.open(fileName.c_str());
getData(inFile, fileName, cities, numCities);
printRoute(cities, numCities);
getDistance(City1, City2);
totalDistance(cities, numCities);
}
please give me detailed explanation for me to study. this is C++computer science cource with the program codeblock. this program deals with swap fucntion (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 citiesO, int &numCities;): prints the cities in order, one per line void printRoute (City cities0, int numCities); //calculates the distance from one city to the next double getDistance(City cityl, City city2); //calculates the total distance from the first city to the last city double totalDistance(City citiesO, int numCities); //uses an algorithm to make the total distance shorter than the initial distance (ifpossible) void improveRoute(City cities0, 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 improvementExplanation / Answer
please find comments in respective functions
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
//structure for city .It’s having name ,x coordinate and y coordinate of city.
struct City
{
string name;
double x;
double y;
};
//reads the initial data from the input file
//returns false if the file is not opened correctly
/*
inFile -> input file stream i.e. pointer to file
filename -> Name of input file
City cities[] -> array of struct City to store the information from file
numCities-> variable for number of cities in file.It’s get updated in this fiunction
*/
bool getData(ifstream &inFile, string fileName, City cities[], int &numCities)
{
string name;
while (inFile >> name) //read file line by line and name of city gets in string name
{
//store city information in array of struct
cities[numCities].name = name;
inFile >> cities[numCities].x;
inFile >> cities[numCities].y;
numCities++;
}
}
//prints the cities in order, one per line
/*City cities[]-> array of struct which contains city information
numCities-> Number of cities in file which we got in getData() function
*/
void printRoute(City cities[], int numCities)
{
for (int i = 0; i < numCities; i++)
{
cout << cities[i].name << " "
<< cities[i].x << " "
<< cities[i].y << endl;
}
}
//calculates the distance from one city to the next
/*
Calculate distance between two cities using distance formula
Distance between (x1,y1) and (x2,y2) is
Dist =
*/
double getDistance(City city1, City city2)
{
double diffSquared = (pow((city2.x - city1.x), 2) + pow((city2.y - city1.y), 2));
double distance = sqrt(diffSquared);
return distance;
}
//used in the improveRoute and totalDistance function
/*
Here city1 and city2 are passed as reference so that after swapping we get correct results i.e. city1 and city2 swapped.If we pass by value then values will not be swapped
*/
void swap(City &c1, City &c2)
{
City temp= c1;
c1= c2;
c2 = temp;
}
//uses an algorithm to make the total distance shorter than the initial distance (if possible)
/*
Here we are calculating distance and comparing with distance i.e.
*/
void improveRoute(City cities[], int numCities)
{
if(getDistance(cities[i], cities[j])<distance) //calculating distance between two cities and comparing with initial distance.If it’s less than initial distance then it’s near.For this Initial distance must be as high as possible
{
closest=j;
distance = getDistance(cities[i], cities[j]);
}
}
//calculates the total distance from the first city to the last city
/*
Here we are calculating distance of first city and last city by using intermediate cities in between.i.e. distance between city1 and city2 ,city2 and city 3 and so on and chossing best city depending on shortest distance between those cities
*/
double totalDistance(City cities[], int numCities)
{
for (int i=0; i<numCities-1; i++)
{
int closest = i+1;
double distance = getDistance(cities[i], cities[closest]);
for (int j=i+2; j<numCities; j++)
{
improveRoute(cities, numCities);
}
swap(cities[i+1], cities[closest]);
}
}
int main()
{
const int MAX_CITIES = 100;
City cities[MAX_CITIES];
int numCities = 0;
string fileName = "route_data.txt";
ifstream inFile; //stream for input file
//calling functions
inFile.open(fileName.c_str());
getData(inFile, fileName, cities, numCities);
printRoute(cities, numCities);
getDistance(City1, City2);
totalDistance(cities, numCities);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.