# arrays and loops

This is a discussion on arrays and loops within the C Programming forums, part of the General Programming Boards category; Ive already written this code but just to help you understand, the original problem was this: A parking garage charges ...

1. ## arrays and loops

A parking garage charges a \$2.00 minimum fee to park for up to 3 hrs. The garag charges an additional .50c per hour for each hour or any part of an hour in excess of 3 hrs. the maximum charge for any given 24 hr period is \$10. Assume that no car parks for longer than 24 hours at a time. write a program that will calculate and print the parking charger for each of 3 customers who parked their cars in the garage yesterday. you should enter the hours parked for each customer. The program should print the results in a table and should calculate and print the total of yesterdays receipt. write and use a function named calculateCharges to determine the charge for each customer. The function should accept the number of hrs that a customers vehicle was parked and return the amount owed by the customer.

but now we are dealing with 8 cars and Instead of using variables car1, car2, car3, charge1, charge2, and charge3 declare two floating point arrays: one for cars and one for charges. Use for loops to read in the number of hours for each car and to print out the table for the cars. Declare and initialize a global constant named NUMBER_OF_CARS and use the constant when declaring the arrays and in the conditions of the for loops. That way if the garage expands again you can just change the constant and the program will handle the new number of cars.

So I need help with implementing arrays and loops into my code

this is my original code

Code:
```
#include <stdio.h>
#include <math.h>

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;```

2. So I need help with implementing arrays and loops into my code
Great. Where's your attempt at arrays and loops?

3. so how do my initializations look? does this seem right based on what im tryin to do in the assignment?

Code:
```#include <stdio.h>
#include <math.h>

float calculateCharges(float);
const int NUMBER_OF_CARS= 8;

main()
{
float cars[NUMBER_OF_CARS];
float charges[NUMBER_OF_CARS];
int car;
float charge;
float hours;
float totalHours = 0.0;
float totalCharges = 0.0;```