Computing.Net > Forums > Programming > Help with making C++ Program

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help with making C++ Program

Reply to Message Icon

Name: Justin S.
Date: November 1, 2002 at 16:57:04 Pacific
OS: XP Pro
CPU/Ram: P4 2.2, 512 MB
Comment:

I need some help with a program I need to write. I am a sophomore in high school taking AP Computer Science A. I need to make a program that computes change for any value (41.12 = 2 $20, 1 $1, 1 dime, 2 pennies) Any help would be appreciated



Sponsored Link
Ads by Google

Response Number 1
Name: Jim
Date: November 1, 2002 at 19:54:52 Pacific
Reply:

Here's one way that's easy for starters:

Create counters for each denomination.

Initialize them to 0.

Input the Total.

Start at the highest denomination.

As long as the total is bigger, increment the denomination counter, and subtract that amount from the total.

Move on to the next denomination.

It's also easier if you use integers instead of reals. In which case you actually work with pennies instead of dollars. $41.12 would be represented as 4112.

Here's some example stuff.


int TwentyDollarBillCount = 0;
int TenDollarBillCount = 0;
int FiveDollarBillCount = 0;
...
int NickelCount = 0;
int PennyCount = 0;

Read in the Total using scanf, cin, or whatever.

OK. Here's where you calculate the counters:

while (Total >= 2000)
{
    ++TwentyDollarBillCount;
    Total -= 2000;
}
while (Total >= 1000)
{
    ++TenDollarBillCount;
    Total -= 1000;
}
...
while (Total >= 5)
{
    ++NickelCount;
    Total -= 5;
}
PennyCount = Total;

Now you have all the counters, and all you have to do is print them out. You can use cout or printf or whatever. You don't want to print the zero counters, so you need a lot of if statements:

if (TwentyDollarBillCount > 0)
    printf("%d Twenty dollar bills ", TwentyDollarBillCount);

if (TenDollarBillCount > 0)
    printf(("%d Ten Dolllar Bills ", TenDollarBillCount);

...

if (NickelCount > 0)
    printf("%d Nickels ", NickelCount);

if (PennyCount > 0)
    printf("%d Pennies ", PennyCount);


If you want to get even more nit picky, you can check for counts of one, and futz with the plurals. It does look kinda lame to print "1 pennies" instead of "1 penny".


0
Reply to Message Icon

Related Posts

See More


scripting Free n best way to learn



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help with making C++ Program

Help with my C++ program www.computing.net/answers/programming/help-with-my-c-program/1813.html

Help with a C Program / Newbie www.computing.net/answers/programming/help-with-a-c-program-newbie/8071.html

Need help with C++? www.computing.net/answers/programming/need-help-with-c/4642.html