Solved

Need help using modular division in C++

Posted on 2004-10-25
934 Views
Last Modified: 2008-01-09
My school assignment requires me to write a program to find out the change left after you input the amount tendered. my teacher tells us to use the % syntax which returns the remainder after division. i tried that but it gives me a error about using %with type double variables. but i must have it type double because i need to find the change in cents and double is the only variable that supports decimals. i have just started learning c++ so please help. thanks.
0
Question by:accesspoint
    10 Comments
     
    LVL 4

    Expert Comment

    by:nagraves
    % or 'mod' is used with integer division.

    int x = 9;
    iny y = 5;
    int z = 0;

    z = x%y;

    z is now equal to 4.  the modulus gives you the remainder.
    0
     
    LVL 4

    Expert Comment

    by:nagraves
    To add to my comment earlier, you could first multiply your double or float by 100 and then typecast to int. then use %.

    None the less, % is an integer operator and cannot be used with floating point numbers.
    0
     
    LVL 39

    Expert Comment

    by:itsmeandnobodyelse
    >>>> i must have it type double because i need to find the change in cents

    No, the number of cents is an integer and not a double:

        double priceInDollars = 11.27;  // dollars
        int       priceInCents   = (int) (price*100);

        int dollar  = priceInCents/ 100;         // integer division gives the right number
        priceInCents = (priceInCents%100);  // calculate remainder
        int quarter    = priceInCents / 25;      // and so on ...
        ...

    Regards, Alex


    0
     
    LVL 39

    Expert Comment

    by:itsmeandnobodyelse
    Correction:

       int       priceInCents   = (int) (priceInDollars*100);
    0
     
    LVL 4

    Expert Comment

    by:nagraves
    Thank you itsmeandnobodyelse. You have demonstrated what I wrote quite well. I didn't think to give an example of typecasting for the kid. :)
    0
     

    Author Comment

    by:accesspoint
    ok so if im understanding correctly, i first have to typecast to int before i can use the mod division and then divide by 100 to get back the decimal?
    0
     
    LVL 4

    Accepted Solution

    by:
    Well..

    Typecast twice if you want that i suppose, you could do this:

        double priceInDollars = 11.27;  // 11.27 dollars
        int       priceInCents   = (int)(priceInDollars*100); // 1127 cents
        double amountTendered = 15.00;  // 15.00
        int       amountInCents =(int)(amountTendered*100);  1500

        int changeInCents = amountInCents - priceInCents; // 373
        int changeDollars = changeInCents/100; // 3
        int changeCents = changeInCents%100; // 73
        double finalChange = ((double)(changeDollars)) + ((double)(changeCents)/100); // 3.73
    //
    //  This is basically what is going on...
    //   you input the price in dollars as a double amount ( 11.27 )
    //   you convert that to cents by typecasting..  (int)(priceInDollars)
    //   you input the amount of cash tendered in dollars as a double (15.00)
    //    you convert that to cents by typecasting.. (int) (amountTendered)
    //  
    //    You find the amount of change in cents by simple subraction
    //     You find the amount of dollars by dividing the changeincents by 100
    //     you find the amount of change in by using %
    //     you typecast back to a double, adding the the typecasted dollars to the typecasted change
    //     which is divided by 100 to give you the decimal.

    NOW I MUST WARN YOU... It is a bad idea to typecast back into a float, because you will be sure to get wrong answers! In my opinion you should just leave your output as int values and have it read  "The change is <changeDollars> dollars and <changeCents> cents."
    0
     
    LVL 39

    Expert Comment

    by:itsmeandnobodyelse
    >>>> then divide by 100 to get back the decimal?

    No, the results you need - as nagraves told you - are integers. The numbers of dollars, quarters, nickels, cents, all are integers. Actually, you don't need any typecasting if you calculate the number of cents like that:

        double d = 12.27;
        int       c = 100 * d; // 1227

    >>>> int changeDollars = changeInCents/100;

    What you see here is an integer division, i. e. the result is an integer - not a decimal -  and that is good as we need the number of dollar bills here and not the decimal value of the change.

    Regards, Alex

    0
     
    LVL 4

    Expert Comment

    by:nagraves
    Well, I'd like the points :)
    0
     

    Author Comment

    by:accesspoint
    sorry guys that i took so long to respond. i didn't even realize i had a open question until i got the email!

    anyways, i tried what nagraves did and it worked out fine. thanks guys.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    933 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now