Parking fees

Assignment 

Solution 

prog5.c 

#include <stdio.h>

#include <math.h>

/* implement the logic in the flowchart */

floatcalculateCharges(float hours) {

floataddHours, addCharges, addCost, total;

if (hours <= 3) {

return 2;

}

addHours = hours – 3;

addCharges = ceil(addHours);

addCost = 0.5 * addCharges;

total = addCost + 2;

if (total > 10) {

return 10;

}

return total;

}

int main() {

float hours1, hours2, hours3; /* the input hours */

float charge1, charge2, charge3; /* the charges for each parking */

floattotalHours, totalCharges;

/* ask the user to enter the hours of parking */

printf(“Enter the hours for car 1: “);

scanf(“%f”, &hours1);

printf(“Enter the hours for car 2: “);

scanf(“%f”, &hours2);

printf(“Enter the hours for car 3: “);

scanf(“%f”, &hours3);

charge1 = calculateCharges(hours1);

charge2 = calculateCharges(hours2);

charge3 = calculateCharges(hours3);

totalHours = hours1 + hours2 + hours3;

totalCharges = charge1 + charge2 + charge3;

/* display in a table */

printf(“\nCar\tHours\tCharge\n”);

printf(“%-5d\t%-5.1f\t%.2f\n”, 1, hours1, charge1);

printf(“%-5d\t%-5.1f\t%.2f\n”, 2, hours2, charge2);

printf(“%-5d\t%-5.1f\t%.2f\n”, 3, hours3, charge3);

printf(“%-5s\t%-5.1f\t%.2f\n”, “TOTAL”, totalHours, totalCharges);

return 0;

}