Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

Dave DiRito

Ranch Hand
+ Follow
since Feb 08, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Dave DiRito

Campbell, apparently our time zones are several hours apart as I am only just now seeing your posts (5:30 AM, 4/24).

Yes, the code does work for negative numbers I believe because it's always multiplying a negative number by a positive as it goes through the recursive cycle.

Using the ? : notation makes it even more elegant. Am I correct in reading it as "If i equals 0, then return 0, else do the recursion loop"?

I am not familiar with the call stack notation you posted, so I can't really interpret it. Thanks to working through this problem with your and Paul's help especially, I do have a better understanding of recursion.

I understand that each method call stacks up on the one before until it reaches the stop point (in this case when i == 0). Then it unwinds by returning it's value (result, in this case) to the one previous to it in the stack until it gets down to the first one which returns it's value to wherever it was called from in the first place (in my program, that would be the main method).

Is that a correct understanding of it?

Dave
13 years ago
Paul, actually it works just fine as is. The spec for it was that ff a negative number is passed to it, say -893, then it should return -889933. That's exactly what it does! I believe that's because the last step always involves multiplying a positive number by a negative one.

I owe you a great big thank you, too!
You started me off in the right direction and kept me on course in the early going. So an infinitely recursive thankYou(thankYou) to you, too.

Dave
13 years ago
Campbell, thank you, thank you so much for hanging in there with me. If it wasn't for your hints and encouragement along the way, I would have never gotten this. It has literally brought me to tears of joy and loud wooping sounds

The code is so elegant it's beautiful and I believe I actually understand it.
thankYou(thankYou)
13 years ago
This seems to be slightly closer. It still works for 0 and single digit numbers and it tacks the single digit numbers after they're doubled onto the larger numbers. It just doesn't go through the recursion to continue the process for the larger numbers.
13 years ago
Thanks for the encouragement, Campbell. I was growing very weary. I haven't banged my head against something this much in a long time.
This code works correctly for 0 and any single digit number:
13 years ago
Ok, I see a pattern:

ones*11*10 to the 0 power doubles the ones digit.
ones(in the next pass will be the tens digit in the original number, if it has a tens digit)*11*10 to the 2nd power doubles the tens digit.
ones(in the third pass will be the hundreds digit in the original number, if it has a hundreds digit)*11*10 to the 4th power doubles the hundreds digit.
etc...

Is this getting closer?
13 years ago
Thank you, Campbell and Garrett. I really want to get this and I appreciate you hanging in and helping me.

I am working on it right now, Campbell. I have taken your advice and made the method return an int, as required, and tried to simplify it. Currently, it works for 0 and single digit numbers only. For numbers greater than 10, it breaks off each digit and turns it into doubles but does not combine them back together into the final produce. That's what I'm missing and I think that's the part that needs recursion to work properly.

Thank you again,
Dave
13 years ago
Sorry to be so dense about this. I greatly appreciate everyone's help walking me through it.

From Garrett's post I see the pattern that to turn the ones into double digits you multiply it by 11, the tens digit becomes a ten thousand number by multiplying by 1100, the hundreds digit becomes a hundred thousand number by multiplying by 110,000. So, I must need some kind of log function like Piet just posted or something to raise 11 to some power of 10 based on the size of the number that's passed into the method. Is that what I need?
13 years ago
Thanks, Campbell.

OK, going back to basics, I see that I can turn 147 into 114477 by doing:
114477 = (7 + 7*10) + (4 + 4*1000) + (1 + 1*10000)

I know how to break apart 147 into each part 7, 4, and 1, to get the numbers I need for the equation above. I just can't figure out how to put this all together into recursive code to get the final number 114477.

I realize I need a base case and I kept trying quotient == 0. That didn't work until I set quotient equal to the original number at the very top of the method. I still don't know what should happen when quotient gets to 0.

Here's my code right now followed by the output it produces. I replaced the variable name quotient with q and I am not yet sending it any negative numbers.

Thanks,
Dave


Enter a number for Double Digits: 147

q = 14
ones = 7
result before call = 77
q = 1
ones = 4
result before call = 44
q = 0
ones = 1
result before call = 11
0
q at 0, num at 0
num = 11
num = 44
num = 77

13 years ago
Ok, I can make 14 into 1144 when the quotient is at 1 and the ones is at 4 by doing:
num = quotient*1000 + quotient*100 + ones*10 + ones
1144 = 1*1000 + 1*100 + 4*10 + 4

I think I need something similar to that, or maybe exactly that, to make 1144 into 114477. I'm not sure if this is correct. Even if it is correct I don't know where or how to fit that in to make it work recursively.

Am I on the right track at all?

Dave
13 years ago
Paul, I greatly appreciate your help with this. Thank you!

I believe what you're telling me is that I need a statement somewhere that puts the pieces back together again. I think it may be something like:
result = result*100 + result;

I think I am also missing the stop condition as Katrina indicated in her psuedocode. I think that it should do the recursion part of taking the number apart by stripping the ones off the end, turning them into doubles (so 7 becomes 77) until it reaches the stop point (I think that should be when quotient == 0). From there it should go back through the stack of method calls and put the number back together with each digit doubled.

Is that how it should work?

Thank you again for your help,
Dave
13 years ago
Garrett, thanks for the suggestion. I don't think a helper method would be permitted as a solution, though. I keep thinking there is some piece I'm missing that has to do with putting the number back together again with the digits doubled that involves "unwinding" part of the recursion after it reaches the stop point.

I can make it look like it's producing the correct result with a System.out.print(result); statement, but if I make it a println statement or make it a method that returns a value, it reveals that I really haven't created an integer with all the digits doubled.

Here is my code as it exists right now along with the results it produces. The negative number condition doesn't work either, but I'm not worried about that until I get the positive number condition working properly first:

Enter a number for Double Digits: 147

quotient = 14
ones = 7
result before dd = 77
quotient = 1
ones = 4
result before dd = 44
quotient = 0
ones = 1
result before dd = 11
-1
114477
13 years ago
Looking at Katrina's psuedocode and comparing to my code I see that I do not have an end condition to stop the recursion at the appropriate place. I can't figure out what the end condition should be. I keep thinking it's when the quotient equals zero because that's when there's no more number left to split up. Also, I'm not sure when should happen when the end condition is reached.

Since I cannot figure out these two things, I'm using the trial and error approach. I either get an infinite loop or an incorrect result.

Still hoping someone will take pity and give me some clue that will help enlighten me.
13 years ago
Thanks, Katrina. That did help. I decided to change the name of my remainder variable to quotient to avoid confusion. I was using remainder to mean the part of the integer that remains after dividing by 10, but then I realized that part of division is called the quotient. Remainder actually refers to what's left over, which in my code I left as ones.

Anyway, here's as close as I got so far when I pass it a positive integer. I'll deal with negative integers after I get this working correctly.

This code produces the following results if I pass it the number 147 for example:

ones = 7
quotient = 14
result = 77
ones = 4
quotient = 1
result = 44
ones = 1
quotient = 0
result = 11
0114477

As you can see, it's great except for that 0 in front. I believe what's causing it is that I'm calling the method when quotient = 0. I have tried a million things to fix it and nothing works. I would greatly appreciate any suggestions anyone has to help me figure out how to fix it.

Thanks,
Dave
13 years ago
I now have two variables called ones and remainder. How do you set it up to cycle through the number and then put it back together again with each digit doubled?
13 years ago