This topic is locked from further discussion.

Avatar image for candid_llama
candid_llama

315

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#1 candid_llama
Member since 2007 • 315 Posts

im supposed to right a program that figures out the change for a customer. i have everything else written, but i can get to finish the formulas to give me the change in dollars, quarters, nickles, dimes, and pennies. Im supposed to assume that the consumer will only pay with whole dollars.

for example, if the change is $1.58, it should give results such as this.

1 dollar coin(s)
2 quarter(s)
0 dime(s)
1 nickel(s)
3 penny(ies)
does anyone know whow i can do this?
Avatar image for dnuggs40
dnuggs40

10484

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#2 dnuggs40
Member since 2003 • 10484 Posts

im supposed to right a program that figures out the change for a customer. i have everything else written, but i can get to finish the formulas to give me the change in dollars, quarters, nickles, dimes, and pennies. Im supposed to assume that the consumer will only pay with whole dollars.

for example, if the change is $1.58, it should give results such as this.

1 dollar coin(s)
2 quarter(s)
0 dime(s)
1 nickel(s)
3 penny(ies)
does anyone know whow i can do this?

candid_llama

I can do it, but not in C++

If you want, i cant write in C#

Let me know...

Avatar image for rimnet00
rimnet00

11003

Forum Posts

0

Wiki Points

0

Followers

Reviews: 7

User Lists: 0

#3 rimnet00
Member since 2003 • 11003 Posts

Use the "mod" function. 1.58 mod 1 = 1, so 1 dollar. Then mod the remainder with quarters.... .58 mod .25 = 2... extra... get it?

int total = 1.54;
int dollars = mod(total,1);
total=total-1*dollars;
int quarters = mod(total,.25);
total=total-.25*quarters;
int nickels = mod(total,.05);
total=total-.05*nickels;
int pennies= total / .01;

If you can't figure out how to get the mod function to work with non-integer values, then multiply everything by 100... ie: 1.58=158. Hope I just did your HW for you :P

Avatar image for bblundell
bblundell

1086

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#4 bblundell
Member since 2006 • 1086 Posts
What is the cin? I'm assuming that the example you gave will be your cout. Right? Is the user inputing the amount of money they are handing to you, but how much does the product cost? Or can it be different products?
Avatar image for captalchol
captalchol

643

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#5 captalchol
Member since 2006 • 643 Posts

Its been awhile since I've programmed but if you write a loop that first checks the largest monetary value then works its way ot the smallest amount until no number is left.

Use the largest demonination possible(1 dollar) you cant any more then subtract that from the total, and repeat with the smaller coins until you have nothing left.

make sure you restart the loop after the largest denomitor is used up

After reading this back ot myslef it doesn't make too much sense but I hope this helps.

if the change is 158

while change != 0

if change>=100 then

while change>=100

change=change-100

dolar=dolar+1

break off and start over

if change>=25 then

while change>=25

change=change-25

quarter++

break

and go on with the rest of the change amounts.

thats generally how i I would do it minus the sytanx but you would probably get the idea.

or you could do it with the mod function like the guy above said ;)

Avatar image for bblundell
bblundell

1086

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#6 bblundell
Member since 2006 • 1086 Posts
Did you write an algorithm? If so copy and paste it here.
Avatar image for Zero_Space
Zero_Space

659

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#7 Zero_Space
Member since 2007 • 659 Posts
Well I'm not trying to rain on anybody's parade, but what exactly is this doing in the PC games forum? Should be in Offtopic
Avatar image for dnuggs40
dnuggs40

10484

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#8 dnuggs40
Member since 2003 • 10484 Posts

While you deffinetly can use the mod function (and some others), here is another way. It's more visual so you can see what going on...

