A parking garage charges a $2.00 minimum fee to park for up to three hours and a
ID: 642395 • Letter: A
Question
A parking garage charges a $2.00 minimum fee to park for up to three
hours and an additional $0.50 per hour for each hour or part thereof over three hours. The maximum
charge for any given 24-hour period is $10.00. Assume that no car parks for longer than 24 hours
at a time. Write a program that will calculate and print the parking charges for each of three customers
who parked their cars in this garage yesterday. You should enter the hours parked for each
customer. Your program should print the results in a tabular format, and should calculate and print
the total of yesterday's receipts. The program should use the function calculateCharges to determine
the charge for each customer. Your outputs should appear in the following format:
Car Hours Charge
1 1.5 2.00
2 4.0 2.50
3 24.0 10.00
TOTAL 29.5 14.50
Explanation / Answer
*/ #include #include double calculateCharges( double hours ); /*
function prototype */ int main() { double h; /* number of hours for current car */
double currentCharge; /* parking charge for current car */
double totalCharges = 0.0; /* total charges */ double totalHours = 0.0; /*
total number of hours */ int i; /* loop counter */ int first = 1; /*
flag for printing table headers */ printf( "Enter the hours parked for 3 cars: " ); /*
loop 3 times for 3 cars */ for ( i = 1; i <= 3; i++ ) { scanf( "%lf", &h ); totalHours += h; /*
add current hours to total hours */ /* if first time through loop, display headers */
if ( first ) { printf( "%5s%15s%15s ", "Car", "Hours", "Charge" ); /*
set flag to false to prevent from printing again */ first = 0; } /* end if */ /*
calculate current car's charge and update total */ totalCharges += ( currentCharge = calculateCharges( h ) ); /*
display row data for current car */ printf( "%5d%15.1f%15.2f ", i, h, currentCharge ); } /* end for */ /*
display row data for totals */ printf( "%5s%15.1f%15.2f ", "TOTAL", totalHours, totalCharges ); return 0; /*
indicate successful termination */ } /* end main */ /*
calculateCharges returns charge according to number of hours */
double calculateCharges( double hours ) { double charge; /* calculated charge */
/* $2 for up to 3 hours */ if ( hours < 3.0 )
{ charge = 2.0; } /* end if */ /*
$.50 for each hour or part thereof in excess of 3 hours */ else if ( hours < 19.0 )
{ charge = 2.0 + .5 * ceil( hours - 3.0 ); } /*
end else if */ else
{ /* maximum charge $10 */ charge = 10.0; } /*
end else */ return charge; /* return calculated charge */ }
/* end function calculateCharges */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.