help with arrays and loops

This is a discussion on help with arrays and loops within the C Programming forums, part of the General Programming Boards category; A parking garage charges a $2.00 minimum fee to park for up to 3 hrs. The garag charges an additional ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    35

    help with 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 8 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.

    Im not sure exactly how to make my table work

    this is what i have so far

    Code:
    #include <stdio.h>
    #include <math.h>
    
    float calculateCharges(float);
    const int NUMBER_OF_CARS= 8;
    
    main()
    {
     int cars[NUMBER_OF_CARS]= {1,2,3,4,5,6,7,8};
     float charges[NUMBER_OF_CARS];
     int i;
     float hours= 0.0;
     float charge= 0.0;
     float totalHours= 0.0;
     float totalCharges= 0.0;
    
     for(i=0; i<NUMBER_OF_CARS; i++)
     {
      printf("Enter the hours for car %d:\n", cars[i]);
      scanf("%.1f",&hours);
      totalHours = totalHours + cars[i];
      charge[i] = calculateCharges[i];
      totalCharges = totalCharges + charge[i]
        
      printf("\n");
      printf("Car\t\t\Hours\t\t\Charge\n");
      printf("[i]\t\t\%.1f\t\t\%.2\n", totalHours[i], totalCharges[i]);   
     }
     
       printf("TOTAL\t\t\%.1f\t\t\%.2f", totalHours, total Charges);
        
       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;
        
        /* 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
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Welcome to the forum! (as Adak would say, don't worry if you don't know who Adak is yet, he will probably pop-up around here... muhahaha)

    First changes:

    1) main() should be int main(void) and should return 0 at the end of the function.
    2) Be very very careful using scanf. Most likely you have an endline character trailing around.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Some comments:
    • You appear to have a function inside a function; I think you're missing at least one closing curly brace.
    • printf() won't know what "%.2" is in
      Code:
      printf("[i]\t\t\%.1f\t\t\%.2\n", totalHours[i], totalCharges[i]);
    • You're missing a semicolon here:
      Code:
        totalCharges = totalCharges + charge[i]
    • In your first for loop you read the user's input into the variable hours, but then you never do anything with this value.
    • In calculateCharges() you seem to have the same code duplicated twice, or essentially the same code written slightly differently. What exactly are you trying to do? Why don't you pick one of these versions and go with it? Do you actually understand what is happening in that code?
    • totalHours and totalCharges are declared as floats, and in some places you use them as such; but in other places you use them as arrays. (And once there's actually a space in the name!) You have to decide whether your variables are arrays or not and use them consistently.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    ok so as far as the totalcharges and totalhours goes, do i need to use them as arrays or floats since they are depending on the total sum of input values?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    and how do i total all of the 8 inputs for hours?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    anybody?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The same way you'd do it on paper.

    total = this1 + this2 + this 3 ...

    Did you even try?

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    how does this look? or is it totally off?

    Code:
    #include <stdio.h>
    #include <math.h> 
     
    float calculateCharges(float);
    const int NUMBER_OF_CARS= 8;
    
    main()
    { 
     int cars[NUMBER_OF_CARS]= {1,2,3,4,5,6,7,8};
     float charges[NUMBER_OF_CARS];
     int i;
     float hours= 0.0;
     float hourscar[i]=0.0;
     float totalHours= 0.0;
     float totalCharges= 0.0;
      
     for(i=0; i<NUMBER_OF_CARS; i++)
     {
      printf("Enter the hours for car %d:\n", cars[NUMBER_OF_CARS]);
      scanf("%.1f",&hours);
      totalHours = totalHours + hourscar[i];
      car[i]charge = calculateCharges[i];
      totalCharges = totalCharges + car[i]charge;
     }

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    What does your compiler say (it would tell you in a few seconds)

    What results do you get when you run the program? Do they match the answer you get if you work it out on paper.

    Test with a trivial example of only 2 or 3 cars - you can easily work out the paper answer for that. If your program gives the same answer, you might be onto something.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You probably want #define or enum to define symbolic constant.

    Code:
    #define NUMBER_OF_CARS     8
    
    or 
    
    enum { NUMBER_OF_CARS = 8 };
    Your code will compile fine with C99 compiler. In that case take note that you are using VLA (Variable Length Array).

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    ok so here is my program now

    Code:
    #include <stdio.h>
    #include <math.h>
    
    float calculateCharges(float);
    const int NUMBER_OF_CARS= 8;
    
    main()
    {
     int cars[NUMBER_OF_CARS]= {1,2,3,4,5,6,7,8};
     float charges[NUMBER_OF_CARS];
     float hours=0.0;
     float totalHours= 0.0;
     float totalCharges= 0.0;
     int i;
    
     for(i=0; i<NUMBER_OF_CARS; i++)
     {
      printf("Enter the hours for car %d:\n", cars[NUMBER_OF_CARS]);
      scanf("%.1f",&hours);
      totalHours = totalHours + hours;
      charges[i] = calculateCharges(hours);
      totalCharges = totalCharges + charges[i];
    
      printf("\n");
      printf("Car\t\t\Hours\t\t\Charge\n");
      printf("%d\t\t\%.1f\t\t\%.2\n", cars[i], hours, charges[i]);
     }
        printf("TOTAL\t\t\%.1f\t\t\%.2f", totalHours, totalCharges);
    
       float calculateCharges(float hours)
       {
        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;
       }
    and these are my error messages and ive tried fixing them but nothing changes

    pgm39prog6.c:25:10: warning: unknown escape sequence '\H'
    pgm39prog6.c:25:10: warning: unknown escape sequence '\C'
    pgm39prog6.c:30: error: `totalHours' was not declared in this scope
    pgm39prog6.c:30: error: `totalCharges' was not declared in this scope
    pgm39prog6.c:30: error: ISO C++ forbids declaration of `printf' with no type
    pgm39prog6.c:30: error: `int printf' redeclared as different kind of symbol
    /usr/include/iso/stdio_iso.h:179: error: previous declaration of `int
    printf(const char*, ...)'
    pgm39prog6.c:30: error: initializer list being treated as compound expression
    pgm39prog6.c: In function `float calculateCharges(float)':
    pgm39prog6.c:38: error: `h' undeclared (first use this function)
    pgm39prog6.c:38: error: (Each undeclared identifier is reported only once for
    each function it appears in.)
    Last edited by pmooney12; 11-24-2010 at 03:10 PM.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > printf("Car\t\t\Hours\t\t\Charge\n");
    Try not going overboard with the \

    > printf("TOTAL\t\t\%.1f\t\t\%.2f", totalHours, totalCharges);
    Which function is this in?


    Try this
    When it compiles, make at most 3 lines of change before pressing compile again.
    If it stops compiling, then look carefully at what your most recent edit was, and think about what you tried to do.


    Bashing the keyboard for several hours, then wondering why you have a page full of errors is counter productive.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    ok so is there anyone on this board that will actually answer my questions with a helpful answer instead of a smart one? i am very new to c programming so smart remarks really just dont help. nobody is making you respond.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    C'mon!

    They didn't serve it up to you on a silver platter, but it was at least a platter (as did your compiler):

    Code:
      printf("Car\t\t\Hours\t\t\Charge\n");
      printf("%d\t\t\%.1f\t\t\%.2\n", cars[i], hours, charges[i]);
     }
        printf("TOTAL\t\t\%.1f\t\t\%.2f", totalHours, totalCharges);
    When Salem notes that you should knock off some of the \ stuff, he means just that -- prick up those ears!

    The compiler also is plainly saying it doesn't recognize the \H \C, etc char's in your format.

    So get rid of \H and \C and other non C char combo's you have.

    This is the escape sequence for a tab: \t

    and that is ALL of it.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    Code:
    #include <stdio.h>
    #include <math.h>
    
    float calculateCharges(float);
    const int NUMBER_OF_CARS= 8;
    
    main()
    {
     int cars[NUMBER_OF_CARS]= {1,2,3,4,5,6,7,8};
     float charges[NUMBER_OF_CARS];
     float hours=0.0;
     float totalHours= 0.0;
     float totalCharges= 0.0;
     int i;
    
     for(i=0; i<NUMBER_OF_CARS; i++)
     {
      printf("Enter the hours for car %d:\n", cars[NUMBER_OF_CARS]);
      scanf("%.1f",&hours);
      totalHours = totalHours + hours;
      charges[i] = calculateCharges(hours);
      totalCharges = totalCharges + charges[i];
    
      printf("\n");
      printf("Car\t\tHours\t\tCharge\n");
      printf("%d\t\t%.1f\t\t%.2\n", cars[i], hours,
      charges[i]);
     }
    
      printf("TOTAL\t\t%.1f\t\t%.2f", totalHours, totalCharges);
    
     float calculateCharges(float hours)
    {
     float cHours=ceil(hours);
     float charge=0.0;
    
      if (cHours <= 3.0)
       charge=2.00;
      else if (cHours>3.0 && cHours <= 17.0)
        charge= ((cHours-3.0)*0.5)+2.0;
      else
        charge= 10.0; 
      return charge;
    }
    and this is my error message now
    pgm39prog6.c: In function `int main()':
    pgm39prog6.c:33: error: parse error before `{' token
    pgm39prog6.c:37: error: `cHours' undeclared (first use this function)
    pgm39prog6.c:37: error: (Each undeclared identifier is reported only once for
    each function it appears in.)
    pgm39prog6.c:44: warning: return to `int' from `float'
    pgm39prog6.c:44: warning: argument to `int' from `float'


    i dont see why errors on line 44 are happening?
    Last edited by pmooney12; 11-24-2010 at 03:37 PM.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

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

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