private string GetChange(decimal dCost, decimal dAmountGiven)
{

int iTenBill = 0;
int iFiveBill = 0;
int iOneBill = 0;
int iQuater = 0;
int iDime = 0;
int iNickle = 0;
int iPenny = 0;
decimal dRemainder = dAmountGiven - dCost;
string sChange = "";

sChange = "Amount is change: " + dRemainder.ToString;

while (dRemainder >= 10) {
iTenBill = iTenBill + 1;
dRemainder = dRemainder - 10;
}

while (dRemainder >= 5) {
iFiveBill = iFiveBill + 1;
dRemainder = dRemainder - 5;
}

while (dRemainder >= 1) {
iOneBill = iOneBill + 1;
dRemainder = dRemainder - 1;
}

while (dRemainder >= 0.25) {
iQuater = iQuater + 1;
dRemainder = dRemainder - 0.25;
}

while (dRemainder >= 0.1) {
iDime = iDime + 1;
dRemainder = dRemainder - 0.1;
}

while (dRemainder >= 0.05) {
iNickle = iNickle + 1;
dRemainder = dRemainder - 0.05;
}

while (dRemainder >= 0.01) {
iPenny = iPenny + 1;
dRemainder = dRemainder - 0.01;
}

sChange = sChange + Constants.vbCrLf + Constants.vbCrLf;
sChange = sChange + "Tens: " + iTenBill.ToString + Constants.vbCrLf;
sChange = sChange + "Fives: " + iFiveBill.ToString + Constants.vbCrLf;
sChange = sChange + "Ones: " + iOneBill.ToString + Constants.vbCrLf;
sChange = sChange + "Quarters: " + iQuater.ToString + Constants.vbCrLf;
sChange = sChange + "Dimes: " + iDime.ToString + Constants.vbCrLf;
sChange = sChange + "Nickles: " + iNickle.ToString + Constants.vbCrLf;
sChange = sChange + "Pennies: " + iPenny.ToString + Constants.vbCrLf;

return sChange;

}

Anyways, good luck. My suggestion is to look into the mod and other functions after you understand how this works.

Avatar image for teuf_
Teuf_

30805

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#9 Teuf_
Member since 2004 • 30805 Posts

LMAO, this is a strange place to post such a question.

Anyway you want to successively subtract values starting with larger denominations

while (change >= 5)
{
change -= 5;
fives++;
}

while (change >= 1)
{
change -= 1;
singles++;
}

while (change >= 0.25)
{
change -= 0.25;
quarters++;
}

...

and so on.

Avatar image for rimnet00
rimnet00

11003

Forum Posts

0

Wiki Points

0

Followers

Reviews: 7

User Lists: 0

#11 rimnet00
Member since 2003 • 11003 Posts
Why would you do loops, when you can do it linearly using the 'mod' function? Loops should always be last resort. I don't know how you are going to be graded, but even high school teachers, i presume, would mark off if you used a loop in this situation.
Avatar image for -AK47-
-AK47-

3277

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#12 -AK47-
Member since 2007 • 3277 Posts
I can do that. Just not in C++. I can do that in visual Basic.
Avatar image for kyrieee
kyrieee

978

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#13 kyrieee
Member since 2007 • 978 Posts
Yeah the loop code is pretty messy, but try to think about programming problems thoroughly before you ask for help, because programming is all about solving problems, and that's what you're supposed to learn in a programming course (that and the language but...) and you won't do that asking for help as soon as you run into trouble.
Avatar image for captalchol
captalchol

643

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#14 captalchol
Member since 2006 • 643 Posts

Why would you do loops, when you can do it linearly using the 'mod' function? Loops should always be last resort. I don't know how you are going to be graded, but even high school teachers, i presume, would mark off if you used a loop in this situation.rimnet00

Well the loops allow him to understand the actual process of whats going on. If its an beginner couse which is it sounds like it is, it is important to have that more basic understanding.

The mod function is more efficient waying of doing though..

Avatar image for dnuggs40
dnuggs40

10484

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#15 dnuggs40
Member since 2003 • 10484 Posts

Yeah the loop code is pretty messy, but try to think about programming problems thoroughly before you ask for help, because programming is all about solving problems, and that's what you're supposed to learn in a programming course (that and the language but...) and you won't do that asking for help as soon as you run into trouble.kyrieee

Thats why I showed him the loop...so he can understand the logic. People don't just pop out of school avid programmers. You start at square one, and once you understand the logic, then you can take the short cuts.

If he can't even figure out the logic behind giving change, you think taking shortcuts with mod functions makes it any easier? He is at square one...he needs to learn accordingly.

Avatar image for bblundell
bblundell

1086

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#16 bblundell
Member since 2006 • 1086 Posts

Yeah the loop code is pretty messy, but try to think about programming problems thoroughly before you ask for help, because programming is all about solving problems, and that's what you're supposed to learn in a programming course (that and the language but...) and you won't do that asking for help as soon as you run into trouble.kyrieee

I was highly surprised that he didn't post an algorithm.......If I ever need help I'll email my cousin the algorithm and the code I got so far.

Avatar image for Gog
Gog

16376

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#17 Gog
Member since 2002 • 16376 Posts
This is largely off-topic.