MATLAB question A parking garage charges a $2.00 minimum fee to park for up to t
ID: 3817236 • Letter: M
Question
MATLAB question
A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an additional $0.50 per hour for each hour or part thereof in excess of three hours. The maximum charge for any 24-hour period is $10.00. This garage does not allow parking for longer than 24 hours at a time. Write a function garage_yourLastName to calculate the parking charges for each customer. Also write how this function would be called in your main program garageTest_yourLastName.m
Input Data : Parking hours Output Data : Total charge
1. Write the function header, remember the function name must match name of the m-file!! 2. The function garage takes in one parameter, parkingHours. And after calculation, it returns one result, totalCharge. 3. Create another file garageTest_yourLastName.m (this is the main program used to test the garage function). 4. Include the header comments and clear the windows. 5. In this program, use input to read in the customer’s name and parking time. 6. While the parking time is less than or equal to 24 hours, call the garage function to return the total charge and then use fprintf to print out customer’s name and total charge. 7. If the parking time is greater than 24 hours, then print out an error message. (See sample output on next page)
Sample Output: Enter your name: Jane Doe Enter number of hours: 0.5
Jane Doe, you expect to park for 0.5 hours, your fee will be $2.00.
Enter your name: Mary Smith Enter number of hours: 10.5 Mary Smith, you expect to park for 10.5 hours, your fee will be $6.00. Enter your name: John Jones Enter number of hours: 25 John Jones, we are sorry, but you cannot park here for more than 24 hours!
What I have so far:
Function:
function total = garage_lastname (hours)
if hours <= 3
total=2.00
else
total = 2.00 + (hours-3)*0.50;
if total > 10
????
end
end
end
In main program:
name = input(' Enter your name: ', 's');
parkingTime = input('Enter number of hours: ');
% Use a while loop to call the function and display output
while parkingTime <= 24
????
fprintf (%s, we are sorry, but you cannot park here for more than 24 hours! ',name);
name = input (' Enter your name: ', 's');
parkingTime = input ('Enter number of hours: ');
end
I'm almost there, just a little confused. Specifically where the question marks (???) are. I am not 100% sure that what I have so far is correct.
Thanks for the help!
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace parking;
//To calculate charges of parking
float calculateCharges(float hours)
{
float total_charge = 2.0;
if(hours > 3){
total_charge += (ceil(hours) - 3) * 0.5;
}
if(total_charge > 10.00){
total_charge = 10.00;
}
return total_charge;
}
void printReport(float car1, float car2, float car3)
{
float car1_charges = calculateCharges(car1);
float car2_charges = calculateCharges(car2);
float car3_charges = calculateCharges(car3);
cout << fixed
<< setprecision(2)
<< setfill('0');
cout << "Car Hours Charge" << endl
<< "1 " << car1 << " $" << car1_charges << endl
<< "1 " << car2 << " $" << car2_charges << endl
<< "1 " << car3 << " $" << car3_charges << endl
<< "TOTAL " << car1 + car2 + car3 << " $"
<< car1_charges + car2_charges + car3_charges << endl;
}
int main()
{
float car1, car2, car3;
cout << "PARKINGMETER.app" << endl
<< "----------------" << endl
<< "Car #1: ";
cin >> car1;
cout << "Car #2: ";
cin >> car2;
cout << "Car #3: ";
cin >> car3;
printReport(car1, car2, car3);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.