# Arrays help

• 11-02-2010, 07:35 PM
Bgreen7887
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}
• 11-02-2010, 08:03 PM
Zack
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.
• 11-02-2010, 08:08 PM
berkeleybross
Dont know if this is the best way to do it, but its the first i came up with ^^
I think it works correctly.

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 :) )
• 11-02-2010, 08:08 PM
tashimoto
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
• 11-02-2010, 08:29 PM
Bgreen7887
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.
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]);                         }                 }         }```
'
• 11-02-2010, 08:31 PM
JosAH
Quote:

Originally Posted by Bgreen7887
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:

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:

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
• 11-02-2010, 09:06 PM
tashimoto
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. :)
• 11-02-2010, 09:10 PM
JosAH
Quote:

Originally Posted by tashimoto
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
• 11-02-2010, 09:37 PM
pbrockway2
Quote:

Originally Posted by tashimoto
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.)