| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I am a complete novice in C++ as this is my first programming class ever. I am completely stuck on this problem:
Develop a progrm which displays all possible ways to break up a $100 bill. It should be formatted as shown: No fifity twenty ten five two one *** *** ***** *** *** *** *** 1 1 1 2 1 2 1 The example shows these add up to 100. Print out the total number of possible combinations. OK, I'm not asking for someone to write this, but I need some direction. Our prof is under the impression that self-learning is the way to teach. I've never seen an example program that prints out in columns like he's asking. Would I use a "for" or "if" or "while" loop or "else"? I really have no clue where to start. I can understand the logic that it should start by printing 2 fifties, then next line 1 fifty, 2 twenties, 1 ten...or am I looking at that the wrong way? ANY help would be appreciated. It would be a lot easier if the prof actually explained this stuff. Thanks. ![]() |
|
#2
|
|||||
|
|||||
|
Printing in columns is easy using the printf() statement. You can specify the minimum length of a variable printed by adding a number between the percent sign (%) and the type (d for an integer, f for a float, etc).
The following example will print all numbers from 0 to 24 in a 5 by 5 square, where every number takes up 2 digits, with one digit in between: cpp Code:
As for the assignment, the best way to do this is write a recursive function, that takes two arguments, a value and a denomination, The function should go through all possible amounts that can be made with that denomination specified, and call itself again, with the remaining value and the next lower denomination. Unless denomination is 1, in which case the current combination should be printed. I know this explanation can be a bit fuzzy if you don't know what recursion is, but in that case, it would be a good thing to go to your teacher and ask him to explain recursion a bit. Hope this gets you on your way
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 275
![]() Last edited by Itsacon : September 28th, 2005 at 04:07 PM. |
|
#3
|
||||
|
||||
|
Ok, I've got a working version, so if you're really stuck, just say.
Be aware that there are 4562 different ways to break up that 100 dollar bill... |
|
#4
|
|||
|
|||
|
Please do post your version. I would like to see if what you have is anything similar to what we have been taught. I've been searching through books for days trying to figure this one out. That number seems awful high for where we are...and I have never heard of recursion, so I'll have to look it up. Thanks for the advice.
Quote:
|
|
#5
|
||||
|
||||
|
Recursion is, simply put, breaking up a problem into smaller, decreasing steps.
For instance, calculating x! (a mathematical expressiong meaning x * x-1 * x-2 *..... * 2 * 1) can easily be done by recursion: I'll post my solution later today, if you still can't figure it out. |
|
#6
|
|||||
|
|||||
|
Ok, here's my solution.
Expanded it a bit to optionally take the amount from the command line, but when called without parameters, it defaults to 100 dollars. See if you can figure out what makes it tick. cpp Code:
|
|
#7
|
||||
|
||||
|
Now do it for Canada... we do'nt have $1 or $2 bills.
![]() Recursion, simple-r put... a function is recursive when it calls itself. Keep in mind, there needs to be a way out... as in Itsacon's code, IF (something) call_myself ELSE print something Otherwise you end up in an infinite loop. |
|
#8
|
||||
|
||||
|
I bet you have 1 and 2 dollar coins...
Same here in europe... Only we call them euros.... |
|
#9
|
|||
|
|||
|
Thanks for the help and explanations! I will go through it with a fine-toothed comb. It looks like we haven't gone over some of this yet. I will discuss this with the prof. Thanks.
|
|
#10
|
||||
|
||||
|
@ Itsacon or MadCowDzz ???
Another side question, How would you substitute the printf, for a cout<< statement or is that even possible to do and still keep the same functionality? Just wondering
![]() |