none
integer division--dividing integers by integers

    Question

  • I have been trying to figure this out all day.. i have a project due tomorrow at 9:30am, am new to c++ but got some things down. My question is i am assigned to divide and integer by 2 to get its half , when i divide an odd number by 2 for instance 3/2 i get 1, whereas we know it suppose to be 1.50, Ive done all the research i read into modulus and etc, i just dont know how to code it do that if i divide any integer by 2 it give me the remainder. well here is my project

     

    /*********************************************************************************

    CS140 21746

    Name: X

    Project 1

    Input: any integer number from the keyboard

    Output: half the number that wa sinput on the screen

    Program description: This program prints half of the number input by the user.

    Formula: input number/2

    File Names: Project1.cpp, outla.txt, out1b.txt

    *********************************************************************************/

    #include <iostream>

    using namespace std;

    int main()

    {

    int number; //number to be input by user

    int total; //half the number input

    //Output the program purpose

    cout<<"THIS PROGRAM PRINTS HALF OF THE NUMBER INPUT" << endl << endl;

    //Prompt for the number

    cout<< "Please enter an integer number ";

    cin>> number; //Input a number

    total = number%2;

    //Divide the number by 2 using integer division

     

     

    //Display half the input number

    cout<< total << " is half the number you typed.\n\n";

    //Display your own message that tells something about you.

    cout << "My name is X, and i am majoring in Computer Science\n";

    cout << "Thank you for using this program i hope it was useful\n\n";

     

    //Display my name

    cout << "Programmer:X\n";

    return 0;

    }

    Thursday, September 09, 2010 1:37 AM

Answers

  • / is divide, % is remainder -- 3/2 is 1, 3%2 is also 1. Use a larger number; 5/2 is 2, 5%2 is 1. The code you've shown treats % as divide.
    Thursday, September 09, 2010 1:50 AM
  • #include <sstream>
    #include <iostream>
    using namespace std;
    
    string IntegerDivision (int numerator, int denominator)
    {
     int result = numerator / denominator;
     int remainder = numerator % denominator;
    
     ostringstream os;
     os << result;
     
     if (remainder != 0)
      os << " " << remainder << "/" << denominator;
    
     return os.str ();
    }
    
    int main ()
    {
     cout << 52 << "/" << 4 << " = " << IntegerDivision (25, 4) << endl;
     cout << 52 << "/" << 5 << " = " << IntegerDivision (25, 5) << endl;
     cout << 3 << "/" << 2 << " = " << IntegerDivision (3, 2) << endl;
     cout << 3 << "/" << 3 << " = " << IntegerDivision (3, 3) << endl;
    
     return 0;
    }
    
    Thursday, September 09, 2010 2:43 AM
  • x/y is the division operator.  If the two datatypes in the division are integer types, then the result will be an integer quotient; the integer remainder is discarded.  If at least one of the datatypes in the division is a floating pointer number, then the result will be a decimal fraction, e.g.

    1/2 yields 0 (remainder 1 discarded)

    1.0/2 yields 0.5

    x%y is the modulo operator.  It returns the remainder of a division of one integer number by another.

    1 % 2 yields 1

    1.0 % 2 is not well defined because 1.0 is not an integer type.

     

    So with your example program, introduce floating point datatypes somewhere in the program.  You could do this on input:

     

    double number;
    ...
    cin >> number;
    

     

    Or you could do when you do the division:

     

    double total;
    ...
    total = number / 2.0;
    
    Note, you need one of the terms in the division to be floating number to use floating division; 2.0 meets this requirement.  It also would have been ok if you had made number a double instead of int.  Also, we need total to be a floating number so it "knows" how to hold the decimal fraction part of the result.

     

    Thursday, September 09, 2010 4:44 PM

All replies

  • / is divide, % is remainder -- 3/2 is 1, 3%2 is also 1. Use a larger number; 5/2 is 2, 5%2 is 1. The code you've shown treats % as divide.
    Thursday, September 09, 2010 1:50 AM
  • How would i code it so that % can be used as a remainder if necessary?
    Thursday, September 09, 2010 1:52 AM
  • % is always a remainder. Have you considered that you may want floating-point arithmetic instead of integral arithmetic?
    Thursday, September 09, 2010 2:29 AM
  • hey, ildjarn

    i know i seem nubby and all but i am 100% new to this and i don't know very much.. can you show me an example on which i can insert a floating point arithmetic? 

    i think its:

    double x = 1.0;

    int y = 2;

    double z = x/y; // z will be 1.0/2 = 0.5.n

    i dont know to to insert this into my code, only because for the number, there isnt a specific integer..

    Thanks for your time.
    ilfjarn






    Thursday, September 09, 2010 2:41 AM
  • #include <sstream>
    #include <iostream>
    using namespace std;
    
    string IntegerDivision (int numerator, int denominator)
    {
     int result = numerator / denominator;
     int remainder = numerator % denominator;
    
     ostringstream os;
     os << result;
     
     if (remainder != 0)
      os << " " << remainder << "/" << denominator;
    
     return os.str ();
    }
    
    int main ()
    {
     cout << 52 << "/" << 4 << " = " << IntegerDivision (25, 4) << endl;
     cout << 52 << "/" << 5 << " = " << IntegerDivision (25, 5) << endl;
     cout << 3 << "/" << 2 << " = " << IntegerDivision (3, 2) << endl;
     cout << 3 << "/" << 3 << " = " << IntegerDivision (3, 3) << endl;
    
     return 0;
    }
    
    Thursday, September 09, 2010 2:43 AM
  • x/y is the division operator.  If the two datatypes in the division are integer types, then the result will be an integer quotient; the integer remainder is discarded.  If at least one of the datatypes in the division is a floating pointer number, then the result will be a decimal fraction, e.g.

    1/2 yields 0 (remainder 1 discarded)

    1.0/2 yields 0.5

    x%y is the modulo operator.  It returns the remainder of a division of one integer number by another.

    1 % 2 yields 1

    1.0 % 2 is not well defined because 1.0 is not an integer type.

     

    So with your example program, introduce floating point datatypes somewhere in the program.  You could do this on input:

     

    double number;
    ...
    cin >> number;
    

     

    Or you could do when you do the division:

     

    double total;
    ...
    total = number / 2.0;
    
    Note, you need one of the terms in the division to be floating number to use floating division; 2.0 meets this requirement.  It also would have been ok if you had made number a double instead of int.  Also, we need total to be a floating number so it "knows" how to hold the decimal fraction part of the result.

     

    Thursday, September 09, 2010 4:44 PM