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. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    35

    arrays and loops

    Ive already written this code but just to help you understand, the original problem was this:
    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. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    So I need help with implementing arrays and loops into my code
    Great. Where's your attempt at arrays and loops?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    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;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with for loops and arrays
    By Skeeter in forum C Programming
    Replies: 6
    Last Post: 10-23-2009, 04:33 PM
  2. Help with Arrays and For loops
    By Phil- in forum C++ Programming
    Replies: 5
    Last Post: 09-07-2009, 11:34 AM
  3. Help using arrays and loops for math
    By LLINE in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2008, 05:09 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. loops and arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-03-2001, 04:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21