changing a US dollar bill with coins

Hello,

i was working on a small program to find how many ways you can break a dollar. i was able to code a simple version that randomly chooses a coin value from an array and add up to a dollar while keeping count of the coins used. How do i run this code to find all possible combinations?

I am new and as you can see from my code i am not sure how to run thru and output all possibility.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

#include <ctime>
#include <iostream>
#include <cstdlib>

using namespace std;



int main()
{
	srand(time(NULL));
    double coins[4] = {0.25, 0.01, 0.10, 0.05};
    double dollar = 1.00;
    double change = 0.00;
    double coin = 0.00;
    
    double QuarterCount = 0.00, PennyCount = 0.00, 
            DimeCount = 0.00, NickleCount = 0.00;
        
    while (change < dollar )
    {
        coin = coins[0 + rand() % 4];
        

        
        if ( (change+coin) <= dollar && change != dollar)
        {
            change += coin;
            
            if (coin == .25)
                QuarterCount++;
            if (coin == .10)
                DimeCount++;
            if (coin == .05)
                NickleCount++;
            if (coin == .01)
                PennyCount++;
            
        }


    }
    cout<<"****************** Change = $"<<change<<" ******************"<<endl;
    cout << "Quarter count = "<<QuarterCount<<" Amount = "<<QuarterCount*0.25<<endl;
    cout << "Dime count = "<<DimeCount<<" Amount = "<<DimeCount*0.10<<endl;
    cout << "Nickle count = "<<NickleCount<<" Amount = "<<NickleCount*0.05<<endl;
    cout << "Penny count = "<<PennyCount<<" Amount = "<<PennyCount*0.01<<endl;

    return 0;
}



****************** Change = $1 ******************
Quarter count = 2 Amount = 0.5
Dime count = 4 Amount = 0.4
Nickle count = 2 Amount = 0.1
Penny count = 0 Amount = 0
Last edited on
Alas, while the answer could technically be found by simulation, you are better off handling it differently.

Consider all (USD, sorry) coins ≤ $1:
  • dollar coin
  • half-dollar
  • quarter
  • dime
  • nickel
  • penny

Here is the next question to ask: does order matter?

For dollar questions the answer is typically “no”. This answer will assume that. (If it is not, then the answer is similar, but you cannot discount branches like we do here.)

A dollar-coin is one way to make a dollar, with no coinage left over. N = 1.

A half-dollar coin makes half a dollar. How many ways can I use coins to make half a dollar?
N += (number of ways to make a half dollar).

Three quarters make 75 cents. How many ways can I use coins to make a quarter?
N += (number of ways to make a quarter)

Do you see the pattern we are building here?

Hope this helps.
I think so, just coding wise is were I'm lost. Would this be a multi demential array? We're the columns would hold the coin counts and the rows would hold the coin values?
Topic archived. No new replies allowed.