You are to write a Trip Distance Calculator that calculates the approximate driv
ID: 644251 • Letter: Y
Question
You are to write a Trip Distance Calculator that calculates the approximate driving distance for a trip the visits multiple cities.
The approximate driving distance between two cities is often depicted in a mileage chart. I mileage chart is a two-dimensional table that lists across rows and columns. To find the distance between two cities, find the cell that is the intersection between the origination city row and the destination city column (or vice versa). A sample mileage chart is listed below.
Distance between some major USA cities
Miles
Kilometers
Atlanta, GA
Chicago, IL
Denver, CO
Houston, TX
Kansas City, KS
Los Angeles, CA
Minnea-
polis, MN
Miami, FL
New York, NY
San Francis- co, CA
Seattle, WA
Atlanta, GA
715
1405
800
805
2185
1135
665
865
2495
2785
Chicago, IL
1150
1000
1085
525
2020
410
1380
795
2135
2070
Denver, CO
2260
1615
1120
600
1025
915
2065
1780
1270
1335
Houston, TX
1285
1750
1805
795
1550
1230
1190
1635
1930
2450
Kansas City, MO
1295
850
965
1280
1625
440
1470
1195
1865
1900
Los Angeles, CA
3515
3250
1650
2495
2610
1935
2740
2800
385
1140
Minneapolis, MN
1825
665
1470
1980
680
3110
1795
1200
2010
2015
Miami, FL
1070
2220
3320
1915
2365
4405
2885
1280
3115
3365
New York, NY
1390
1275
2865
2630
1925
4505
1935
2060
3055
2860
San Francisco, CA
4015
3435
2040
3105
3000
615
3240
5015
4915
810
Seattle, WA
4485
3330
2140
3940
3060
1835
2675
5415
4600
1305
Table 1 Mileage Chart Distance Between Some USA Cities
Requirements
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. This corresponds to the diagonal in Table 1. All the empty cells of the table on the diagonal should be populated with 0.
The following is a sample run of your program.
Please choose a city:
Atlanta, GA
Chicago, IL
Denver, CO
Houston, TX
Kansas City, MO
Los Angeles, CA
Minneapolis, MN
Miami, FL
New York, NY
San Francisco, CA
Seattle, WA
Exit
City: 1
Distance = 0 miles
Please choose a city:
Atlanta, GA
Chicago, IL
Denver, CO
Houston, TX
Kansas City, MO
Los Angeles, CA
Minneapolis, MN
Miami, FL
New York, NY
San Francisco, CA
Seattle, WA
Exit
City: 2
Distance = 715 miles
Please choose a city:
Atlanta, GA
Chicago, IL
Denver, CO
Houston, TX
Kansas City, MO
Los Angeles, CA
Minneapolis, MN
Miami, FL
New York, NY
San Francisco, CA
Seattle, WA
Exit
City: 3
Distance = 1715 miles
Please choose a city:
Atlanta, GA
Chicago, IL
Denver, CO
Houston, TX
Kansas City, MO
Los Angeles, CA
Minneapolis, MN
Miami, FL
New York, NY
San Francisco, CA
Seattle, WA
Exit
City: 12
Total Distance = 1715 miles
*Write program using C language.
Distance between some major USA cities
Miles
Kilometers
Atlanta, GA
Chicago, IL
Denver, CO
Houston, TX
Kansas City, KS
Los Angeles, CA
Minnea-
polis, MN
Miami, FL
New York, NY
San Francis- co, CA
Seattle, WA
Atlanta, GA
715
1405
800
805
2185
1135
665
865
2495
2785
Chicago, IL
1150
1000
1085
525
2020
410
1380
795
2135
2070
Denver, CO
2260
1615
1120
600
1025
915
2065
1780
1270
1335
Houston, TX
1285
1750
1805
795
1550
1230
1190
1635
1930
2450
Kansas City, MO
1295
850
965
1280
1625
440
1470
1195
1865
1900
Los Angeles, CA
3515
3250
1650
2495
2610
1935
2740
2800
385
1140
Minneapolis, MN
1825
665
1470
1980
680
3110
1795
1200
2010
2015
Miami, FL
1070
2220
3320
1915
2365
4405
2885
1280
3115
3365
New York, NY
1390
1275
2865
2630
1925
4505
1935
2060
3055
2860
San Francisco, CA
4015
3435
2040
3105
3000
615
3240
5015
4915
810
Seattle, WA
4485
3330
2140
3940
3060
1835
2675
5415
4600
1305
Explanation / Answer
//C program that display the cities
//and ask for source and destination cities
//and print the total distancees
//between cities
//Header files
#include<stdio.h>
#include<conio.h>
int main()
{
//set the size of the array is 11
const int SIZE=11;
//to calculate total distance
int totalDistance=0;
//city index
int index;
//to store source city
int city;
//to store destinaiton city
int destination;
//create a 2 dimensional array of 11 rows and 11 columns
//each diagnonal is set to zero that distance from same city to
//same city is 0
int distance[SIZE][SIZE]={
{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}
};
//character array of size 11 and each character string of size 50
char cities[SIZE][50]={"Atlanta, GA",
"Chicago, IL",
"Denver, CO",
"Houston, TX",
"Kansas City, KS",
"Los Angeles, CA",
"Minneapolis, MN",
"Miami, FL",
"New York, NY",
"San Francis- co, CA",
"Seattle, WA"};
//print cities
printf("Please choose a city: ");
for(index=0;index<SIZE;index++)
printf("%s ",cities[index]);
printf("Exit ");
printf("City :");
//promtp for source city from where the journey starts
scanf("%d",&city);
//add the distance to totalDistance
totalDistance+=distance[city-1][city-1];
//print the distance to same city
printf("Distance =%d miles ", totalDistance);
do
{
printf("Please choose a city: ");
for(index=0;index<SIZE;index++)
printf("%s ",cities[index]);
printf("Exit ");
printf("City :");
//ask for distination city
scanf("%d",&destination);
//then add the distance to total distnce
if(destination>=1 &&destination<=11)
totalDistance=totalDistance+distance[city-1][destination-1];
//print toatl distnce
printf("Distance : %d ", totalDistance);
//if user enters the destination city 12
//then exit from the while loop
}while(destination!=12);
//print total distance
printf("Total Distance : %d ", totalDistance);
//pause the program output
getch();
return 0;
}//end of main method
---------------------------------------------------------------------------------------------------------------------------------------
Sample output:
Please choose a city:
Atlanta, GA
Chicago, IL
Denver, CO
Houston, TX
Kansas City, KS
Los Angeles, CA
Minneapolis, MN
Miami, FL
New York, NY
San Francis- co, CA
Seattle, WA
Exit
City :1
Distance =0 miles
Please choose a city:
Atlanta, GA
Chicago, IL
Denver, CO
Houston, TX
Kansas City, KS
Los Angeles, CA
Minneapolis, MN
Miami, FL
New York, NY
San Francis- co, CA
Seattle, WA
Exit
City :2
Distance : 715
Please choose a city:
Atlanta, GA
Chicago, IL
Denver, CO
Houston, TX
Kansas City, KS
Los Angeles, CA
Minneapolis, MN
Miami, FL
New York, NY
San Francis- co, CA
Seattle, WA
Exit
City :3
Distance : 2120
Please choose a city:
Atlanta, GA
Chicago, IL
Denver, CO
Houston, TX
Kansas City, KS
Los Angeles, CA
Minneapolis, MN
Miami, FL
New York, NY
San Francis- co, CA
Seattle, WA
Exit
City :12
Distance : 2120
Total Distance : 2120
Note: The distance from first city to the selecting cities becomes 2120()+715+1405).
Output shown in the question is incorrect.
Hope this helps you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.