Start New Discussion within our Software Development Community

I am new to c programming. I am trying to write a parking garage program that uses arrays to find the charge for each car and the total charge. Here is what I have so far. Please help me in any way that you can.

#include <stdio.h>
#include <math.h>


float calculateCharges(float);
int NUMBER_OF_CARS = 3;

main()
{

  int x = 1;
  float fHours[NUMBER_OF_CARS];
  float fCharge[NUMBER_OF_CARS];

  for(x=1; x<=NUMBER_OF_CARS; x++)
   {
    printf("Enter number of hours for car %d: ", x);
    scanf("%f", &fHours[x]);
   }

  printf("\n\n");
  printf("Car\t\tHours\t\tCharge\n");

  for(x=1;x<4;x++)
  {
  printf("%d\t\t%.1f\t\t%.2f\n", x, fHours, fCharge);
  }

}

float calculateCharges(float fHours)
{
  float fCharge = 0.0;
  if(fHours < 3)
  { 
    fCharge = 2.00;
    return fCharge;
  }   

  else
  {
    fHours = fHours-3; 
    fCharge = ceil(fHours) * 0.5;
    fCharge = 2.00 + fCharge;
    
    if(fCharge > 10)
    {  
      fCharge = 10;
      return fCharge;
    }

    else
      return fCharge;
   }
}

In C, arrays start with 0. In your case, you'll always be adding some data out of the preallocated boundary you've set when declaring your array. Also, your code is confusing the way it's structured there. Use the code tags to situate your work better.

Here's hoping it helps.

This question has already been answered. Start a new discussion instead.