i have one question; would this be a recursive or non recursive algorythm; convert the given integer to a string using x.toString() a method that exits in java.lang and then usign a for loop print the character at each index of the string via println command? The forloop:
tested this procedure proves to be working:
for (int i = 0; i < c.length(); i++) { System.out.println(c.charAt(i)); }
@6:55, nice try. Yes that works, but I should have mentioned that converting the integer to a string should not be allowed, because it makes the problem too easy :-)
you have instructed in the lab that we do not use the method numDigits to write our recursive method. However, I am having trouble writing a recursive method that does not count the number of digits in it. Are we supposed to implement the method without counting the number of digits, or just find a different way to count the digits?
@6:36: You can use numDigits in the recursive method, but it's discouraged -- I don't know what you would use it for. The simplest solution doesn't use it.
To write the recursive method you have to think very simply. Don't over complicate the problem. Decide on a stopping case, and then decide how the problem will converge to that stopping case.
Just to give you an idea, not considering the if-else keywords and braces, my recursive method is literally 3 lines of code. It looks ridiculously simple, almost too simple, but that's exactly the power of recursive methods.
Is it possible for someone to post a recursive method solution? I have been trying to solve it for a while now and I am kind of lost as to how to do it, thank you so much beforehand!
Hi Prof,
ReplyDeleteI had a doubt for lab 7: do we have to sort the elements after extracting them or not? as the tester already has them in order.
There is no order. You're give just ONE number. In the tester that number happens to be 234,567. It's an integer, you just print it vertically.
ReplyDeleteIf you're given 637 you should print:
6
3
7
NOT:
3
6
7
because that would be 367. Get it?
@ anon(March 21, 2011 10:02 AM )
ReplyDeletethank you :)
i have one question; would this be a recursive or non recursive algorythm;
ReplyDeleteconvert the given integer to a string using x.toString() a method that exits in java.lang
and then usign a for loop print the character at each index of the string via println command?
The forloop:
tested this procedure proves to be working:
for (int i = 0; i < c.length(); i++)
{
System.out.println(c.charAt(i));
}
@6:55, nice try. Yes that works, but I should have mentioned that converting the integer to a string should not be allowed, because it makes the problem too easy :-)
ReplyDeletethank u prof.
ReplyDeleteSir,
ReplyDeleteyou have instructed in the lab that we do not use the method numDigits to write our recursive method. However, I am having trouble writing a recursive method that does not count the number of digits in it. Are we supposed to implement the method without counting the number of digits, or just find a different way to count the digits?
@6:36: You can use numDigits in the recursive method, but it's discouraged -- I don't know what you would use it for. The simplest solution doesn't use it.
ReplyDeleteYes sir,
ReplyDeleterealized how simple the solution is without it... hahaha... thank you
It looks like the one we did in class.
ReplyDeleteHow do you separate the first digit of the number from the rest of the digits?
To write the recursive method you have to think very simply. Don't over complicate the problem. Decide on a stopping case, and then decide how the problem will converge to that stopping case.
ReplyDeleteJust to give you an idea, not considering the if-else keywords and braces, my recursive method is literally 3 lines of code. It looks ridiculously simple, almost too simple, but that's exactly the power of recursive methods.
@Anonymous(March 23, 2011 10:21 AM)
ReplyDeleteJust out of curiousity, how many lines do you have on your non recursive method?
@ 11.44
ReplyDeleteOddly enough, I was having trouble with the non-recursive method. It seemed like it was going to be longish.
But, in the end it was just a single line inside a for-loop.
Recursion isn't meant for these implementations though.
Is it possible for someone to post a recursive method solution? I have been trying to solve it for a while now and I am kind of lost as to how to do it, thank you so much beforehand!
ReplyDelete@Anon 2:04 PM
ReplyDeleteTry chapter 11 in the textbook.
any hints on how to start?..... anyone?
ReplyDelete@ 9:30AM
ReplyDeleteTake a look at (March 23, 2011 5:01 PM) and (March 23, 2011 10:21 AM)
I wrote the non-recursive method in one line inside for loop.
ReplyDelete