# passing arguments by reference - finding integer and fraction parts

• 05-15-2011
jackson6612
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;   } //---------------------------------------------------------------```
• 05-15-2011
laserlight
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.
• 05-15-2011
jackson6612
Quote:

Originally Posted by laserlight
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?

Quote:

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
• 05-15-2011
tabstop
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.
• 05-15-2011
jackson6612
Thanks a lot, tabstop.
• 05-15-2011
jackson6612
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;   } //---------------------------------------------------------------```
• 05-15-2011
tabstop
Because there are numbers that fit in a float that don't fit in a long.
• 05-15-2011
jackson6612
Quote:

Originally Posted by tabstop
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.
• 05-15-2011
tabstop
Quote:

Originally Posted by jackson6612
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.
• 05-15-2011
jackson6612
Quote:

Originally Posted by tabstop
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.
• 05-15-2011
tabstop
Quote:

Originally Posted by jackson6612
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; }```