Parking Fees Extended
Assignment
Objective:
Write a program that uses arrays and loops
Assignment:
Your parking garage is expanding! Instead of only three cars, you can now have eight cars in the garage at onetime. You could repeat the block of code below in your program 5 more times for the extra cars.
printf(“Enter the hours for car 1: “);
scanf(“%f”, &car1);
totalHours = totalHours + car1;
charge1 = calculateCharges(car1);
totalCharges = totalCharges + charge1;
But you realize by using arrays and for loops you could actually make your program shorter even though theprogram is handling more cars. Instead of using variables car1, car2, car3, charge1, charge2, and charge3declare two floating point arrays: one for cars and one for charges. Use for loops to read in the number of hoursfor each car and to print out the table for the cars. Declare and initialize a global constant namedNUMBER_OF_CARS and use the constant when declaring the arrays and in the conditions of the for loops. That wayif the garage expands again you can just change the constant and the program will handle the new number of cars.
The input prompts and output statements must match the sample execution below.
Hints:
You may use your code or my code for program 5 as a starting point for program 6.
Start by initializing NUMBER_OF_CARS to 3 and make sure your new program duplicates program 5. Then changeNUMBER_OF_CARS to 8 and retest your program.
You should not need to change the function calculateCharges.
Sample Execution:
Enter the hours for car 1: 1.5
Enter the hours for car 2: 4
Enter the hours for car 3: 7.65
Enter the hours for car 4: 2
Enter the hours for car 5: 2.5
Enter the hours for car 6: 3.3
Enter the hours for car 7: 14
Enter the hours for car 8: 20
Car Hours Charge
1 1.5 2.00
2 4.0 2.50
3 7.7 4.50
4 2.0 2.00
5 2.5 2.00
6 3.3 2.50
7 14.0 7.50
8 20.0 10.00
TOTAL 54.9 33.00
My code for program 5:
#include
#include
float calculateCharges(float);
main()
{
float car1, car2, car3;
float charge1, charge2, charge3;
float totalHours = 0.0;
float totalCharges = 0.0;
printf(“Enter the hours for car 1: “);
scanf(“%f”, &car1);
totalHours = totalHours + car1;
charge1 = calculateCharges(car1);
totalCharges = totalCharges + charge1;
printf(“Enter the hours for car 2: “);
scanf(“%f”, &car2);
totalHours = totalHours + car2;
charge2 = calculateCharges(car2);
totalCharges = totalCharges + charge2;
printf(“Enter the hours for car 3: “);
scanf(“%f”, &car3);
totalHours = totalHours + car3;
charge3 = calculateCharges(car3);
totalCharges = totalCharges + charge3;
printf(“\n”);
printf(“Car\t\tHours\t\tCharge\n”);
printf(“1\t\t%.1f\t\t%.2f\n”, car1, charge1);
printf(“2\t\t%.1f\t\t%.2f\n”, car2, charge2);
printf(“3\t\t%.1f\t\t%.2f\n”, car3, charge3);
printf(“TOTAL\t\t%.1f\t\t%.2f\n”, totalHours, totalCharges);
}
float calculateCharges(float h)
{
const float MINIMUM_CHARGE = 2.0;
const float MAXIMUM_CHARGE = 10.00;
/* the garage charges 2.00 for any amount of
time up to 3 hours */
if (h<=3)
return MINIMUM_CHARGE;
/* determine how many hours past 3 hours */
h = h – 3;
/* 0.50 for each hour (or part of an hour) past 3 hours */
float additionalAmount = ceil(h) * 0.50;
float charge = MINIMUM_CHARGE + additionalAmount;
if (charge > MAXIMUM_CHARGE)
return MAXIMUM_CHARGE;
else
return charge;
}
Solution
prog6.c
#include
#include
/* total number of cars (array size) */
constint NUMBER_OF_CARS = 8;
floatcalculateCharges(float);
main()
{
float cars[NUMBER_OF_CARS];
float charges[NUMBER_OF_CARS];
floattotalHours = 0.0;
floattotalCharges = 0.0;
int i;
/* use a for loop to enter the hours and calculate the charges */
for (i = 0; i < NUMBER_OF_CARS; i++)
{
printf(“Enter the hours for car %d: “, i + 1);
scanf(“%f”, &cars[i]);
totalHours = totalHours + cars[i];
charges[i] = calculateCharges(cars[i]);
totalCharges = totalCharges + charges[i];
}
printf(“\n”);
printf(“Car\t\tHours\t\tCharge\n”);
/* use a for loop to display the charges */
for (i = 0; i < NUMBER_OF_CARS; i++)
{
printf(“%d\t\t%.1f\t\t%.2f\n”, i + 1, cars[i], charges[i]);
}
printf(“TOTAL\t\t%.1f\t\t%.2f\n”, totalHours, totalCharges);
}
floatcalculateCharges(float h)
{
const float MINIMUM_CHARGE = 2.0;
const float MAXIMUM_CHARGE = 10.00;
/* the garage charges 2.00 for any amount of
time up to 3 hours */
if (h<=3)
return MINIMUM_CHARGE;
/* determine how many hours past 3 hours */
h = h – 3;
/* 0.50 for each hour (or part of an hour) past 3 hours */
floatadditionalAmount = ceil(h) * 0.50;
float charge = MINIMUM_CHARGE + additionalAmount;
if (charge > MAXIMUM_CHARGE)
return MAXIMUM_CHARGE;
else
return charge;
}