Question: help fix lowest cost keeps retruning zero and the tour prints nothing Purpose: Traveling Salesman - find the lowest cost tour when traveling from US

help fix lowest cost keeps retruning zero and the tour prints nothing
Purpose: Traveling Salesman - find the lowest cost
tour when traveling from US to 8 other countries
and then back to US.
*/
#include
#include
#include
#include "GraphMatrix.h"
#include
using namespace std;
const int SIZE =10;
const string COUNTRY_CODES[SIZE]={"AU","BR","CA","CN","GL","IT","NA","RU","US","US"};
/*
Use the following structure to store each tour & its cost
You will create an array of Tour variables in your program
*/
struct Tour
{
string tour[SIZE];
int cost;
};
// Function Prototypes
int searchCountryCode(string);
GraphMatrix* readFileMakeMatrix();
void printStringArray(string* arr, int size);
void lexicographic(string* countries, int size, Tour* tourOptions, GraphMatrix* matrix, int& permutationIndex);
void saveTour(Tour* tourOptions, string* permutation, GraphMatrix* matrix, int& permutationIndex);
void findLowest(Tour* tourOptions);
int main()
{
Tour* tourOptions = new Tour[40320];
//read in the flight information from the file and then create the weight matrix
GraphMatrix* matrix = readFileMakeMatrix();
string* countries = new string[SIZE -1];
cout <<"
*************************COUNTRIES*******************
";
for (int x =0; x < SIZE -1; x++)
{
countries[x]= COUNTRY_CODES[x];
cout << countries[x]<< endl;
}
//generate all possible tours (starting & ending with "US") using lexicographic permute algorithm
//you will need to call your lexicographic function, sending the modified countries array with the 8 country codes
cout <<"
*************************SOLUTION*******************
";
//find the lowest cost tour and print it out (including the cost)
cout << "Generating permutations and calculating costs...
";
int permutationIndex =0;
lexicographic(countries, SIZE -1, tourOptions, matrix, permutationIndex); // Updated function call
findLowest(tourOptions);
cout <<"
Happy Traveling!
";
delete[] countries;
delete[] tourOptions;
delete matrix;
return 0;
}
/*
Function: searchCountryCode
Parameters: a string with the country code like "BR"
Returns: an integer representing this country's index in the GraphMatrix.
For example, if the parameter is "BR", it should return index 1. If the parameter is "CA", it should return index 3
It is returning the index value in the COUNTRY_CODES array.
Implement this search function with the binary search algorithm!
*/
int searchCountryCode(string country)
{
for (int i =0; i < SIZE; ++i){
if (COUNTRY_CODES[i]== country){
return i;
}
}
return -1; // Not found
}
/*
Title: readFileMakeMatrix
Purpose: this function will read the data in flights.txt and create an
adjacency matrix with the country codes. The matrix values will be the
cost of the flights.
*/
GraphMatrix* readFileMakeMatrix()
{
ifstream inFile;
GraphMatrix* matrix = new GraphMatrix(SIZE -1);
cout <<"
Created the matrix.";
string country1, country2;
int price;
inFile.open("C:/Users/rockc/Downloads/flights.txt");
cout <<"
Reading from flights.txt
";
if(inFile)
{
while(inFile >> country1)
{
inFile >> country2;
inFile >> price;
//add price to graph matrix
matrix->addEdge(searchCountryCode(country1), searchCountryCode(country2), price);
cout <<"
Added edge from "<< country1<<" to "<< country2<<" with cost of $"<< price;
}
}
else
cout <<"
Sorry, I am unable to open the file.
";
inFile.close();
cout <<"
FLIGHT PRICE GRAPH MATRIX
";
matrix->printGraph();
cout << endl;
return matrix;
}
/*
Title: printStringArray
Purpose: this function will print the array of strings with a space
between each string
*/
void printStringArray(string* arr, int size)
{
for(int x =0; x < size; x++)
{
cout << arr[x]<<"";
}
cout << endl;
}
/*
Function: lexicographic
Parameters:
- countries: array of country codes
- size: size of the countries array
- tourOptions: array to store all tours and their costs
- matrix: graph matrix containing flight costs
- permutationIndex: index to store the tour in tourOptions
Purpose: Generate all possible tours starting and ending with "US" using the lexicographic permutation algorithm.
calculate the cost of each tour and stores it in tourOptions.
*/
void generatePermutations(string* countries, int start, int end, Tour* tourOptions, GraphMatrix* matrix, int& permutationIndex)
{
if

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!