Java code trace

Discussion in 'General Discussion / Real life stuff' started by MC_Scout, Feb 16, 2016.

  1. MC_Scout

    MC_Scout Fearless Leader Staff Member Manager

    Messages:
    7,112
    Likes Received:
    13,282
    Minecraft:
    MC_Scout
    So Im in an AP computer Science A class. (AP Computer Science A is the official name from the college board) and my teacher handed us a coding packet, on papper.
    The idea was that we had to trace the code and apply our knowledge of java to explain what the hell happend.
    It took myself, who is a novice, a friend of mine, who is supper smart, and someone else who has coded for a long time to work this problem. Eventually we got it, but it took us a long time to figure it out.

    So now Im curious to see if you guys to figure it out.
    The rule is that you you can not use a program to figure out the output. You can only use your brain and something to keep track of the values, like paper.
    Code:
    public void mysteryMethod (int [] first, int [] second) {
    int smaller = 0;
    if (first.length < second.length)
          smaller = first.length;
    else
            smaller = second.length;
    int temp = 0;
    int pos = 0;
    for (int i = 0; i < smaller; i++) {
    if (first [i] < second [i] ) {
          temp = first [pos];
          first [pos] = second [pos];
          second [pos] = temp;
          pos++;
    }
    }
    }
    
    

    Given the Following declarations:
    int [] arrA = {5, 3, 6, 1, 2, 3, 12, 9, 7};
    int [] arrB = {7, 9, 2, 11, 0, 4};

    What do arrA and arrB hold after the following call: mysteryMethod (arrA, arrB)?



    So ya, have fun it took us a long time to figure it out.
     
  2. _ItsMayDay

    _ItsMayDay Smol Blueberry Pun Skele 4 Life Donator

    Messages:
    670
    Likes Received:
    1,757
    Minecraft:
    LolNope. If I had one, it'd most likely be "SmolSkel"
    What be this nailed substance?
     
  3. ItsParakeetOitc

    ItsParakeetOitc

    Messages:
    180
    Likes Received:
    52
    Im only 12

    How am I supposed to no this?
     
  4. Huesan

    Huesan Trust me -- I'm an engineer Donator

    Messages:
    1,212
    Likes Received:
    1,888
    Minecraft:
    Huesan
    First logic statement will be false
    So smaller = 6

    Loop will run 6 times
    0 true swap 5, 7
    1 true swap 3, 9
    2 false no swap
    3 true swap 6, 2
    4 false no swap
    5 true swap 1, 11

    Final answer:
    int [] arrA = {7, 9, 2, 11, 2, 3, 12, 9, 7};
    int [] arrB = {5, 3, 6, 1, 0, 4};

    It's only logic :p
     
  5. ItsParakeetOitc

    ItsParakeetOitc

    Messages:
    180
    Likes Received:
    52
  6. XxGabitzaxX

    XxGabitzaxX Dealing with them haters

    Messages:
    353
    Likes Received:
    187
    Minecraft:
    WolfHuntG
    How do you feel with your soul sold to the devil in exchange of supernatural coding ?
     
  7. Huesan

    Huesan Trust me -- I'm an engineer Donator

    Messages:
    1,212
    Likes Received:
    1,888
    Minecraft:
    Huesan
    I'm in CS major ._.
     
    Katy_ likes this.
  8. RadeonX

    RadeonX 闇はどこにでもあります。 Donator

    Messages:
    634
    Likes Received:
    1,498
    Gz on taking up coding.

    Enjoy the road ahead.
    (C#>All.)
     
    Katy_ likes this.
  9. Mcc457

    Mcc457 ⭐⭐⭐⭐⭐

    Messages:
    3,351
    Likes Received:
    1,947
    Minecraft:
    PentilexBros
    What he said
     
    Sakujo, Katy_ and Huesan like this.