You are to write a Trip Distance Calculator that calculates the approximate driv
ID: 643326 • Letter: Y
Question
You are to write a Trip Distance Calculator that calculates the approximate driving distance for a trip that visits multiple cities. Write a program that lists a menu of cities, including an exit option, and keeps track of the total estimated driving miles as each city is chosen. When the user exits, the final total estimated driving miles are displayed. You must use the mileage data list in Table1. You are allowed to augment the list. For example, you can add Indianapolis, but you must leave the existing cities. Adding a city, requires you to include all the estimated driving distances to all the current cities. Driving from a city to itself gives a distance of 0. All the empty cells of the table on the diagonal should be populated with 0. Use C language and use Arrays and pointers.Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
void cityList(string str[], int size){
for(int i = 0; i < size; ++i){
cout << (i + 1) << ". " << str[i] << endl;
}
}
int main(){
int option, total = 0, size = 11;
string arr[] = {"Atlanta, GA", "Chicago, IL", "Denver, CO", "Houston, TX", "Kansas City, MO",
"Los Angeles, CA", "Minneapolia, MN", "Miami, FL", "New York, NY", "San Francisco, CA", "Seattle, WA"};
int distance[11][11] = {{0, 715, 1405, 800, 805, 2185, 1135, 665, 865, 2495, 2785},
{1150, 0, 1000, 1085, 525, 2020, 410, 1380, 795, 2135, 2070},
{2260, 1615, 0, 1120, 600, 1025, 915, 2065, 1780, 1270, 1335},
{1285, 1750, 1805, 0, 795, 1550, 1230, 1190, 1635, 1930, 2450},
{1295, 850, 965, 1280, 0, 1625, 440, 1470, 1195, 1865, 1900},
{3515, 3250, 1650, 2495, 2610, 0, 1935, 2740, 2800, 385, 1140},
{1825, 665, 1470, 1980, 680, 3110, 0, 1795, 1200, 2010, 2015},
{1070, 2220, 3320, 1915, 2365, 4405, 2885, 0, 1280, 3115, 3365},
{1390, 1275, 2865, 2630, 1925, 4505, 1935, 2060, 0, 3055, 2860},
{4015, 3435, 2040, 3105, 3000, 615, 3240, 5015, 4915, 0, 810},
{4485, 3330, 2140, 3940, 3060, 1835, 2675, 5415, 4600, 1305, 0}
};
int f, s;
cityList(arr, size);
cout << "Where are you starting at ?: ";
cin >> f;
while(f < 1 || f > 11){
cout << "Error: try again" << endl;
cout << "Where are you starting at ?: ";
cin >> f;
}
do{
cout << "1. Next destination" << endl;
cout << "2. exit" << endl;
cout << "Enter an option: ";
cin >> option;
switch(option){
case 1:
cityList(arr, size);
cout << "Choose an option: ";
cin >> s;
while(s < 1 || s > 11){
cout << "Error: try again " << endl;
cout << "Choose an option: ";
cin >> s;
}
total += distance[f - 1][s - 1];
f = s;
break;
default:
cout << "Please choose a valid option" << endl;
}
}while(option != 2);
cout << "Total distance = " << total << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.