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: 3574237 • 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

Cities.cpp

#include<stdio.h>
#include <string>
#include <fstream>
#include <conio.h>
#include <math.h>
#define MAX_SIZE 1000

using namespace std;

// City class
class City
{
   public:
       double x;
       double y;
       string name;
};

// Function declarations
bool getData(string fileName, City cities[MAX_SIZE],int numberOfCities);
void printRoute(City cities[MAX_SIZE],int numberOfCities);
double getDistance(City,City);
double totalDistance(City cities[MAX_SIZE],int numberOfCities);

// Main function
main()
{
   string fileName = "route_data.txt";
   int length = 5;
   City cities[length];
   getData(fileName,cities,length);
   printRoute(cities,length);
   printf("Total distance : %f",totalDistance(cities,length));
}

// Fetches cities from file with given length
bool getData(string fileName, City cities[],int numberOfCities)
{
   ifstream file (fileName.c_str());
   if(file.is_open())
   {
       double x, y;
       char name[100];
       int count=0;
       while (file >> name >> x >> y && count < numberOfCities)
       {
           cities[count].name = name;
           cities[count].x = x;
           cities[count++].y = y;
       }
       file.close();
   }  
}

// Prints route between cities
void printRoute(City cities[],int numberOfCities)
{
   int count = 0;
   while (count < numberOfCities)
   {
       printf("%s ",cities[count].name.c_str());
       count++;
   }
}

// Calculates distance between two cities
double getDistance(City source,City destination)
{
   double distance = pow((destination.x - source.x),2) + pow((destination.y - source.y),2);
   return pow(distance,0.5);
}

// Calculates distance between first and last cities
double totalDistance(City cities[],int numberOfCities){
   int count = 0;
   double total = 0;
   while (count < numberOfCities - 1)
   {
       total += getDistance(cities[count],cities[count+1]);
       count++;
   }
}

route_data.txt

kondapur 0 0
madhapur 4 0
hitech 16 0
gachibowli 32 0
begumpet 64 0

Sample output

kondapur
madhapur
hitech
gachibowli
begumpet
Total distance : 64.000000

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