#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0

    How do I make this a function?


    Ok... so I basically have the program doing what I need it to but I have to write it as a function and I am getting stuck there. Have a look and let me know what you think. Thanks.

    Code:
    //A parking garage charges a $2.00 minimum fee to park for up to three hours
    //and an additional $0.50 per hour for each hour or part thereof over three 
    //hours. The maximum charge for any given 24-hour period is $10.00. Assume 
    //that no car parks for longer than 24 hours at a time. Write a program that 
    //will calculate and print the parking charges for each of three customers who 
    //parked their cars in this garage yesterday. You should enter the hours 
    //parked for each customer. Your program should print the results in a neat 
    //tabular format, and should calculate and print the total of yesterday's 
    //receipts. The program should use the function calculateCharges to determine 
    //the charge for each customer.
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void )
    {
      int car;
      float minfee = 2.00, maxfee = 10.00 , hours, totalcharge, totalhours, 
            calculateCharge;
    
      totalcharge = 0;
      totalhours = 0;
      car = 1;
    
       while( car < 4 ){ 
    		
    	printf( "enter the hours parked: " );
    	scanf( "%f",&hours );
    			
    	  if( hours <= 3 ) {
    	     calculateCharge = minfee;
             }
    		    else {
    			   calculateCharge = ( (hours - 3) * 0.5 + minfee );
                   }
                      if( calculateCharge > maxfee ) {
    			         calculateCharge = ( maxfee );
                         }
                            car = car;
                            printf( "Car#: %d   ", car );
                            
                               totalhours = hours;
    		                   printf( "Hours: %.2f   ",totalhours );
    
    		                      totalcharge = calculateCharge;
    		                      printf( "Charge: $%.2f\n",totalcharge );
    			            
                                     car = car + 1;                     
       }
      system("PAUSE");	
      return 0;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    float calculateCharge (float hours) {
        float minfee = 2.00, maxfee = 10.00 , calcCharge;
        if( hours <= 3 ) {
            calcCharge = minfee;
        }
        else {
            calcCharge = ( (hours - 3) * 0.5 + minfee );
        }
        
        if( calcCharge > maxfee ) {
            calcCharge = maxfee;
        }
    
        return calcCharge;
    }
    
    int main( void )
    {
        int car = 1;
        float hours;
        while( car < 4 ){ 
            printf( "enter the hours parked: " );
            scanf( "%f", &hours );
            
            printf( "Car#: %d   ", car );
            
            printf( "Hours: %.2f   ",hours );
            
            printf( "Charge: $%.2f\n",calculateCharge(hours) );
            
            car = car + 1;
        }
    
        return 0;
    }

    Comments on this post

    • dwise1_aol disagrees : Now that you have just simply handed him the answer, what did he learn?
    • ptr2void disagrees : You should not have done this.
    • Winters agrees : balance
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0
    Thanks for the answer but a little explanation would be great. If you or someone else can explain. Thanks.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Basically, you look for the code that's used to calculate the charge and you move that over to the function you're creating. Within that code, you look at the variables that are needed to perform that calculation; those will be the parameters for the function. Then back in the calling function, you replace that code with a function call.

    Of course, you also need to be careful about other variables whose values get changed, etc.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0
    Umm... OK. ;)

    I'm slowly getting the hang of this.

    Thanks.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    dwise1_aol disagrees: Now that you have just simply handed him the answer, what did he learn?
    ptr2void disagrees: You should not have done this.
    Sorry, I understand what you mean. Kids these days get ready-made solutions as they have access to internet from a very young age - this retards thinking.

    Will keep this in mind.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    At least he did come back and ask for an explanation, indicating that he does want to learn. But sadly there are far too many of the other kind who just want someone to do their work for them.

IMN logo majestic logo threadwatch logo seochat tools logo