Results 1 to 9 of 9
Thread: Arrays help
- 11-02-2010, 06:35 PM #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}
- 11-02-2010, 07:03 PM #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.
- 11-02-2010, 07:08 PM #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.
split will take an array and split it into a 2d array after the position.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 }
merge will take two arrays and merge it into one
(you need to make the two methods :) )
- 11-02-2010, 07:08 PM #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! :)
ChrisLast edited by tashimoto; 11-02-2010 at 07:56 PM. Reason: tabs/spacing didn't come out right.
:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
- 11-02-2010, 07:29 PM #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]); } } }
- 11-02-2010, 07:31 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Suppose you can reverse part of an array from position lo (inclusive) up to position hi (exclusive); here's the reverse method:
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[] reverse(int[] a, int lo, int hi) { for (; lo < --hi; lo++) { int t= a[lo]; a[lo]= a[hi]; a[hi]= t; } 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).Java Code:int[] rotLeft(int[] a, int i) { reverse(a, 0, i); reverse(a, i, a.length); reverse(a, 0, a.length); return a; }
The advantage of all this trickery is that you don't need any temporary storage.
kind regards,
JosLast edited by JosAH; 11-02-2010 at 07:39 PM.
- 11-02-2010, 08:06 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!
:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
- 11-02-2010, 08:10 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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, 08:37 PM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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
-
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Help with arrays
By rp1783 in forum New To JavaReplies: 5Last Post: 02-20-2010, 05:09 AM -
Arrays
By karabo101 in forum New To JavaReplies: 12Last Post: 10-11-2009, 05:02 PM -
Need help with 2D arrays...
By rrsv2 in forum New To JavaReplies: 3Last Post: 11-30-2008, 03:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks