Thread: passing arguments by reference - finding integer and fraction parts

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    passing arguments by reference - finding integer and fraction parts

    Hi

    Which of the following codes, CODE 1 and CODE 2, is more correct and understandable?

    What's the need of converting "temp" into float again? Why don't we leave it as double? Or, just make it int?

    CODE 1
    Code:
    // ref.cpp
    // demonstrates passing by reference
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    
       {
       void intfrac(float dumnumber, float& dumintpart, float& dumbfracpart);
    
       float number, intpart = 0, fracpart = 0;
    
       do {
    
          cout << "Enter a real number: ";
          cin >> number;
    
          intfrac(number, intpart, fracpart);
    
          cout << "Integer part is " << intpart
               << ", fraction part is " << fracpart << endl;
    
          } while( number != 0.0 );
    
       system("pause");
       return 0;
       }
    
    //--------------------------------------------------------------
    // intfrac()
    // finds integer and fractional parts of real number
    void intfrac(float dumnumber, float& dumintpart, float& dumfracpart)
       {
    
       long temp = static_cast<long>(dumnumber);
       dumintpart = static_cast<float>(temp);
       dumfracpart = dumnumber - dumintpart;
    
       }
    //---------------------------------------------------------------

    CODE 2
    Code:
    // ref.cpp
    // demonstrates passing by reference
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    
       {
       void intfrac(float dumnumber, float& dumintpart, float& dumbfracpart);
    
       float number, intpart, fracpart;
    
       do {
    
          cout << "Enter a real number: ";
          cin >> number;
    
          intfrac(number, intpart, fracpart);
    
          cout << "Integer part is " << intpart
               << ", fraction part is " << fracpart << endl;
    
          } while( number != 0.0 );
    
       system("pause");
       return 0;
       }
    
    //--------------------------------------------------------------
    // intfrac()
    // finds integer and fractional parts of real number
    void intfrac(float dumnumber, float& dumintpart, float& dumfracpart)
       {
    
       long temp = static_cast<long>(dumnumber);
       dumintpart = static_cast<float>(temp);
       dumfracpart = dumnumber - dumintpart;
    
       }
    //---------------------------------------------------------------
    Last edited by jackson6612; 05-15-2011 at 06:25 AM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It would be easier if you just asked if initialising intpart and fracpart would make the code more readable than not doing so.

    I'd say that this is your call. Generally, initialising variables is good practice, but here they will only be initialised to be overwritten.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by laserlight View Post
    It would be easier if you just asked if initialising intpart and fracpart would make the code more readable than not doing so.

    I'd say that this is your call. Generally, initialising variables is good practice, but here they will only be initialised to be overwritten.
    Sorry. Now I realize I could have been more clear with my query. Thanks for the comment.

    Would you please also comment on the following query?

    What's the need of converting "temp" into float again? Why don't we leave it just as a double? Or, just make it int?
    Regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Somebody was just being explicit about the type conversions. You would get the same thing by just saying
    Code:
    dumintpart = temp;
    since the assignment would have to do the conversion silently as well. But at some point you're going to have to convert back to a float, since that is what is returned.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, tabstop.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Well, I was wondering what's the need to declare "dumintpart" as float& ? I would have simply declared it as int& because "dumintpart" should be int type and this way there won't be any need for casting. What do you say? Is there some particular reason for declaring "dumintpart" as a float& ? Please guide me on this. Thanks.

    Code:
    // ref.cpp
    // demonstrates passing by reference
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    
       {
       void intfrac(float dumnumber, float& dumintpart, float& dumbfracpart);
    
       float number, intpart = 0, fracpart = 0;
    
       do {
    
          cout << "Enter a real number: ";
          cin >> number;
    
          intfrac(number, intpart, fracpart);
    
          cout << "Integer part is " << intpart
               << ", fraction part is " << fracpart << endl;
    
          } while( number != 0.0 );
    
       system("pause");
       return 0;
       }
    
    //--------------------------------------------------------------
    // intfrac()
    // finds integer and fractional parts of real number
    void intfrac(float dumnumber, float& dumintpart, float& dumfracpart)
       {
    
       long temp = static_cast<long>(dumnumber);
       dumintpart = static_cast<float>(temp);
       dumfracpart = dumnumber - dumintpart;
    
       }
    //---------------------------------------------------------------
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because there are numbers that fit in a float that don't fit in a long.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by tabstop View Post
    Because there are numbers that fit in a float that don't fit in a long.
    Are you alluding to the fact that long has a wide range? But the purpose is to find the integer part so simple declaration int would do. Strictly, "2.0" is not an integer. Please guide me. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jackson6612 View Post
    Are you alluding to the fact that long has a wide range? But the purpose is to find the integer part so simple declaration int would do. Strictly, "2.0" is not an integer. Please guide me. Thanks.
    You started so well, and then ... you do know what the word "wide" means, right?

    Anyway, as an example, you could use 1.12e17.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by tabstop View Post
    You started so well, and then ... you do know what the word "wide" means, right?

    Anyway, as an example, you could use 1.12e17.
    Thanks, tabstop, and sorry for asking this again.

    But don't you think using int& wouldn't hurt that much? Please let me know. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jackson6612 View Post
    Thanks, tabstop, and sorry for asking this again.

    But don't you think using int& wouldn't hurt that much? Please let me know. Thanks.
    Run the following to see the point:
    Code:
    #include <iostream>
    
    int main() {
        float foo = 1.12e17;
        long bar = foo;
        std::cout << foo << " " << bar << " " << std::endl;
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. int&, passing of arguments by reference, etc
    By jackson6612 in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2011, 03:26 AM
  2. Managed C++, passing arguments by reference
    By jimzy in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 01:03 PM
  3. passing bye parts of program
    By ReLiEnThAwK in forum C++ Programming
    Replies: 5
    Last Post: 04-06-2006, 12:02 PM
  4. Finding Seperate Parts of Date
    By drdroid in forum C++ Programming
    Replies: 1
    Last Post: 02-14-2003, 09:24 PM
  5. How to get the fraction part of a float as an integer?
    By A. Motaz in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 09:00 AM