Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,766 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,597 people online right now. Registration is fast and FREE... Join Now!




making change homework help

 
Reply to this topicStart new topic

making change homework help

truthuntold
21 Feb, 2007 - 10:09 PM
Post #1

New D.I.C Head
*

Joined: 21 Feb, 2007
Posts: 2


My Contributions
Hi, I have been trying to get this nested for loop to work but I havent had any success. Here is what I have...
CODE
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
// Variables
const int QUARTERS = 25;
const int DIMES = 10;
const int NICKELS = 5;
const int PENNIES = 1;
int q = 0;
int d = 0;
int n = 0;
int p = 0;
int cents = 0;
int coins = 0;
int total = 0;
int qtotal = 0;
int dtotal = 0;
int ntotal = 0;
int ptotal = 0;


//apperance
system ("cls");
system ("color 1f");
//welcome message & instruction
cout << "Welcome, this program will find all the ways of making change using quarters,\n";
cout << "dimes, nickels, and pennies. In the specificed amount of cents and amount of\n";
cout << "coins.\n\n";
//ask for input of cents and coins
cout << "How many cents would you like to use? ";
cin >> cents;
cout << "\nHow many coins would you like to use? ";
cin >> coins;




for (; cents > q; q++) {
    
   for (; cents > d; d++) {
      
      for (; cents > n; n++) {
                
          for (; cents > p ; p++) {
            
                      
              cout << "q is " <<  q  << "  d is " << d << "  n is " << n << "  p is " <<p <<"  total is " << total << "  coins is " << coins << endl;
         }      
      }
   }
}


    return 0;
}


Here is the assignment detail. I have tried many different thing. I can only get the pennies to count or they go infinite.

For this assignment, you will write a C++ program to find all the ways of making change using quarters, dimes, nickels, and pennies. Your program should prompt the user to find out the amount of money in cents (not dollars), and the maximum number of coins that can be used. Your program will then find all possible combinations of coins to achieve the requested total without using too many coins. It will print out every combination it finds, and then it will print out the total number of ways that it found.

You can use any method you want, but the simplest is to loop through every possible way of combining quarters, dimes, nickels, and pennies, and evaluate each one to see if it meets the restrictions, i.e., if it totals to the right amount and does not exceed the maximum number of coins allowed.

Consider the case where the user asks to find all ways of making change for a dollar using no more than 10 coins. Since a quarter is worth 25 cents, any valid solution will use 0, 1, 2, 3, or 4 quarters, but never any more. For any given coin value, dividing the desired total by the value of the coin tells you the maximum number of times that coin can be used. A 25 cent quarter divides into a dollar (100 cents) 4 times, so there couldn't possibly be more than 4 quarters in a pile of change that totals a dollar.

Likewise, there is a limit to the number of dimes, nickels, and pennies that can be used to make a given amount of money. If your program is finding all ways to make change for a dollar, you should consider every combination of up to 4 quarters, 10 dimes, 20 nickels, or 100 pennies. Those numbers will change depending on the amount the user requests; they are given here just as an example for the case of a dollar.

Comparing the value of each coin to the total amount gives you a limit on how many combinations you have to test. In this example, the number of possible combinations of quarters, dimes, nickels, and pennies is 4*10*20*100 or 80,000 combinations of coins. Not all of those combinations equal 100 cents. For each combination, you add up the value of the coins by multiplying the number of quarters times the value of a quarter (25 cents), the number of dimes times the value of a dime (10 cents), etc. If the total is right, and if the number of coins is not too high (10 in our example), then you can print out the combination you found and increment the total number of solutions that you've found.

To find each possible combination, you need a series of nested loops, meaning loops within loops. The loop that tries out every possible number of quarters has a block of statements that are executed for each iteration of the loop (within curly braces). Inside that block will be another loop that tries every possible number of dimes, executing a block of statements each iteration. Within that block will be yet another loop that tries every possible number of nickels, and within that loop will be a loop that tries every possible number of pennies.

