944,184 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 433
  • C RSS
Nov 8th, 2009
0

Please Help me with Arrays

Expand Post »
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)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tjackson is offline Offline
1 posts
since Nov 2009
Nov 8th, 2009
0
Re: Please Help me with Arrays
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.
Reputation Points: 34
Solved Threads: 16
Junior Poster
Chilton is offline Offline
106 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: About Outp function in conio.h for c or c++?
Next Thread in C Forum Timeline: gcc warning with -Wwrite-strings flag





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC