# Thread: help with arrays and loops

1. ## 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. 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.

• 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.

4. 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. and how do i total all of the 8 inputs for hours?

6. anybody?

7. The same way you'd do it on paper.

total = this1 + this2 + this 3 ...

Did you even try?

Quzah.

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

10. 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. 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.)

12. > 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.

13. 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. 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. 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.)