(Homework) Arrary Confusion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Newjavabug
    New Member
    • Mar 2008
    • 5

    (Homework) Arrary Confusion

    I am supposed to be writing an array that shifts numbers to the right in BlueJ. For example 1, 2, 3, 4, 5 would be printed as 5, 1, 2, 3, 4. I have searched the Internet and cannot figure out how to begin with this. I did see one post on this site, but the conversation did not make sense to me.

    Does anyone know where I can find information about starting to do this? How do you learn what code to use??? So far, below is my pathetic attempt to start:

    public class Array_Shift
    {
    public static void main(String arg[])
    {
    int num[] = {1, 2, 3, 4, 5};
    }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Newjavabug
    I am supposed to be writing an array that shifts numbers to the right in BlueJ. For example 1, 2, 3, 4, 5 would be printed as 5, 1, 2, 3, 4. I have searched the Internet and cannot figure out how to begin with this. I did see one post on this site, but the conversation did not make sense to me.

    Does anyone know where I can find information about starting to do this? How do you learn what code to use??? So far, below is my pathetic attempt to start:

    [code=java]
    public class Array_Shift
    {
    public static void main(String arg[])
    {
    int num[] = {1, 2, 3, 4, 5};
    }
    }[/code]
    Because arrays are very static data structures you can't structurally manipulate
    them, i.e you have to move the content of the elements around. You start with
    this:
    [code=java]
    { 1, 2, 3, 4, 5 }
    [/code]
    and you want the end result to be this:
    [code=java]
    { 5, 1, 2, 3, 4 }
    [/code]
    If you look closely, you see that you have to move a[i] to a[i+1] for all applicable
    values of i. the rightmost element must end up as the first element. Moving all
    elements one position to the right can be done using a bit of loop trickery-dickery:
    [code=java]
    for (int i= array.length; --i > 0; array[i]= array[i-1]);
    [/code]
    This is a complete giveaway so figure it out yourself; all elements are moved to the
    right starting on the right side of the array. This loop results in the following
    content of the array:
    [code=java]
    { 1, 1, 2, 3, 4 }
    [/code]
    So the rightmost element has to be saved temporarily before the loop statrts and
    put back at the beginning of the array after the loop ends; I leave that to you.

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by JosAH
      ...
      [code=java]
      for (int i= array.length; --i > 0; array[i]= array[i-1]);
      [/code]
      ....
      That old bag again ...

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by r035198x
        That old bag again ...
        What you're staring at Sir, is a carefully handcrafted for loop with an empty body.
        Nuffin' wrong with it.

        kind regards,

        Jos ;-)

        Comment

        • Newjavabug
          New Member
          • Mar 2008
          • 5

          #5
          Thank you! I'm not entirely sure I understand what to do, but I'll work on it and let you know if my solution works later tonight.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Newjavabug
            Thank you! I'm not entirely sure I understand what to do, but I'll work on it and let you know if my solution works later tonight.
            Study that funny for-loop and write a few small test programs and see what it
            does; r035198x doesn't like those terse tricky loops but don't pay any attention
            to him ;-)

            Basically all you have to do is this:

            [code=java]
            int temp= array[array.length-1]; // save the last element
            ... // that funny loop of mine here
            array[0]= temp; // put last element in the first place
            [/code]

            Now that isn't rocket science is it?

            kind regards,

            Jos

            Comment

            • Newjavabug
              New Member
              • Mar 2008
              • 5

              #7
              Agh, I'm sorry... I feel like this is rocket science. I've only done one java assignment, and it was in a lab, so I am still shakey on even starting the "public" stuff.

              I tried a few more pathetic attempts, but the array variable isn't even recognized.

              Could you give me full example? I need to learn how to construct this type of thing for the test, so I am by no means asking for an answer to my assignment. I think you've given me the answer already, but your responses are not registering with me for some reason (probably my ignorance on this topic). Perhaps, give me an example of a left rotate, and I can try to figure out the right one? I have no idea...


              public class input_text
              {
              public static void main(String arg[])
              {
              int num[] = {1, 2, 3, 4, 5};
              {
              int temp= array[array.length-1];
              for (int i= array.length; --i > 0; array[i]= array[i-1]);
              array[0]= temp; // put last element in the first place
              }
              }
              }

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                The clue is that I'm using the generic name 'array' for the array. You named it 'num'
                and declared it that way. Simply change my name to your name.

                kind regards,

                Jos

                Comment

                • Newjavabug
                  New Member
                  • Mar 2008
                  • 5

                  #9
                  Ahhh, thank you! ^_^

                  Comment

                  Working...