Answer to Question #336314 in C++ for Program Training

Question #336314

Toll roads have different fees at different times of the day and on weekends. Write a function CalcToll() that has three arguments: the current hour of time (int), whether the time is morning (bool), and whether the day is a weekend (bool). The function returns the correct toll fee (double), based on the chart below.

Weekday Tolls

  • Before 7:00 am ($1.15)
  • 7:00 am to 9:59 am ($2.95)
  • 10:00 am to 2:59 pm ($1.90)
  • 3:00 pm to 7:59 pm ($3.95)
  • Starting 8:00 pm ($1.40)

Weekend Tolls

  • Before 7:00 am ($1.05)
  • 7:00 am to 7:59 pm ($2.15)
  • Starting 8:00 pm ($1.10)

Ex: The function calls below, with the given arguments, will return the following toll fees:

CalcToll(8, true, false) returns 2.95

CalcToll(1, false, false) returns 1.90

CalcToll(3, false, true) returns 2.15

CalcToll(5, true, true) returns 1.05


1
Expert's answer
2022-05-02T13:37:13-0400
#include <iostream>
using namespace std;


double CalcToll(int time, bool morning, bool weekend) {
	double price;
	if (not morning)
		time = time + 12;
	if (weekend){
		if (time >= 0 && time < 7){
			price = 1.05;
		}	
		if (time >= 7 && time < 20){
			price = 2.15;
		}
		if(time >= 20 && time < 24){
			price = 1.10;
		}
	}
	else{
		if (time >= 0 && time < 7){
			price = 1.15;
		}	
		if (time >= 7 && time < 10){
			price = 2.95;
		}
		if(time >= 10 && time < 15){
			price = 1.90;
		}
		if (time >= 15 && time < 20){
			price = 3.95;
		}
		if(time >= 20 && time < 24){
			price = 1.40;
		}
	}	
    return price;
}


int main() {
    int hour;
    bool m,w;
    cout << "Please enter current hour of time: ";
    cin >> hour;
	cout << "Whether the time is morning (1 - yes, 0 - no): ";
	cin >> m;
	cout << "Whether the day is aweekend (1 - yes, 0 - no): ";
	cin >> w;
	cout << "\n********************************";
    cout << "\n*  The correct toll fee: " << CalcToll(hour,m,w) << "  *";
    cout << "\n********************************";
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS