reverse order

I have hard time understand this question. English is not my lang.

 
Write a recursive function to display decimal digits of an arbitrary integer in reverse order. 
In short write a function int reverse (int arbitraryInteger);

1
2
//which called like this
reverse(1234567);
//will display
765321

Making it recursive is a tip, I suppose :)
1
2
3
4
5
int int reverse (int arbitraryInteger)
{
    int i = /*some magic number */ 0;
    return /*some magic algorithm */ + reverse (i);
}
Last edited on
The instruction is to display the integer in reverse order -- not to compute the actual reverse order integer.

1
2
3
4
5
6
7
8
9
10
11
void display_in_reverse( int n )
{
  cout << /* one digit of n */ ;

  n = /* do something to n */

  if (/* when do I not stop? */)
  {
    display_in_reverse( n );
  }
}

Be sure to consider what happens when n is zero. Because you want the following things to display properly:

1
2
3
4
5
display_in_reverse( 0 );

display_in_reverse( 207 );

display_in_reverse( 3 );
0

702

3

But you certainly don't want to see

 
display_in_reverse( 32 );
230

The hint I have given you will help you past that problem.

Hope this helps.
Topic archived. No new replies allowed.