These loops should be for loops, not while loops like we've used before. A for loop follows this form:

for (init; condition; increment) {
body of loop;
}

Where init is usually a statement to initialize a counter, such as "int i = 0". condition is a boolean expression that tells whether the loop should continue (just like in a while loop), for example "i < 10", and increment is a command executed between each iteration, usually to increment the loop variable, for example "i++". A for loop that iterates for every value of i from 0 to 9 would be:

for (int i = 0; i < 10; i++) {
do stuff;
}

which is basically equivalent to:

int i = 0;
while (i < 10) {
do stuff;
i++;
}

for loops exist because looping over a range of values is a really common part of many programs. They are generally used when you use a counter of some kind.

Here is an example of nested for loops that print out all the numbers from 0 to 999, including leading zeros:

for (int hundreds = 0; hundreds < 10; hundreds++) {
for (int tens = 0; tens < 10; tens++) {
for (int ones = 0; ones < 10; ones++) {
cout << hundreds << tens << ones << endl;
}
}
}

Your program will have a similar structure, but instead of looping until the number of quarters reaches some constant value, you will want to loop until the value of the quarters exceeds the total amount of money you are considering.

At the top of your program, you should define the following constant values:

const int QUARTER = 25;
const int DIME = 10;
const int NICKEL = 5;
const int PENNY = 1;

const means that the value is constant, i.e., you cannot change the value after the initial assignment. Using uppercase names is good style for constant values. When considering the value of various coins, you should use the constants given above, e.g., if you have q quarters, the value in cents would be q * QUARTER.

When printing out results, your program should line up the numbers in columns. To do this, you should #include <iomanip> at the top of your program, and use the setw(width) stream manipulator. Using cout << setw(3) << n; will print the value of n using at least 3 characters, padding with leading spaces if necessary.

No pseudocode is provided for this assignment. You may find it helpful to try writing your own pseudocode as a first step.
Examples

Example 1:

Please enter the amount in cents: 100
Please enter the maximum number of coins: 10
quarters: 0, dimes: 10, nickels: 0, pennies: 0, total: 10
quarters: 1, dimes: 6, nickels: 3, pennies: 0, total: 10
quarters: 1, dimes: 7, nickels: 1, pennies: 0, total: 9
quarters: 2, dimes: 2, nickels: 6, pennies: 0, total: 10
quarters: 2, dimes: 3, nickels: 4, pennies: 0, total: 9
quarters: 2, dimes: 4, nickels: 2, pennies: 0, total: 8
quarters: 2, dimes: 5, nickels: 0, pennies: 0, total: 7
quarters: 3, dimes: 0, nickels: 5, pennies: 0, total: 8
quarters: 3, dimes: 1, nickels: 3, pennies: 0, total: 7
quarters: 3, dimes: 2, nickels: 0, pennies: 5, total: 10
quarters: 3, dimes: 2, nickels: 1, pennies: 0, total: 6
quarters: 4, dimes: 0, nickels: 0, pennies: 0, total: 4

Total of 12 ways to make change for 100 cents
using no more than 10 coins

User is offlineProfile CardPM
+Quote Post

Israel
RE: Making Change Homework Help
21 Feb, 2007 - 11:10 PM
Post #2

D.I.C Addict
Group Icon

Joined: 21 Nov, 2004
Posts: 626


