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

NOTES:

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.

Explanation / Answer

Note: class name and function name already given

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

using namespace std;

struct City
{
   string name;
   double x;
   double y;
};

bool getData(string inputFile, City cities[], int &numCities);
double getDistance(City city1, City city2);
double td(City cities[], int numCities);
void greedyRouteImprove(City cities[], int numCities, const int MAX_CITIES);
void improveRoute(City cities[], int numCities);
const int MAX_CITIES = 100;

int main()
{
   int numCities = 0;

   City cities[MAX_CITIES];

   string inputFile = "route_data.txt";

   if (getData(inputFile, cities, numCities))
   {
      
       for (int dd = 0; dd < numCities - 1; dd++)
       cout <<cities[dd].name << " -> " << cities[dd+1].name << ": "<< getDistance(cities[dd], cities[dd+1]) << endl;
       cout << " The total distance is: " << td(cities, numCities) << endl;
       improveRoute(cities, numCities);
   }
   system("pause");
   return 0;
}

bool getData(string inputFile, City cities[], int &numCities)
{
   ifstream inFile;
   inFile.open("route_data.txt");
   if (inFile)
   {
       string tok;
       while (inFile >> tok)
       {
           cities[numCities].name = tok;
           inFile >> cities[numCities].x;
           inFile >> cities[numCities].y;
           numCities++;
       }
       return true;
   }
   else
   return false;
}

double getDistance(City city1, City city2)
{
   return (sqrt(pow((city2.y - city1.y),2) + pow((city2.x - city1.x),2)));
}

double td(City cities[], int numCities)
{
   double distance = 0;

   for (int t = 0; t < numCities-1; t++)
   distance += getDistance(cities[t],cities[t+1]);

   return distance;
}

void improveRoute(City cities[], int numCities)
{
   double DA[100][100];
   double CA[100][100];

   for (int a = 0; a < numCities; a++)
   for (int b = 0; b < numCities; b++)
       DA[a][b] = (getDistance(cities[a], cities[b]));

   for (int r = 0; r < numCities; r++)
   {
       for (int c = 0; c < numCities; c++)
       {
           if (DA[r][c] == 0.00)
           {
               DA[r][c] = -1.00;
           }

       }

   }

   std::copy(&DA[0][0], &DA[0][0]+numCities*numCities,&CA[0][0]);
   const int num = numCities;
   int ro = 0;
   double td = 0;
   int pA [20];

   while(ro < num)
   {
   for (int a = 0; a < num; a++)
   {
       for (int b = 0; b < num; b++)
       {
           cout << DA[a][b] ;
       }
       cout << endl;
   }
   cout << endl;

   double mr = 100.0;
   double mc = 100.0;

   for (int r = 0; r < num; r++)
   {
       for (int c = 0; c < num; c++)
       if (DA[r][c] < mr && DA[r][c] != -1)
           mr = DA[r][c];

       for (int iC = 0; iC < num; iC++)
           if (DA[r][iC] != -1)
               DA[r][iC] = (DA[r][iC] - mr);
       mr = 100;
   }

   for (int c = 0; c < num; c++)
   {
       for (int r = 0; r < num; r++)
           if (DA[r][c] < mc && DA[r][c] != -1)
               mc = DA[r][c];
       for (int iR = 0; iR < num; iR++)
           if (DA[iR][c] != -1)
               DA[iR][c] = (DA[iR][c] - mc);
       mc = 100;
   }

   double pen = 0;
   double penA[100][100];
   double min_r = 100.0;
   double minCol = 100.0;

   for (int x = 0; x < num; x++)
   for (int y = 0; y < num; y++)
       penA[y][x] = 0.00;

   for (int a = 0; a < num; a++)//row
   {
       for (int b = 0; b < num; b++)//column
       {
           if (DA[a][b] == 0)
           {
               DA[a][b] = 100.0;
               for (int c = 0; c < num; c++)
               {
                   if (DA[a][c] < min_r && DA[a][c] != -1)
                   min_r = DA[a][c];
               }
               for (int r = 0; r < num; r++)
               {
                   if (DA[r][b] < minCol && DA[r][b] != -1)
                   minCol = DA[r][b];
               }
               penA[a][b] = (min_r + minCol);
               min_r = 100.0;
               minCol = 100.0;
               DA[a][b] = 0;
           }
       }
   }

   double gr_pen = 0.0;

   cout << "Matrix for penality: " << endl;
   for (int a = 0; a < num; a++)
   {
       cout << setw(5);
       for (int b = 0; b < num; b++)
       {
       cout << setw(5) << penA[a][b] << " | ";
       if (penA[a][b] > gr_pen)
       gr_pen = penA[a][b];
   }
   cout << endl;
   }

   int ocount = 0;
   int scra_r = 0;
   int scra_c = 0;

   for (int a = 0; a < num; a++)
   for (int b = 0; b < num; b++)
   if (penA[a][b] == gr_pen)
   while (ocount < 1)
   {
       cout << "Location: " << a << " -> " << b << endl;
       pA[a] = b;
       td += CA[a][b];
       scra_r = a;
       scra_c = b;
       ocount++;
   }
   DA[scra_c][scra_r] = -1;
   for (int r = 0; r < num; r++)
       DA[r][scra_c] = -1;
   for (int c = 0; c < num; c++)
       DA[scra_r][c] = -1;

   //cout << " Matrix: Reduced" << endl;
   for (int a = 0; a < num; a++)
   {
       for (int b = 0; b < num; b++)
       {
          
           if (penA[a][b] > gr_pen)
           gr_pen = penA[a][b];
           }
          
       }
   ro++;
   }

   int st = 0;
   int city = 0;
   cout << " Route: ";
   while (st < num -1)
   {
  
       city = pA[city];
       st++;
   }

   for (int i = 0; i < num; i++)
   cout << i << "->" << pA[i] << ", ";
   cout << " Total distance: " << td << endl;
}

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