2

I have an assignment to write a recursive function that writes the digits of a positive integer in reverse order. My problem is that the function doesn't display the reverse correctly. I know im supposed to use % or 10 when displaying the number and / of 10 when in the recursive call as well as the base case is supposed to be < 10. Here is my code.

#include <iostream>
using namespace std;

int reverse(int,int);

int main()
{
    int number;
    int n;

    cout << " Enter number to reverse." << endl;
    cin >> number;
    cout << reverse(number % 10,0);

    return 0;
}//end main

int reverse(int number,int n)
{

    if(n < 10)
    {
        return n;
    }
    else
    {
        return reverse(number/10,n);
    }
}// end reverse
Kayla Bianchi
  • 67
  • 3
  • 5
  • 11
  • 1
    It's difficult to imagine how you're trying to implement things here. For starters, your `n` is always 0, which is always < 10, thus you're always going to return 0. Even if n were variable and could sometimes be > 10, it never changes so you would enter an endless recursion. – mah Sep 04 '12 at 20:30

7 Answers7

3

I think this is what your function should be:

void reverse(int number){
    if(number == 0) //base/basic case i.e if number is zero the problem is already solved, nothing to do, so simply return
        return;
    else{
        cout << number % 10; // print that last digit, e.g 103%10 == 3 
        reverse(number/10); //solve the same problem but with smaller number, i.e make the problem smaller by dividing it by 10,  initially we had 103, now 10 
    }
}
Mr.Anubis
  • 5,132
  • 6
  • 29
  • 44
Rami Jarrar
  • 4,523
  • 7
  • 36
  • 52
  • I explained your code a bit so op can understand what code is doing and why rather than simply showing him code which will be no help to him I think – Mr.Anubis Sep 04 '12 at 20:58
1

You could use following code (if you do not mind striping leading zeros, or you could accumulate chars in string or ostringstream)

unsigned reverse(unsigned n, unsigned acc)
{
    if (n == 0)
    {
            return acc;
    }
    else
    {
            return reverse(n / 10, (acc * 10) + (n % 10));
    }
}

unsigned reverse(unsigned n)
{
    return reverse(n, 0);
}
Greg
  • 1,660
  • 13
  • 12
  • Oops, I came out with the same idea. But this method works for negatives also, keeping the sign makes some sense I guess. – ch0kee Sep 04 '12 at 21:08
1

This solution will omit trailing zeroes, because it is literally reversing the content of the integer:

int reverse(int number, int n = 0)
{
  if (number == 0)
  {
    return n;
  }
  else
  {
    int nextdigit = number%10;
    int nextprefix = n*10+nextdigit;
    return reverse(number/10 ,nextprefix);
  }
}
ch0kee
  • 736
  • 4
  • 12
0

You could also do:

int reverse(int number,int n) {
if(number > n) {
    cout << number << endl;
    reverse(number-1,n);
}

But you should get rid of first number printing twice.

Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
jviotti
  • 17,881
  • 26
  • 89
  • 148
0
int rev(int n) {
    if(n<10&&n>-10) return n;

    int length=0;
    for (int i=n; i; i/=10) length++;

    return n%10*(int)pow(10, length-1) + rev(n/10);

}

Here's my solution. It takes only one parameter and return an int. Also don't forget to include cmath.

int intLength(int i) {
    int l=0;
    for(;i;i/=10) l++;
    return l;
}

int rev(int n) {
    return n<10&&n>-10 ? n : n%10*(int)pow(10, intLength(n)-1) + rev(n/10);
}

Or this way it's a bit more elegant.

Naheel
  • 497
  • 3
  • 13
0

Constructing a void function to print is fine but if you want your function to return an reversed integer you can try this:

int revN(int n,int y=0){
   y+=n%10;
   if (n<10) return y;
   return revN(n/10,10*y); 
}

Here y=0 indicates y takes default value of 0 when not passed as arguments. Ex: need to find a reversed integer of 576.

int x= revN(576);
prateek
  • 1
  • 2
0

I'm not sure is this good or not. I'm still learning about recursive as well.

int reverseNumber(int number, int mod) {
    if(number==0) return mod;

    return (mod * pow(10, floor(log10(number))+1)) + reverseNumber(number/10, number%10);
}

int main() {
    int n; cin>>n;
    cout<<reverseNumber(n,0);
}
Ananda Vj
  • 172
  • 10