/* Exercise 5.9 Solution */ #include #include float calculateCharges (float hours); void main () { float h, currentCharge, totalCharges = 0.0, totalHours = 0.0; int i, first = 1; printf ("Enter the hours parked for 3 cars: "); for (i = 1; i <= 3; i++) { scanf ("%f", &h); totalHours += h; if (first) { printf ("%5s%15s%15s\n", "Car", "Hours", "Charge"); /* prevents the header from printing again */ first = 0; } currentCharge = calculateCharges (h); totalCharges += currentCharge; printf ("%5d%15.1f%15.2f\n", i, h, currentCharge); } printf ("%5s%15.1f%15.2f\n", "TOTAL", totalHours, totalCharges); } float calculateCharges (float hours) { float charge; if (hours < 3.0) charge = 2.0; else if (hours < 19.0) charge = 2.0 + .5 * ceil (hours - 3.0); else charge = 10.0; return charge; }