Fuck Java, I need help again

Discussion in 'OT Technology' started by SiempreFresco, Nov 9, 2005.

  1. SiempreFresco

    SiempreFresco New Member

    Joined:
    Jun 16, 2003
    Messages:
    7,142
    Likes Received:
    0
    Location:
    NYC
    Fuck java and recursive algorithms. Will there ever be a point where I just "get it" ? I still don't feel competent at all at programming even though I've been doing it for class for over 2 months now.

    Anyway I need help once again, any hlp/tips would be appreciated:
    1. Write and implement a recursive algorithm to find the maximum element in an array of integers. Your algorithm should be implemented as a static method. Write a driver class to demonstrate the operation of your method.

    2. Implement Euclid's algorithm for finding the greatest common divisor of two positive integers. Create a driver to test your implementation. (See Programming Project 11.2 on p. 606 of L&L.)
     
  2. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    err i dont really want to touch java after C# =/
     
  3. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    So what is it that you're having trouble with? It sounds like you're having more trouble with the concept than with the language.
     
  4. Bruticus

    Bruticus half dead OT Supporter

    Joined:
    Apr 10, 2004
    Messages:
    4,608
    Likes Received:
    0
    Location:
    Melbourne
    Yeh...

    It's just a simple loop.

    something like (i'll write it c style, you can convert it however)

    Code:
    int max;
    
    for( i = 0 ; i < ARRAYLENGTH ; i++ ){
      if( i == 0 ){ /* first value */
        max = array[i];
      } else {
        if( max < array[i] ){
          max = array[i];
        }
      }
    }
    
    Just steps through the array value by value, updating the max value if it finds a bigger one. Pretty basic to do it this way.
     
  5. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    You missed the recursive part of the assignment ;)
     
  6. Bruticus

    Bruticus half dead OT Supporter

    Joined:
    Apr 10, 2004
    Messages:
    4,608
    Likes Received:
    0
    Location:
    Melbourne
    Oh lol gg speed reading :) I gotta study for exam tomorrow so I can't be fucked writing more code :p Still recursive isn't much harder.
     
  7. SiempreFresco

    SiempreFresco New Member

    Joined:
    Jun 16, 2003
    Messages:
    7,142
    Likes Received:
    0
    Location:
    NYC
    :werd:

    I guess its a bit of both I don't fully understand what Im writing sometimes when Im writing it, if that makes sense :dunno:
    But I also am having trouble with recursive
     
  8. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    my first year of java in high school, i didnt really understand it for the first 4 or 5 months. after that it just all of a sudden clicked and i really understood what i was doing. at the beginning of the year, i was getting c's and d's, but after i started understanding it i got all a's for the rest of the year and all through the ap class my senior year. for some people it just takes longer to understand what is actually going on.
     
  9. SiempreFresco

    SiempreFresco New Member

    Joined:
    Jun 16, 2003
    Messages:
    7,142
    Likes Received:
    0
    Location:
    NYC
    So any help on the programming? :x:
     
  10. sam758

    sam758 OT Supporter

    Joined:
    Aug 26, 2003
    Messages:
    901
    Likes Received:
    0
    the pseudocode for #1 should be something like:

    max(array, ndx)
    if (ndx >= length(array) - 1) // base case.
    return array[ndx];
    else
    next = max(array, ndx + 1)
    return array[ndx] > next ? array[ndx] : next
     
  11. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    The idea of recursion is that you break a problem down bit by bit until it's at a base case where the answer is trivial. So, say we were trying to evaluate a factorial (five factorial = 5! = 5 * 4 * 3 * 2 * 1).

    1! = 1, so this is a base case - the answer is very trivial.

    So, our code would do:
    5! = 5 * 4!
    4! = 4 * 3!
    3! = 3 * 2!
    2! = 2 * 1!
    1! = 1 (return to 2! and on up the stack)

    The maximum-finding problem is not a problem that lends itself naturally to recursion, but i can be done recursively. Euclid's algorithm, on the other hand, is by its very nature recursive, so it only makes sense to write it recursively.
     
  12. SiempreFresco

    SiempreFresco New Member

    Joined:
    Jun 16, 2003
    Messages:
    7,142
    Likes Received:
    0
    Location:
    NYC
    I figured it out thank everone.

    Got another question though:

    How would I write pseudocode out for a parallel addition algorithm of 100 items yusing 100 cpus?
     

Share This Page