Sunday, March 20, 2011

Lab 7, lab 6 signins

Lab 7 has been posted here. This lab is about recursion.

Lab 6 signins are here.

18 comments:

  1. Hi Prof,
    I 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.

    ReplyDelete
  2. 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.

    If you're given 637 you should print:
    6
    3
    7

    NOT:

    3
    6
    7

    because that would be 367. Get it?

    ReplyDelete
  3. @ anon(March 21, 2011 10:02 AM )
    thank you :)

    ReplyDelete
  4. 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));
    }

    ReplyDelete
  5. @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 :-)

    ReplyDelete
  6. Sir,

    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?

    ReplyDelete
  7. @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.

    ReplyDelete
  8. Yes sir,

    realized how simple the solution is without it... hahaha... thank you

    ReplyDelete
  9. It looks like the one we did in class.
    How do you separate the first digit of the number from the rest of the digits?

    ReplyDelete
  10. 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.

    ReplyDelete
  11. @Anonymous(March 23, 2011 10:21 AM)
    Just out of curiousity, how many lines do you have on your non recursive method?

    ReplyDelete
  12. @ 11.44

    Oddly 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.

    ReplyDelete
  13. 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
  14. @Anon 2:04 PM

    Try chapter 11 in the textbook.

    ReplyDelete
  15. any hints on how to start?..... anyone?

    ReplyDelete
  16. @ 9:30AM
    Take a look at (March 23, 2011 5:01 PM) and (March 23, 2011 10:21 AM)

    ReplyDelete
  17. I wrote the non-recursive method in one line inside for loop.

    ReplyDelete