solution to break a bill

C

cypisek

I am trying to write a program that will print out all possible
solutions of breaking a 100 bill.
The program will display:
50 20 10 5 1
2 0 0 0 0
as one of the possible solutions.

I already have the logic to print this solution but can't seem to put
that into a loop that will give me all the solutions. I don't know on
how to compare the solution that is already working to the next
solution. Any help would be greatly appreciated:

Here is what I have so far:
resetcounter();

b_50_c = final_sum/b_50;

if (final_sum%b_50==0)
{
writebreakdown(b_50_c,b_20_c,b_10_c,b_5_c,b_1_c,count);
}
else
{
final_sum = final_sum - b_50;
b_20_c = final_sum/b_20;

if (final_sum%b_20==0)
{
writebreakdown(b_50_c,b_20_c,b_10_c,b_5_c,b_1_c,count);
}
else
{
final_sum = final_sum - b_20;
b_10_c = final_sum/b_10;

if (final_sum%b_10==0)
{
writebreakdown(b_50_c,b_20_c,b_10_c,b_5_c,b_1_c,count);
}
else
{
final_sum = final_sum - b_10;
b_5_c = final_sum/b_5;

if (final_sum%b_10==0)
{
writebreakdown(b_50_c,b_20_c,b_10_c,b_5_c,b_1_c,count);
}
else
{
final_sum = final_sum - b_5;
b_1_c = final_sum/b_1;
writebreakdown(b_50_c,b_20_c,b_10_c,b_5_c,b_1_c,count);
}
}
}

}
 
D

David Harmon

On 17 Sep 2006 17:19:57 -0700 in comp.lang.c++, "cypisek"
I already have the logic to print this solution but can't seem to put
that into a loop that will give me all the solutions.
....

if (final_sum%b_50==0)
{
writebreakdown(b_50_c,b_20_c,b_10_c,b_5_c,b_1_c,count);
}

As I mentioned here recently, repetitive code bugs the heck out of
me. In this case, it is your multiplicity of similarly named
variables, your many calls to writebreakdown, and your deeply nested
ifs all with very similar structure. My eyes glaze over looking to
see if there is some tiny typo buried in the middle of that to spoil
the whole thing.

Instead, I would encourage you to put the denominations in some
collection and iterate over it. An incomplete sketch of some such
approach follows:

int denom[] = {50, 20, 10, 5, 1};
for (int i=0; i<5; ++i) {
int howmany = remaining_value / denom;
remaining_value -= howmany * denom;
cout << "give back " << howmany << " " << denom <<"s\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top