# Thread: Arrays help

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

## 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. 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. 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]);
}```
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. Member
Join Date
Sep 2010
Location
Oregon, usa
Posts
69
Rep Power
0
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. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
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. 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:

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. Member
Join Date
Sep 2010
Location
Oregon, usa
Posts
69
Rep Power
0
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. 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

9. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
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.)

#### Posting Permissions

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