Dream Kudos: 175
My Contributions
Well, C/C++ is not my forte`. But I believe the syntax of your loop is off for starters.
CODE
for (; cents > q; q++)


Should be...
CODE
for (cents = 0; cents > q; q++)

You have to initialize the loop (give it a starting place) before it can begin.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Making Change Homework Help
22 Feb, 2007 - 12:02 AM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
Well lets look at the problem on paper... suppose I needed to give 1.53 in change... that is 153 cents, well 25cents goes into 153 6 times with a remainder of 3... 15 dimes with a remainder of 3, 30 nickels with a remainder of 3.... so if I want to find out how many of a given coinage I can give I devide the total by the value of that coin... and and have some remainder left over...

lets say that CENTS = my change

I know that there is a max of CENTS/25 quarters capable of being here... so I want to loop from 0 to CENTS/25 subtracting 25 for each iteration...

That leaves me makeing change for CENTS - NumOfQ *25 with dimes again devide by 10 and loop though

then nickels CENTS - NumOfQ * 25 - NumOfD * 10 divided by 5...

no need to loop for pennies just calculate what the change left is...

Basicly it looks like you need 3 loops (Q, D, N) and then calculate what is left and that is the pennies.

To code something you MUST undstand what you are doing GIGO - Garbage in Garbage out, so if you find you can't code something take out a piece of paper and see if you can solve it there.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Making Change Homework Help
22 Feb, 2007 - 12:15 AM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
BTW you don't NEED to initalize variable in a for-loop, once upon a time programers used things like the code below all the time:
Example:
CODE
for(;;) {
/*print Menu */
/*get Menue Choice */
if (choice<>27)
{
  /*check other choices*/
} else { break };
}

Such programs are ok when your code is a few hundred lines long, but when they get to be HUGE projects with lots of team members working on it, such loops cause all kinds of errors, and errors that can be VERY hard to find. So the those sages who pass on thier wizdom in programing stlye advice tell us not to do it like that, they should know, they tried it. Use the commands and statments as they were intended unless a hack is really the only way to acomplish your goal without rewriting the whole thing.

This post has been edited by NickDMax: 22 Feb, 2007 - 12:16 AM
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Making Change Homework Help
22 Feb, 2007 - 02:26 AM
Post #5

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(NickDMax @ 22 Feb, 2007 - 01:15 AM) *

BTW you don't NEED to initalize variable in a for-loop, once upon a time programers used things like the code below all the time:
Example:
CODE
for(;;) {
/*print Menu */
/*get Menue Choice */
if (choice<>27)
{
  /*check other choices*/
} else { break };
}

Such programs are ok when your code is a few hundred lines long, but when they get to be HUGE projects with lots of team members working on it, such loops cause all kinds of errors, and errors that can be VERY hard to find. So the those sages who pass on thier wizdom in programing stlye advice tell us not to do it like that, they should know, they tried it. Use the commands and statments as they were intended unless a hack is really the only way to acomplish your goal without rewriting the whole thing.

Nick

Very nice description...

Love your work
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Making Change Homework Help
22 Feb, 2007 - 04:21 AM
Post #6

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
hi truthuntold.

in the for loops you've made

for (; cents > q; q++)

you increment the condition which doesn't seem right.
If you increment a condition in a for loop you'll get nowhere.

The thing that you should increment or decrement in this case is cents
You have a nice explanation in your post how to use for loops. Read it again
and then again....and follow NickDMax's's's's's hints.
for(i=0;i<100;i++)
{
read;
think;
read;
}

wink2.gif

This post has been edited by PennyBoki: 22 Feb, 2007 - 04:21 AM
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Making Change Homework Help
22 Feb, 2007 - 01:43 PM
Post #7

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(PennyBoki @ 22 Feb, 2007 - 05:21 AM) *

hi truthuntold.

in the for loops you've made

for (; cents > q; q++)

you increment the condition which doesn't seem right.
If you increment a condition in a for loop you'll get nowhere.


Hi All

Penny, since both values are variables, it can be done this way.

The drawback is the potential to introduce a bug if your program is altering the value of cents in such a way that it never makes the condition false (infinite loop).

In this instance, the logic is based on cents Greater Than Q, so this does make sense, as long as the programmer assures that the bug described above can't occur.

regards


Greg
User is offlineProfile CardPM
+Quote Post

Israel
RE: Making Change Homework Help
22 Feb, 2007 - 08:55 PM
Post #8

D.I.C Addict
Group Icon

Joined: 21 Nov, 2004
Posts: 626


Dream Kudos: 175
My Contributions
Guess I learned something new... blink.gif Like I said C/C++ is not my strong point.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 06:20AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month