Thread: Help with Modulus

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    59

    Help with Modulus

    Hi All,

    Can one of you guys give me some advice on the following,

    I have 2 float numbers and i want to change them into ints then
    find the remander via modulus, this is what i have :-

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    void main()
    {
    
    float float_num1;
    float float_num2;
    int int_num3;
    int int_num4;
    
    cout << "1st number:";
    cin >> float_num1;
    
    cout << "2nd number:";
    cin >> float_num2;
    
    int_num3 = (int)float_num1;
    int_num4 = (int)float_num2;
      
    cout << int_num3 % int_num4;
    }
    If i enter 98.33 for float1 & 15.22 for float2
    The remainder shuld be 5 (if my maths is right)
    But i get the result 8 in my program?

    Can anyone see a problem?

    Cheers

    Boontune

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Can anyone see a problem?
    Your math is wrong. :-) 98 % 15 = 8, try it with a calculator if you want.
    *Cela*

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    98%15 is indeed 8.
    15*6 = 90
    98-90 = 8

    That said, your program still has a few flaws. main is not returning an int, like it should. You are not using stdlib.h yet it is still included, and you are using the depracated header includes (standard headers do not end in .h). Here is a more cleanly written version.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	
    	float float_num1;
    	float float_num2;
    	int int_num3;
    	int int_num4;
    	
    	cout << "1st number:";
    	cin >> float_num1;
    	
    	cout << "2nd number:";
    	cin >> float_num2;
    	
    	int_num3 = (int)float_num1;
    	int_num4 = (int)float_num2;
    	 
    	cout << int_num3 % int_num4 << endl;
    	return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Wow, two of you posted while I was rewriting the program. AND Salem's post had the same exact (to the WORD, minus spacing and a period) sentence as mine (98 % 15 is indeed 8).

    Pretty weird.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    59
    I know its a mess, i only through it together just to quickly show you guys!!

    Oh crap, it is right!! hahaha

    Sorry for wasting your time boys!


    cya soon

    Boontune

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. modulus operator with double operands?
    By chickenandfries in forum C Programming
    Replies: 7
    Last Post: 03-28-2008, 07:21 AM
  2. modulo 2 modulus
    By breik in forum C Programming
    Replies: 5
    Last Post: 03-24-2006, 02:30 PM
  3. Modulus algorithm
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-10-2004, 10:34 AM
  4. Floating-point modulus
    By Sang-drax in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2002, 08:20 PM
  5. using modulus & weighting factors
    By task in forum C Programming
    Replies: 4
    Last Post: 09-11-2002, 05:52 PM