Question Problem with move items of an array

drew99

New member
Joined
Mar 22, 2011
Messages
1
Programming Experience
3-5
Hi,
I have a problem with move items of an array according to a schema.
This is the purpose of the code:

arrayi= {1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5}

Move items in the array to achieve this:
arrayf= {1,2,3,4,5,5,1,2,3,4,4,5,1,2,3,3,4,5,1,2,2,3,4,5,1}

So:
12345 12345
12345 51234
12345 -> 45123
12345 34512
12345 23451

Obviously this is just one example, the code must be able to give the same result with a n items (n perfect square). I've tried many times but I've have not succeeded.
Thanks in advance for help.
 

ignyte87

Active member
Joined
Feb 20, 2011
Messages
39
Programming Experience
1-3
1. Keep a counter of how many items you moved the last time.
2. Skip counter spaces and copy n - counter items.
3. Copy the last counter items into the skipped spaces.

You will probably want to use a for loop, similar to the following. Maintain the counter separately from this loop. You will probably need nested loops for steps 2 and 3.
VB.NET:
For i as integer = 0 to n^2 -1 Step n 

for j as integer = counter to i + n
    ' Copy end items (4, 5...) to beginning
next

for k as integer = i to counter
    ' add beginning items (1, 2...) to end
next

counter += 1

Next
 
Last edited:
Top Bottom