| ||||||||||||||||||||||||||
![]() |
|
|
«
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: 562
![]() 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
![]() |
|
#11
|
||||
|
||||
|
Quote:
Then just use: cout<< "text" << variable_name << "more text" << another_var_name << "etc....";
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#12
|
|||||
|
|||||
|
B-Con, that way, you can't define the width of the variables (which I suppose is what Geo meant)
This can be done in several ways: cpp Code:
|
|
#13
|
||||
|
||||
|
Thanks for your help Itsacon, & Bcon. That is exactly what I was looking for Itsacon. When using C functions like printf I know is available to C++ but like I said I started learning in C++ and that's what most of the books taught first other than printf, so for the past four months of my learning I have been using cout<< for all my programs output. I am just now going into C stuff because I realized those books I read left out a lot about C and being that C++ is just its superset I think it would have been better of me to learn C first
But no biggie. Thanks again ya'll, " who needs a mentor when I got all you guyz to help me out when ever I need it " <Thanks> |
|
#14
|
||||
|
||||
|
You may have noticed that on most of my examples, I use pure C syntax, I do this for several reasons:
|
|
#15
|
||||
|
||||
|
I'm back
insomnia has me again lol. Well, let me ask you this, what language do you use to program in and what types of things do you program, just out of curiosity. The new book I got goes through C examples first so you have a good base when they start to teach you C++, its a little repeditive but it seems, like you say, easier. |
|
#16
|
||||
|
||||
|
For my work, I program web interfaces and database applications. Mostly in PHP/MySQL
For my hobby, I do other people's homework on devarticles.com, in all kinds of lanquages ![]() For my study, I do microcontroller development in assembler, C, C++ etc. Also some normal (PC) applications, mostly in C/C++. |
|
#17
|
||||
|
||||
|
LOL to that second one.
Sounds interesting, I hope to be there some day =). Im not stalking you or nothing but I have just been having fun tracking your posts, mostly because a lot of my questions lately have been either directed to you or answered by you lol. |
|
#18
|
||||
|
||||
|
Gives me something to do...
Want to join my fanclub? ![]() |
|
#19
|
||||
|
||||
|
LOL, but seriously really lol.
But for real this time its bed time for me or I think I'm gonna have an aneurysm. |
|
#20
|
||||
|
||||
|
Congratulations, you have been added to the fanclub...
Perks: free answers to your questions... ![]() |
|
#21
|
||||
|
||||
|
ALRIGHT Im in
Oh ya bed... forgot ![]() It's almost seven AM now might as well suck it up and try to make it through the day now. ![]() |
|
#22
|
||||
|
||||
|
Auch... anything important planned for today?
BTW: you may pronounce your membership in your own signature of course... |
|
#23
|
||||
|
||||
|
Na, not much planned today. Most of the same stuff. I have the luxury right now of concentrating on school, but all the classes I take so far aren't about programming so they kinda take a back seat to my little independent study of the C/C++ language. That's why I'm always here sucking up all the information I can get, you guys are like my secondary Professors right now.
Quote:
SWEET BTW: Does your fan club have groupies... ![]() LMBO so hard when I saw your signature, I think I wet myself!! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > 2 New To have a Clue |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|