473,473 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Recursion

7 New Member
I can get any number to work as long as it doesnt include a 0. If it has a 0 the reversed number just comes out with the 0 on the end. I cannot figure out why. I think may have just made it too complicated and need to redo it. But if someone can help me I would appreciate it

Expand|Select|Wrap|Line Numbers
  1. int reverseDigit(int a)
  2. //reverseDigit reverses the order of the digits in an integer. So 1234 becomes 4321
  3. //It only has to be passed the integer, and it returns its reversed order
  4. {
  5.     int x = 1; //for while loop
  6.     if(a < 10) return a;
  7.     else 
  8.     {
  9.         int b = (a%10)*10;
  10.         cout << b << endl;
  11.         int c = reverseDigit(a/10);
  12.         if(b==0) return (c*10);
  13.         else if(c < 10) return (c+b);//if c is less than 10 it only needs to be added to b
  14.         else //else we have to find how big b has to be for the next decimal point past c
  15.         {
  16.             while(x <= c) x = x*10;
  17.             b = x*(a%10);
  18.             return (b+c);
  19.         }
  20.     }
  21. }
  22.  
Oct 30 '07 #1
1 1241
Banfa
9,065 Recognized Expert Moderator Expert
Your problem is this line

Expand|Select|Wrap|Line Numbers
  1.       if(b==0) return (c*10);
*10 puts the 0 in the wrong place, it adds it to the right hand end of the number where as you need to add the digits to the left hand end.

However knowing that does help much, if you reverse 1203 using your function it will do this

1. stick 3 on the left hand end of 120 reversed
2. stick 0 on the left hand end of 12 reversed
3. stick 2 on the left hand end of 1 reversed

the value of step 3 is 21, however the value of step 2 is 021 but since leading 0 have no meaning in a binary number this is 21. Step 2 and 3 return the same value.


This is quite a classic example, it is very complicated to write a function that reverse the digit recursively using the interface function protoype. Using a slightly altered prototype it is simple, I solved the problem using this function structure

Expand|Select|Wrap|Line Numbers
  1. static int rd_helper(int a, int ra)
  2. {
  3. <code snipped, after all you need something to do :D>
  4. }
  5.  
  6. int reverseDigit(int a)
  7. //reverseDigit reverses the order of the digits in an integer. So 1234 becomes 4321
  8. //It only has to be passed the integer, and it returns its reversed order
  9. {
  10.   return rd_helper(a, 0);
  11. }
  12.  
Not the original function is used as an interface onto another, recursive function with a slightly different prototype. This passes extra data at each recursion, the user does not need to know about this data but doing it this way means that the function rd_helper only contains a handful of code lines (a recursion end statement, a calculation line and a call to itself).


The recursion is simple using the right data but the interface function does not use all that data.
Oct 30 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
2
by: Csaba Gabor | last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get any rise from them when I posted this a week ago, so I am reposting this to some of my favorite programming groups: I'm...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
13
by: robert | last post by:
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception. What...
20
by: athar.mirchi | last post by:
..plz define it.
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.