Please Help me with Arrays

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2009
Posts: 1
Reputation: tjackson is an unknown quantity at this point 
Solved Threads: 0
tjackson tjackson is offline Offline
Newbie Poster

Please Help me with Arrays

 
0
  #1
Nov 8th, 2009
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.





  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4.  
  5. float calculateCharges(float);
  6. int NUMBER_OF_CARS = 3;
  7.  
  8. main()
  9. {
  10.  
  11. int x = 1;
  12. float fHours[NUMBER_OF_CARS];
  13. float fCharge[NUMBER_OF_CARS];
  14.  
  15. for(x=1; x<=NUMBER_OF_CARS; x++)
  16. {
  17. printf("Enter number of hours for car %d: ", x);
  18. scanf("%f", &fHours[x]);
  19. }
  20.  
  21. printf("\n\n");
  22. printf("Car\t\tHours\t\tCharge\n");
  23.  
  24. for(x=1;x<4;x++)
  25. {
  26. printf("%d\t\t%.1f\t\t%.2f\n", x, fHours, fCharge);
  27. }
  28.  
  29. }
  30.  
  31. float calculateCharges(float fHours)
  32. {
  33. float fCharge = 0.0;
  34. if(fHours < 3)
  35. {
  36. fCharge = 2.00;
  37. return fCharge;
  38. }
  39.  
  40. else
  41. {
  42. fHours = fHours-3;
  43. fCharge = ceil(fHours) * 0.5;
  44. fCharge = 2.00 + fCharge;
  45.  
  46. if(fCharge > 10)
  47. {
  48. fCharge = 10;
  49. return fCharge;
  50. }
  51.  
  52. else
  53. return fCharge;
  54. }
  55. }
Last edited by peter_budo; Nov 8th, 2009 at 7:36 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: Chilton is an unknown quantity at this point 
Solved Threads: 2
Chilton Chilton is offline Offline
Newbie Poster
 
0
  #2
Nov 8th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 253 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC