By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,943 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,943 IT Pros & Developers. It's quick & easy.

solution to break a bill

P: n/a
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,co unt);
}
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,co unt);
}
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,co unt);
}
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,co unt);
}
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,co unt);
}
}
}

}

Sep 18 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 17 Sep 2006 17:19:57 -0700 in comp.lang.c++, "cypisek"
<cy*****@ptd.netwrote,
>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,co unt);
}
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[i];
remaining_value -= howmany * denom[i];
cout << "give back " << howmany << " " << denom[i] <<"s\n";
}
Sep 18 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.