Recursive Function to Reverse an Integer

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2008
Posts: 3
Reputation: badmofo is an unknown quantity at this point 
Solved Threads: 0
badmofo's Avatar
badmofo badmofo is offline Offline
Newbie Poster

Recursive Function to Reverse an Integer

 
0
  #1
Nov 4th, 2008
I have an assignment to write a recursive function that takes one integer variable and output the integer in reverse order to the screen. I've tried converting it to a char array(c++ "string" class not allowed) but could not get a counter to work. I can reverse an int array with and "upper" and "lower" variable no problem, but I'm stuck without them.

???? reverse(int num) //This is what the assignment calls for.

I'm not looking for anyone to write it for me but I would appreciate some tips.
Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 36
Reputation: freudian_slip is an unknown quantity at this point 
Solved Threads: 4
freudian_slip's Avatar
freudian_slip freudian_slip is offline Offline
Light Poster

Re: Recursive Function to Reverse an Integer

 
0
  #2
Nov 4th, 2008
Why would you need to use a char array? using int array should be fine.

reverse (num)
   array[count] = num
   count ++
   get a new num
   reverse (num)
   print out array[count]
   count--

It might be logically flawed somewhere..just make sure you have a base case (where you would stop calling the reverse function).
Last edited by freudian_slip; Nov 4th, 2008 at 12:54 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Recursive Function to Reverse an Integer

 
0
  #3
Nov 4th, 2008
If you're just displaying the reversed thing to the screen, there is no need to store any numbers in any arrays.

Each call to the function will print out a single digit (the digit of number in the units place). If the number is more than 9, the function will call itself with number/10 as the argument. This will recursively print out the number in reverse. When all digits have been printed, the function will stop, and return, due to the above stated call condition.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,023
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 300
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic

Re: Recursive Function to Reverse an Integer

 
0
  #4
Nov 4th, 2008
raw scetch of how I should start to do it:
revprint(int n)
{
if n==0 return
cout<<n%10
revprint(n/10)
}
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Recursive Function to Reverse an Integer

 
0
  #5
Nov 4th, 2008
I'd do it that way too, just I'd add a little if-else-abs() trick to handle n<0 (otherwise it would print -567 reversed as -7-6-5).

something like
  1. if(n<0) {
  2. cout << "-" << abs(n%10);
  3. revprint(n/-10);
  4. }
  5. else {
  6. cout << n%10;
  7. revprint(n/10);
  8. }
Last edited by mrboolf; Nov 4th, 2008 at 10:32 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 3
Reputation: badmofo is an unknown quantity at this point 
Solved Threads: 0
badmofo's Avatar
badmofo badmofo is offline Offline
Newbie Poster

Re: Recursive Function to Reverse an Integer

 
0
  #6
Nov 5th, 2008
Thanks everyone! I got it working using "n % 10" I was making it too complicated as usual
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,023
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 300
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic

Re: Recursive Function to Reverse an Integer

 
0
  #7
Nov 5th, 2008
So you can mark this as solved.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC