Results 1 to 9 of 9

Thread: Arrays help

  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Arrays help

    can anyone write in psuedocode for me..Dont know how to start.1. Rotating array contents.
    Write a Java program that rotates an array of integers by the number and direction entered through the command line. For example, given lst={1,2,3,4,5,6}, your code will return:

    command> java rotate 3 right
    before l={1,2,3,4,5,6,7}
    after l={5,6,7,1,2,3,4}

    command> java rotate 2 left
    before l={1,2,3,4,5,6,7}
    after l={3,4,5,6,7,1,2}

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Think about how many elements are moved in each case. In the case where it's moving 3 right, you're taking 3 elements from the right side of the array and putting them at the start of a new array, then adding the remaining elements to the end. When moving left 2, you're taking all of the elements BUT the first 2 and putting them at the front, then appending the remaining 2 at the end.

    Can you kind of see how you can create a situation for this? I want to avoid a full pseudocode right now as it's important for you to nail the logic down yourself once you have a concept in mind.

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Dont know if this is the best way to do it, but its the first i came up with ^^
    I think it works correctly.

    Java Code:
    rotate (int[] initialArray, int pos, boolean left) {
        int [][] splitarray
        if (left) {
             splitarray = split (initialArray, length - pos);
        } else {
             splitarray = split (initialArray, pos);
        }
        int answerarray = merge (splitarray[1], splitarray[0]);
        return answerarray
    }
    split will take an array and split it into a 2d array after the position.
    merge will take two arrays and merge it into one
    (you need to make the two methods :) )

  4. #4
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    Think of your array as circular instead of linear and it might help you to visualize what should happen

    (I tried to type a visual for you, but I couldn't get it formatted properly)

    http://home.comcast.net/~chris89/RotateExample.pdf

    You'll need a loop and a temp variable to swap values within your array.

    Give it a try and post some code if you have more questions.
    Hope this helps! :)
    Chris
    Last edited by tashimoto; 11-02-2010 at 07:56 PM. Reason: tabs/spacing didn't come out right.

  5. #5
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Thanks for the answers guys.This is just hw that due next week..im more interested in what steps i need to get to the goal ..i.e im drawing/trying to imagine and write down as all you have suggested in the future. Here's a program i made that takes number in from command line and writes them in reverse it works. I just pissed as i know i should be able to guide myself for this problem using this one and i cant.
    Java Code:
    public class arrayargs
    	{
    		public static void main(String[] args)
    		{
    		int total=0;
    		int []  lst = new int[args.length];
    			int x = 0;
    			int temp;
    			for (int i=0 ; i<args.length; i++){
    			x = Integer.parseInt(args[i]);
    			System.out.println(x+" ");
    			total +=x;
    			}
    			System.out.println(total);
    			System.out.println("");
    			for (int i=0 ; i<args.length; i++){
    					x = Integer.parseInt(args[i]);
    					temp = x;
    					x = lst[args.length -(1 + i)];
    					lst[lst.length -(1 + i)]= temp;
    			 }
    			for (int i=0 ; i<args.length; i++) {
    					System.out.println(lst[i]);
    			}
    		}
    	}
    '

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Bgreen7887 View Post
    can anyone write in psuedocode for me..Dont know how to start.1. Rotating array contents.
    Write a Java program that rotates an array of integers by the number and direction entered through the command line. For example, given lst={1,2,3,4,5,6}, your code will return:

    command> java rotate 3 right
    before l={1,2,3,4,5,6,7}
    after l={5,6,7,1,2,3,4}

    command> java rotate 2 left
    before l={1,2,3,4,5,6,7}
    after l={3,4,5,6,7,1,2}
    Suppose you can reverse part of an array from position lo (inclusive) up to position hi (exclusive); here's the reverse method:

    Java Code:
    int[] reverse(int[] a, int lo, int hi) {
       for (; lo < --hi; lo++) {
          int t= a[lo]; a[lo]= a[hi]; a[hi]= t;
       }
       return a;
    }
    If you pass your array { 1, 2, 3, 4, 5, 6, 7 } and call method reverse(a, 0, 2) on it you get the following result back { 2, 1, 3, 4, 5, 6, 7 }. Pass it again to the same method reverse(a, 2, a.length) and you get { 2, 1, 7, 6, 5, 4, 3 } back. Reverse the entire array using the method call reverse(a, 0, a.length) and you get the result { 3, 4, 5, 6, 7, 1, 2 }. Exactly what you want. So rotating your array left by i elements is:

    Java Code:
    int[] rotLeft(int[] a, int i) {
       reverse(a, 0, i);
       reverse(a, i, a.length);
       reverse(a, 0, a.length);
       return a;
    }
    I leave rotating an array right by r postions as an exercise for the reader. (hint: rotating right by r positions is the same as rotating left by a.length-r positions).

    The advantage of all this trickery is that you don't need any temporary storage.

    kind regards,

    Jos
    Last edited by JosAH; 11-02-2010 at 07:39 PM.

  7. #7
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    Ooooo, I LIKE it Jos!! How did you figure out that pattern? :)

    (Does it work if the user types in a number greater than the size of the array?)
    IT DOES... would have to subtract/divide to get the correct position within the array size. :)
    Last edited by tashimoto; 11-02-2010 at 08:15 PM. Reason: answered my own question!

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by tashimoto View Post
    Ooooo, I LIKE it Jos!! How did you figure out that pattern? :)
    I got it from an old book "Software engineering in RatFor" by Dennis Ritchie. It's lost folklore but ever so useful, especially when memory is tight. You do have to check the boundaries but that is just to prevent segmentation faults or AIOOB exceptions.

    kind regards,

    Jos

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by tashimoto View Post
    Think of your array as circular instead of linear

    This suggests (to my mind at least) a different approach: have a class that wraps the array and also has a "start" index variable. The class provides set/get methods which do a bit of arithmetic with start and the array length to alter the wrapped array.

    With this setup rotations are trivial: they just alter the value of start.

    (I recognise that this isn't a solution to the problem that the OP was posed: but it might be a reason for not posing that problem in practice.)

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  3. Help with arrays
    By rp1783 in forum New To Java
    Replies: 5
    Last Post: 02-20-2010, 05:09 AM
  4. Arrays
    By karabo101 in forum New To Java
    Replies: 12
    Last Post: 10-11-2009, 05:02 PM
  5. Need help with 2D arrays...
    By rrsv2 in forum New To Java
    Replies: 3
    Last Post: 11-30-2008, 03:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •