Thread: Math Division Problem

  1. #1
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Unhappy Math Division Problem

    I will put the code in first! I will not pu the whole code as it takes too much space! I will just put the thing that I need help on!
    Code:
    else if (choice == 4)
        {
        printf ("You chose division!\n\n");
        printf ("Input the first integer,x:\n");
        scanf ("%d", &x);
        printf ("Input the second integer, y:\n");
        scanf ("%d", &y);
        printf ("The result is:%d\n", x / y);
        }
    If I do not put in soem decimals like 10 and 5 or 30 and 10 or something that divides proper, I would give me some crazy results! I tried floating point with %f instead of %d and defined x and y as float also and it gives me -o.something different all the time!

    What is the mistake or what do I need to do to make it work properly?


    BTW, I tried the % operator instead of / division too, then it gives me plain 0!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by cookie View Post
    What is the mistake or what do I need to do to make it work properly?
    If you expect a floating point result, use floating point rather than integers. Integer math is not the same as floating point math.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Could you show your "crazy results"?
    like 5/2 == 2
    or 17 / 3 = 5
    ?

    And also your code using floats could be of some interest. Post it too.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Integer division - 9/5 doesn't mean 1.8 or 2, it means 1. Even to a float, double, or 1 million-bit floating point variable, it doesn't mean exactly 1.8. That's the thing with division.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  5. #5
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Making myself clear

    All I want from it is to show me the remainder also!

    I tried the % operator but still doesn't work!
    <<deleted because of colour and size abuse>>

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    If you would post a sample input and corresponding output of what you call "crazy", we might be able to better assist you in understanding what is going on.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Just make x and y doubles instead of ints (and make the appropriate changes to the scanf and printf format specifiers) and things should be fine. Doing division on integers chops off any "remainder".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    printf ("&#37;d divided by %d is %d reminder %d", 10,3,10/3,10%3);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cube rotation math problem
    By n3v in forum Game Programming
    Replies: 6
    Last Post: 08-03-2007, 05:41 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Math Problem....
    By NANO in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-11-2002, 04:37 AM
  4. math problem
    By unixOZ in forum Linux Programming
    Replies: 4
    Last Post: 10-19-2002, 12:17 AM
  5. Little math problem
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-27-2001, 07:44 PM