Question
In C++ Need some help altering my program so that you do not visit the same city more than once.Hint: Invalidate an entire row or
In C++
Need some help altering my program so that you do not visit the same city more than once.Hint: Invalidate an entire row or column.
Sample Run
City List
----------------
0) Lansing
1) Flint
2) Detroit
3) Jackson
4) Grand Rapids
5) Battle Creek
6) Kalamazoo
7) Clare
Enter start city: 7
Enter stop city: 5
Clare to Lansing = 85
Lansing to Flint = 53
Flint to Detroit = 77
Detroit to Jackson = 68
Jackson to Battle Creek = 30
Total = 313
_________________________________________________________
Main.cpp
#include
#include
using namespace std;
#include "Lab27.h2"
/******************************
* main()
******************************/
void main()
{
int i, j, s, e;
cout << "City List ------------------" << endl;
// Show cities
for(i=0;i cout << city[i] << endl; cout << "Enter start city: "; cin >> s; cout << "Enter stop city: "; cin >> e; i = s, j = 0; int total = 0; // until start is not equal to end while (i != e) { if (map[i][j] != 0 ) { cout << city[i] << " to " << city[j] << " = " << map[i][j] << endl; total += map[i][j]; map[i][j] = 0; i = j, j = 0; } else { j++; } } cout << "Total = " << total; } Data.h2 // Cities and Distances #define CITY_MAX8 int map[CITY_MAX][CITY_MAX] = { {0,53,80,34, 75,35, 0, 85 }, { 53, 0,77, 0,0, 0, 0, 94 }, { 80,77, 0,68,0, 0, 0,0 }, { 34, 0,68, 0,0,30, 0,0 }, { 75, 0, 0, 0,0, 0,59,109 }, { 35, 0, 0,30,0, 0,47,0 }, {0, 0, 0, 0, 59,47, 0,0 }, { 85,94, 0, 0,109, 0, 0,0 } }; string city[CITY_MAX] = { "Lansing","Flint","Detroit","Jackson","Grand Rapids","Battle Creek","Kalamazoo","Clare" };
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started