Q: Draw the hierarchy chart and design the logic for a program that calculates t
ID: 3767030 • Letter: Q
Question
Q: Draw the hierarchy chart and design the logic for a program that calculates the projected cost of an automobile trip. Assume that the user's car travels 20 miles per gallon of gas. Design a program that prompts the user for a number of miles driven and a current cost per gallon. The program computes and displays the cost of the trip as well as the cost if gas prices rise by 10 percent. The program accepts data continuously until 0 is entered for the number of miles. Use appropriate modules, including one that displays "End of program" when the program is finished.
I have already finished making the actual program for this question, but I am having trouble designing the hierarchy chart. My classes uses Python if that helps.
Explanation / Answer
#include <iostream>
#include <iomanip> // need to include this header file for formatting i/o
#include <string> // need to include this header file to handle strings properly
using namespace std;
// Global variables declared here; these have scope (visibility) in all modules
// declared in this file
int numMilesDriven = 0;
double costPerGallon = 0;
double tripCost = 0;
double newTripCost = 0;
// Named constants declared/initialized here
const double INCREASE = 0.10;
const int MILES_PER_GALLON = 20;
const string MILES_PROMPT = "Enter the number of miles driven: ";
const string GALLON_PROMPT = "Enter the current cost per gallon: ";
const string END_LINE = "End of program....";
void houseKeeping(); // Function prototype
void detailLoop(); // Function prototype
void endOfJob(); // Function prototype
// MODULE: main()
// This is the main() function which acts primarily as a dispatch module.
// The houseKeeping(), detailLoop() and endOfJob() modules are all called from
// within the main() module.
int main()
{
cout << fixed << setprecision(2) << endl; // I/O formatting here
houseKeeping();
while(numMilesDriven!=0)
{
detailLoop();
}
endOfJob();
// TODO : The main iteration in the program is a while loop; a sentinel value
// of zero is used to terminate the loop. Call the detailLoop() from within this
// while loop.
// TODO: Output the ending output statement
return 0;
}
// MODULE: houseKeeping()
// This module primes the input to our main loop
void houseKeeping()
{
// TODO : prime the input to our main loop
cout << MILES_PROMPT;
cin >> numMilesDriven;
return;
}
// MODULE: detailLoop()
// This module makes the sausage (i.e., prompts for cost of gallon of gas;
// and calculates the trip cost based on miles driven. Also calculates
// the trip cost if the cost per gallon of gas increases by 10%.
void detailLoop()
{
//TODO: Prompt for the cost of a gallon of gas
cout << GALLON_PROMPT;
cin >> costPerGallon;
tripCost = numMilesDriven / MILES_PER_GALLON * costPerGallon;
cout << tripCost;
// TODO: Calculate the trip cost and the trip cost if the cost
// of the gas goes up by 10%
newTripCost = tripCost * (1 + INCREASE);
cout << newTripCost;
// TODO: output the calculated trip costs to the user
cout << MILES_PROMPT;
cin >> numMilesDriven;
// TODO: re-prime your input
return;
}
// MODULE: endOfJob()
// This module prints the ending message when the while loop in main is exited.
void endOfJob()
{
// TODO: Output the ending output statement in this module
cout << END_LINE;
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.