Solved

making change using int and mod division

Posted on 2003-10-24
11
337 Views
Last Modified: 2010-04-01
I am writing a program that calculates change returned, but I need to do so with both int and mod division.  Here are the problems I am having:
1.  Once I do int division, how do I do mod division?
2.  I want to use a switch, but the compiler won't let use a double as a value in my case, so what value could I use??

here is my code:

amtChange=price-paid;
switch(amtChange)
case 10.00:  tens=(amtChange/10.00)+.005;
      amtChange=amtChange-(10.00*tens);

case 1.00: dollars = (amtChange / 1.00)+.005;      
        amtChange = amtChange - (1.00*dollars);

case .25:  quarters = (amtChange / .25)+.005;
        amtChange = amtChange - (.25 * quarters);

case .10:  dimes = (amtChange / .10)+.005;
          amtChange = amtChange - (.10 * dimes);

case .05:  nickels = (amtChange / .05)+.005;
         amtChange = amtChange - (.05 * nickels);  

default:  pennies = (amtChange / .01)+.005;

Thanks...
0
Comment
Question by:rgill67
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 9

Accepted Solution

by:
tinchos earned 500 total points
ID: 9616339
For the first question, I'm not sure what you meant

you can do

int x = 6;
int y = 4;
int result;
int rest;

result = x/y;   // result = 1
rest = x%y;    // rest = 2

For the second one

as switch handles integer values, lets say you'll be working with 2 decimals

you can do

switch( atmChange * 100 )           // 100 if you want to work with the 2 decimal places
{
   case 1000:
                        // Code for 10.00
   case 100:
                        // Code for 1.00
   case 25:
                        // Code for 0.25
   case 10:
                        // Code for 0.10
   case 5:
                        // Code for 0.05
   default:
                        // Code
}

Hope this helps
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9616401
using both: For example: change is 24.38
multiply that by 100 gives you 2438.
2438%1000 gives 2, so 2 $10
438%100 gives 4 so 4 $1
38/25 gives 1 so 1 quarter
13/5 gives 2 so 2 nickles
3/1 gives 3 so 3 pennies.

With the switch statement you're using I guess you can do switch(atmChange*100)
case 1000:
case 100:
case 25:
etc.

Is that what you want?
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9616403
sorry tinchos, i was writing while you posted :)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:tinchos
ID: 9616427
Ok, no problem

happens all the time

:)
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9616458
rgill67,
> I am writing a program that calculates change returned, but I need to do so
> with both int and mod division.  Here are the problems I am having:
> 1.  Once I do int division, how do I do mod division?
> 2.  I want to use a switch, but the compiler won't let use a double as a value
> in my case, so what value could I use??

There is no need for a switch.  A switch is for when you want to process one of a different number of values.  For this, you need to process EACH value (10's, then 1's, then quarters, etc.).  You don't just want to do one.

So, try this:
      amtChange = price-paid;

      tens = amtChange / 10.00;
      amtChange = fmod( amtChange, 10.00 );

      dollars = amtChange;
      amtChange = fmod( amtChange, 1.00 );

      quarters = amtChange / 0.25;
      amtChange = fmod( amtChange, 0.25 );

      dimes = amtChange / 0.10;
      amtChange = fmod( amtChange, 0.10 );

      nickels = amtChange / 0.05;
      amtChange = fmod( amtChange, 0.05 );

      pennies = amtChange / 0.01;

I assumed that amtChange and price-paid are floats or doubles.  If they are not, then this will need to be tweaked slightly, but I can help you with that.


Hope that helps,
Dex*
0
 
LVL 1

Expert Comment

by:Mustak_Shaikh
ID: 9616719
rgill67,
first of all i think there is no need to use switch case over here.
I think you are dividing the variable with it self only
i.e. when amtChange is 10
then you are doing amChange/10
which obvious means 10/10

Please make your problem very clear, i guess you are missing something.

Mustak_Shaikh
0
 

Author Comment

by:rgill67
ID: 9617199
Both tinchos and n-fortynine were helpful with the switch, thank you...
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9617210
rgill67,

Would you please explain why you accepted that answer?  The first part was right on about how to do MOD division, but the rest of it won't help you at all.

Dex*
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9617214
You're welcome rgill67

Glad it helped

Have a nice weekend

Tincho
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9617225
Sorry dexstar,

But I don't agree with you that my comment won't help at all.

Why are you saying that?

0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9617318
tinchos,

Firstly, I don't mean to offend.  You definitely answered the question that he asked.  I guess that's why he accepted your answer.  That's fine.

My problem is that the answer to that question won't help him solve his problem because a switch statement is not at all appropriate.

> switch( atmChange * 100 )           // 100 if you want to work with the 2 decimal places
> {
>    case 1000:
>                         // Code for 10.00
>    case 100:
>                         // Code for 1.00
>    case 25:
>                         // Code for 0.25
>    case 10:
>                         // Code for 0.10
>    case 5:
>                         // Code for 0.05
>    default:
>                         // Code
> }

What if atmChange = 37.37 ?  Then it will switch on 3737, which doesn't have a corresponding case statement, so it will execute the "default".  How is that going to help you compute the amount of change to give?  The example only will work if you are giving exactly 10 dollars, 1 dollar, 25 cents, 10 cents, 5 cents, or a penny in change.  If you want to figure out how to give out $37.37, it won't work at all.

Anyway, I guess he got the answer to his question, but after he plays with it for a while, he'll figure out that it isn't going to work using a switch statement, and he'll have to come back to ask another question.  No, wait, he won't because I already gave him the answer.  Oh well.  It's just points.

Dex*
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best book to learn C++ 4 87
No module found pypyodbc, 3 45
what are the top 5 c++14 features that should not be used in a low latency application? 3 35
boost::uuid crashes 17 41
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question