reversing digits

This is a discussion on reversing digits within the C++ Programming forums, part of the General Programming Boards category; I'm just looking for a hint in the right direction if someone could help me please. The first part works ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    14

    Unhappy reversing digits

    I'm just looking for a hint in the right direction if someone could help me please. The first part works good, but i can't the the call by reference to reverse the digits for me. It outputs a 0 or it outputs the numbers in the same order that i input them.
    Code:
    // reversedigits.cpp
    // written by 
    // cis
    
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    int revdigits ( int ); // call by value
    void revdigits1 ( int, int& );// call by reference
    int width ( int ) ;
    void width1 ( int, int& );
    
    int main()
    
    {
    	int number = 0,
    		r = 0;
    	cout << "Enter a number between 1 and 9999: " << fixed;
    	cin >> number;
    	cout << "The number with its digits reversed is: " << setw ( width ( number ) ) <<  revdigits ( number  ) << endl;
    	void revdigits1 ( int n, int r );
    	cout << "The number with its digits reversed is: " ;
    	void width1 ( int n,int r );
    	cout << r << endl;
    
    	return 0;
    }
    
    int revdigits ( int n )
    
    {
    	int reverse = 0,
    		divisor = 1000,
    		multiplier = 1;
    	while  ( n > 10 ) 
    	{
    		if  ( n >= divisor )
    		{
    			reverse += n / divisor * multiplier;
    			n %= divisor;
    			divisor /= 10;
    					multiplier *= 10;
    
    		}
    		else
    			divisor /= 10;
    	}
    	reverse += n * multiplier;
    	return reverse;
    }
    
    void revdigits1 ( int n, int &r )
    
    {
    	int reverse = 0,
    		divisor = 1000,
    		multiplier = 1;
    	while  ( n > 10 ) 
    	{
    		if  ( n >= divisor )
    		{
    			reverse += n / divisor * multiplier;
    			n %= divisor;
    			divisor /= 10;
    					multiplier *= 10;
    
    		}
    		else
    			divisor /= 10;
    	}
    	reverse += n * multiplier;
    	r = reverse;
    }
    
    int width ( int n )
    {
    	if ( n / 1000 )
    		return 4;
    	else if ( n / 100 )
    		return 3;
    	else if ( n / 10 )
    		return 2;
    	else
    		return 1;
    }
    
    
    void width1 ( int n, int &r )
    {
    	if ( n / 1000 )
    		r = 4;
    	else if ( n / 100 )
    		r = 3;
    	else if ( n / 10 )
    		r = 2;
    	else
    		r = 1;
    }

  2. #2
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    First off you don't need to put void before your function call ( the call not the header). So right here:
    Code:
    	cout << "The number with its digits reversed is: " << setw ( width ( number ) ) <<  revdigits ( number  ) << endl;
    	void revdigits1 ( int n, int r );
    	cout << "The number with its digits reversed is: " ;
    	void width1 ( int n,int r );
    The void is unnescecary(sp?), I'll add more later.
    Last edited by CheesyMoo; 03-26-2003 at 08:45 PM.
    If you ever need a hug, just ask.

  3. #3
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    I don't understand what you're trying to do in this next code portion:
    Code:
    int width ( int n )
    {
    	if ( n / 1000 )
    		return 4;
    	else if ( n / 100 )
    		return 3;
    	else if ( n / 10 )
    		return 2;
    	else
    		return 1;
    }
    If n / 10 is a positive number, it will be true. So if someone enters 24 for the n this part:
    Code:
    if(n / 1000)
     return 4;
    Will return 4 because it is a positive number, not because it's 4 digits long.

    I think what you want to do is:
    Code:
    if(n / 1000 >= 1)
       return 4;
    Unless I'm on drugs everything I said is mostly correct.
    If you ever need a hug, just ask.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    14
    Ok, the void is part of my call by reference and i think that it needs to be there. The call by value part of my program seems to work ok. this part of the code
    Code:
    int width ( int n )
    {
    	if ( n / 1000 )
    		return 4;
    	else if ( n / 100 )
    		return 3;
    	else if ( n / 10 )
    		return 2;
    	else
    		return 1;
    }
    once the calculations are done in the code this part breaks the number down to a single digits so that it can put in reverse order
    now then what i really do not unerstand is this
    Code:
    void width1 ( int, int& ); // call by reference if i am correct function prototype
    i'm having problems with the implimintation of this function
    it worked ok doing it with call by value but i'm not sure about the call by reference.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    There's nothing wrong with your function. The problem is with your function call. The way you wrote it, your function call is trying to re-declare the function. The call should just name the function and send the parameters; it shouldn't declare them. Also, you're introducing a new variable n that has no value.

    In main() just make this simple change:

    replace
    void revdigits1 ( int n, int r );

    with:
    revdigits1 ( number, r );

    and it will compile and run correctly.


    One more thing, also in main(), this line:
    void width1 ( int n,int r );
    is not doing anything at all & should not be there. It's not interfering with anything, but it's not doing anything either. If you comment it out you will see that nothing changes.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    14
    thanks for the help. I'm still kind of comfused though in the part of the code that my teacher gave me it had this.

    void width1(int, int&);

    I thought that maybe it was somehow suppost to be used to reverse the order of the digits.
    Code:
    void width1 ( int n, int &r )
    {
    	if ( n / 1000 )
    		r = 4;
    	else if ( n / 100 )
    		r = 3;
    	else if ( n / 10 )
    		r = 2;
    	else
    		r = 1;
    }
    so is any of this even necessary in my code for reversing the digits?

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Depends on the requirements of the program. Sometimes teachers do things illogically to demonstrate a point or provide practice. Without knowing the requirements it is difficult to say whether the steps you have taken are necessary or not. To just show the digits in reverse order without consideration for how you do it, the step isn't necessary. For example, here's code that displays the value of num passed to the function by value without any return or reference necessary. You can't do anything with the reversed digits, but the digits do appear on the screen in reverse order, and the value of num can be any valid int, not just an int with 4 digits.

    void revDigits(int num)
    {
    int mod;
    while(num > 9)
    {
    mod = num % 10;
    cout << mod;
    num /= 10;
    }
    }

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    14
    He wanted us to write a program that can display and number from 1 to 9999 in reverse order by using call by reference and call by value
    and gave us the prototypes
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int reversedigits(int);
    void reversedigits1(int, int&);
    int width(int);
    void width1(int, int&);
    
    
    int main()
    {
    	int number = 0;
    	
    	cout << "Enter a number between 1 and 9999: ";
    	cin >> number;
    
    	cout << "The number with its digits reversed is: ";
    	cout << setw(width(number)) << reversedigits(number);
    	cout << endl;
    
    	return 0;
    }
    
    
    int reversedigits (int n)
    {
    	int reverse =0;
    	int divisor = 1000;
    	int multiplier = 1;
    
    	while( n > 10) {
    		if (n >= divisor) {
    			reverse += n / divisor * multiplier;
    			n %= divisor;
    			divisor /= 10;
    			multiplier *= 10;
    		}
    		else
    			divisor /= 10;
    	}
    
    	reverse += n * multiplier;
    	return reverse;
    }
    
    void reversedigits1(int n, int &r)
    {
        int reverse =0;
    	int divisor = 1000;
    	int multiplier = 1;
    
    	while( n > 10) {
    		if (n >= divisor) {
    			reverse += n / divisor * multiplier;
    			n %= divisor;
    			divisor /= 10;
    			multiplier *= 10;
    		}
    		else
    			divisor /= 10;
    	}
    
    	reverse += n * multiplier;
    	r = reverse;
    }
    
    int width(int n)
    {
        if(n / 1000)
    		return 4;
    	else if(n / 100)
    		return 3;
    	else if(n / 10)
    		return 2;
    	else
    		return 1;
    }
    this is the code that he gave us all we had to do was finish it.

    thas is why i keep trying to figure out what to do with this part

    void width1(int, int&);
    Last edited by lizardking3; 03-27-2003 at 10:00 AM.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If this does what you want:

    cout << "The number with its digits reversed is: " << setw ( width ( number ) ) << revdigits ( number ) << endl;


    and you want the functions using the reference parameters to mimic the above code then change this:

    void revdigits1 ( int n, int r );
    cout << "The number with its digits reversed is: " ;
    void width1 ( int n,int r );
    cout << r << endl;

    to this:

    revdigits1 (number, r);
    int w = 0;
    width1 (number, w);
    cout << "The number with its digits reversed is: " << setw(w) << r << endl;

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    14
    thanks

  11. #11
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Okay sorry I thought that you were trying to find how many digits were in the number.
    If you ever need a hug, just ask.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    CheesyMoo:

    The way I understand it is that the value 0 will be interpreted as false in any situation where true/false is called for, such as in conditionals, whereas any other value, positive or negative, will be interpretted as true.

    if n is an int with the value of 24 then

    n/1000 will be 0 by property of integer math

    so the when used as a conditional as in

    if(n/1000)

    the conditional will be false.

    Therefore, I believe that width() and width1() will return the number of digits in number, as long as number is less than 9999 and greater than 0. I'm not sure what the instructor wants the students to do with width() and width1() (using them in setw() seems a little trivial; but I've been known to do worse), but I think they are functional as is.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Just for your info, here's another number reversing thread
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Feb 2003
    Posts
    14
    Something that my teacher did tell us though is that there is a bug in the code. If you enter any number that has a zero in it the zero will not be displayed. I don't know why but that seems to hold true. If you enter 0000 it will output just one zero. If you enter 1001 then it outputs 11 or 1203 would output 321. Seems to be a problem.

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>If you enter 0000 it will output just one zero
    And you consider that to be a problem? In numeric terms, 0000 is the same as 0.

    If you want to reverse the extact number as entered, eg keep the leading zeros, you'll have to read it as a string, and reverse that instead. This means you won't actually treat it as a real number.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reversing digits in C
    By Digital Thought in forum C Programming
    Replies: 22
    Last Post: 10-15-2010, 06:34 AM
  2. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  3. Hex digits stored in char array.
    By Kevinmun in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 03:05 PM
  4. Reversing Digits Project (Help Please)
    By fenixataris182 in forum C++ Programming
    Replies: 9
    Last Post: 07-12-2005, 01:08 PM
  5. Reversing the order of the digits?
    By Mak in forum C Programming
    Replies: 15
    Last Post: 10-09-2003, 09:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21