954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with Recursion

Hello. I need to write a static recursive method that returns the frequency of occurrence a particular digit d in an integer n. For example, if passed (1342457,4) it should return 2, whereas when passed (1342457,6) it should return 0.

I won't put my code in as it is arbitrary, I just need help with the logic in this one; I can't grasp what needs to be done.

I should also note that no variables are allowed besides the two that the user inputs as the integer, and digit to be checked for (so it must be calculated completely recursively).

Farfie
Newbie Poster
3 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 
I should also note that no variables are allowed besides the two that the user inputs as the integer, and digit to be checked for

I think this requires the use of at least one more variable. I would read the integer in as a string, create an array of all it's char's and then compare each one to the digit.

jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53
 

I don't know how to delete this post, ha. But I'm writing over it here to make this thread less confusing..

Farfie
Newbie Poster
3 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 
I think this requires the use of at least one more variable. I would read the integer in as a string, create an array of all it's char's and then compare each one to the digit.

Yeah, that's what I would do in a real world situation as well, also, I agree in the thinking that there needs to be at least one more variable.. Problem is, I can very distinctly remember my professor saying that no more variables are allowed.

Here's an example to get your mind jogging just in case.

// Returns the largest element in an integer array
public static int maxInArray(int[] a, int size)
{
if (size==1)
return (a[0]);
else
{
int temp = maxInArray(a, size-1);

if(temp>a[size-1])
return temp;
else
return a[size-1];
}
}


Also note that he did use a variable in this one, so I guess it's a bad example. I would love to argue with someone about the need of another variable to compute this but I simply don't have the experience with recursion to make a statement (this is my first assignment on recursion, just started the chapter last week).

Farfie
Newbie Poster
3 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Here's a hint for you.

If you cannot separate the entered number into characters, the only way you can test individual digits is to divide the number down. If you use modular division (%10) the result will be the final digit in the number which can be tested. Using integer division by 10 would discard the final digit and leave the rest for further testing.

stevelg
Light Poster
34 posts since Oct 2009
Reputation Points: 23
Solved Threads: 7
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: