User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 427,177 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,270 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 2003 | Replies: 5
Reply
Join Date: Jul 2005
Posts: 1
Reputation: cherryluscious is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cherryluscious cherryluscious is offline Offline
Newbie Poster

Help Need help with programming!

  #1  
Jul 1st, 2005
Hi everyone,
I need help writing a program.. I have no idea how to start off ...can you please help me...I am very lost Thank you soo much if you can help because I am extremely confused.

A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an additional $0.50 per hour or part thereof in excess of 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 calculates and prints the parking charges for each of three customers who parked their cars in this garage yesterday. You should enter the number of hours parked for each customer. Your program should print the results in a net 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.

-Use a for loop to prompt the user for the number of hours parked for each of the three customers.

-Declare variables to store the total number of hours and the total charges for each customer

-The variable for all charges and numbers of hours should be of type double

-Function calculateCharges should use a nested if/else structure to determine the customer charge.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Troy
Posts: 1,277
Reputation: Rashakil Fol has a spectacular aura about Rashakil Fol has a spectacular aura about 
Rep Power: 7
Solved Threads: 36
Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Salamander Man

Re: Need help with programming!

  #2  
Jul 1st, 2005
I recommend starting by giving yourself input and calculating the results by hand. Ask yourself, "What steps am I doing to solve this problem?" Then write down on paper exactly what one does to solve the problem.

If you can describe the problem completely, then writing your computer program is just a matter of translation. This is a good programming strategy for when you are first starting out.
Reply With Quote  
Join Date: Jul 2005
Posts: 4
Reputation: xshashiy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xshashiy xshashiy is offline Offline
Newbie Poster

Re: Need help with programming!

  #3  
Jul 6th, 2005
hi here is the solution for your problem.

#include "stdio.h"
#define NUM_CUSTOMERS 3
main()
{
	int num_hours ,customers;
	float charge_min = 2.0,rate = 0.5 , charge_max = 10.;
	float calculate_charge;

	for(customers=1;customers<= NUM_CUSTOMERS ; ustomers++)
	{
		printf("enter the number of hours\n");
		scanf("%d",&num_hours);
		if(num_hours<=3)
		calculate_charge = charge_min;
		else
		{ if (num_hours<19)
			calculate_charge = charge_min + rate*(num_hours -3);
		  else calculate_charge = 10.;
		}
	printf("charge = %f\n",calculate_charge);
	} // end of for loop.
}// end of main.
<< moderator edit: added [code][/code] tags >>

run the above code and get the disired result . it wont printf in tabular formate.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,333
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Need help with programming!

  #4  
Jul 6th, 2005
>hi here is the solution for your problem.
Wonderful, yet another helpful bozo who's willing to teach beginners to be ignorant and rely on others to do their work for them.

>run the above code and get the disired result
Maybe, but then again, maybe not. Your entire program is undefined, and even if it weren't, it's littered with poor practices. I suggest you learn C before trying to teach it. Here are a few of the problems:

>#include "stdio.h"
It's conventional to use angle brackets for standard library headers and double quotes for everything else. When you use double quotes for standard headers, it suggests that you haven't used C long enough to know how code is expected to look. Also, most implementations search different locations first depending on whether <> or "" is used. By using "stdio.h", you may be forcing the implementation to unsuccessfully search a bunch of places before actually finding stdio.h. On the other hand, <> is more likely to search the correct path first.

>main()
This is correct:
int main ( void )
>float
There's no reason to use float. In fact, float may be less efficient than double, which is the default for floating-point constants. If you really think you're smart and float is the best choice, append f or F onto the end of every floating-point constant to be strictly correct.

>for(customers=1;customers<= NUM_CUSTOMERS ; ustomers++)
The only excuse for this is sheer stupidity. Aside from the obvious syntax error of using "ustomers", starting a loop at 1 rather than 0 and using <= instead of < defeats the concept of asymmetric bounds to make correctness checking simple and it encourages fencepost errors.

>scanf("%d",&num_hours);
I don't care how inexperienced someone is, you always set a good example by testing input functions for errors and end-of-file. There's no excuse to use a bare scanf, and the correct usage is something like this:
if ( scanf ( "%d", &num_hours ) != 1 ) {
  /* Handle error or EOF */
}
>if(num_hours<=3)
"Up to" means "up to, but not including". So you're actually charging the minimum for four hours rather than three.

>}// end of main.
This is where the undefined behavior is. You say you're going to return int, but you don't return anything.

On top of all of that, ignoring poor formatting, you "solved the problem" but failed to meet most of the requirements. So not only did you break the rules by doing the OP's homework, you did it with a very bad solution.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jun 2005
Location: Troy
Posts: 1,277
Reputation: Rashakil Fol has a spectacular aura about Rashakil Fol has a spectacular aura about 
Rep Power: 7
Solved Threads: 36
Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Salamander Man

Re: Need help with programming!

  #5  
Jul 6th, 2005
Originally Posted by Narue
>main()
This is correct:
int main ( void )

int main() is also acceptable, at least with C99.

Originally Posted by Narue
>}// end of main.
This is where the undefined behavior is. You say you're going to return int, but you don't return anything.

Note that he didn't actually say he was going to return int ;-)
With the C99 standard, if main has no return statement, then return 0 is assumed. Portability to older compilers is of course desirable though.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,333
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Need help with programming!

  #6  
Jul 6th, 2005
>int main() is also acceptable, at least with C99.
With C89 as well, but it's not semantically consistent with K&R-style function declarations. Rather than try to explain that an empty parameter list is okay in a definition but grievously broken in a declaration, it's better to simply recommend void.

>Note that he didn't actually say he was going to return int ;-)
Yes, he did. Since he's obviously compiling as C89, int is assumed when no return type is stated explicitly.

>With the C99 standard, if main has no return statement, then return 0 is assumed.
With the lack of C99 implementations, relying on a C99 feature that would be undefined in C89 is the height of stupidity.

>Portability to older compilers is of course desirable though.
You say this as if I couldn't grab half a dozen modern and popular compilers and fail to compile the simplest of C99 programs on all of them.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 9:34 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC