943,742 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 8020
  • C++ RSS
Nov 4th, 2008
0

Recursive Function to Reverse an Integer

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
badmofo is offline Offline
3 posts
since Nov 2008
Nov 4th, 2008
0

Re: Recursive Function to Reverse an Integer

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.
Reputation Points: 11
Solved Threads: 4
Light Poster
freudian_slip is offline Offline
36 posts
since Oct 2008
Nov 4th, 2008
0

Re: Recursive Function to Reverse an Integer

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.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Nov 4th, 2008
0

Re: Recursive Function to Reverse an Integer

raw scetch of how I should start to do it:
revprint(int n)
{
if n==0 return
cout<<n%10
revprint(n/10)
}
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Nov 4th, 2008
0

Re: Recursive Function to Reverse an Integer

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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Nov 5th, 2008
0

Re: Recursive Function to Reverse an Integer

Thanks everyone! I got it working using "n % 10" I was making it too complicated as usual
Reputation Points: 10
Solved Threads: 0
Newbie Poster
badmofo is offline Offline
3 posts
since Nov 2008
Nov 5th, 2008
0

Re: Recursive Function to Reverse an Integer

So you can mark this as solved.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Sep 24th, 2010
0
Re: Recursive Function to Reverse an Integer
if we give a input which has a digit 0 in between it then loop will end there and output will be incorrect
for eg 1034
the loop will end at the second digit 0 and otput will be incorrect

what to do in this case??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anuj khasgi is offline Offline
1 posts
since Sep 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: text editor
Next Thread in C++ Forum Timeline: Reading an Input File into 3 Arrays?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC