•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Status Updates

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Moving elements in an array

pseudocode array element

9 replies to this topic

### #1 Fischerspooner

Fischerspooner

CC Regular

• Just Joined
• 27 posts

Posted 06 December 2006 - 11:48 AM

Hi,

I'm currently studying Java on my own and I wanted to try to "rotate" elements in an array.
So the start would be:
{5,3,8,7,7,2,0,6,1,4}
But if I enter "rotate 1" it should give me
{4,5,3,8,7,7,2,0,6,1}

But I keep running into ArrayIndexOutOfBounds errors if I check my pseudocode in Java.

I don't want some code, just a hint or two would be very much appreciated:)

This is what I have already:
```for(t=DIM-1 ; t > rot ; t--)
{
if(t < DIM)
{
k = table[t];
table[t-1] = table[t];
table[t+1]= k;
}
else
{
k = table[t];
table[t-1] = table[t-DIM];
table[t+1] = k;
}
}
```

• 0

### #2 Guest_Jordan_*

Guest_Jordan_*
• Guest

Posted 06 December 2006 - 01:30 PM

Without doing the code for you, I suggest using a debugger and stepping through the function. You will then be able to see where the object out of bounds is coming from.
• 0

### #3 Fischerspooner

Fischerspooner

CC Regular

• Just Joined
• 27 posts

Posted 07 December 2006 - 03:31 AM

I ran it through the debugger but I still cant find my mistake. I'm just overlooking something.

Some pseudocode:

temp = table[i+rot]; // remember what was on i+rot
table[i] = table[i+rot] // change the place of element i to i+rot
if (i+rot < table.length)
start from 0

My head is all messed up :/
• 0

### #4 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 07 December 2006 - 09:46 AM

At a guess, you are trying to push beyond the beginning/end of your array. One note: t < DIM is always true.
• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### #5 Fischerspooner

Fischerspooner

CC Regular

• Just Joined
• 27 posts

Posted 07 December 2006 - 02:44 PM

I used a second array to rotate and it works. Is it even possible with one array?

```for(t=0 ; t < DIM ; t++)
{
if(t+rot < DIM)
{
table2[t+rot] = table[t];
}
else
{
table2[t+rot-DIM] = table[t];
}
}```

• 0

### #6 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 08 December 2006 - 09:48 AM

It's possible, but you have to be careful around the beginning/end of the array.
• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### #7 Fischerspooner

Fischerspooner

CC Regular

• Just Joined
• 27 posts

Posted 08 December 2006 - 10:55 AM

It's possible, but you have to be careful around the beginning/end of the array.

Could you post some code? I know I didn't ask for code but I'm eager to learn...
• 0

### #8 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 11 December 2006 - 09:51 AM

```
#include <iostream>

int GCF(int a,int b)

{

int quot, mod;

do

{

quot = a/b;

mod = a%b;

a = b;

b = quot;

} while (mod != 0);

return a;

}

int main()

{

const int DIM=9;

int table[DIM];

int rot=3;

int loops = GCF(DIM,rot);

for (int i=0;i<DIM;i++) table[i] = i+1;

for (int i=0;i<DIM;i++) std::cout << table[i] << " ";

std::cout << "\n";

for (int i=0;i<loops;i++)

{

int temp=table[i];

for (int j=(i+rot)%DIM;j!=i;j=(j+rot)%DIM)

{

table[(j-rot)%DIM]=table[j];

}

table[DIM+(i-rot)%DIM]=temp;

}

for (int i=0;i<DIM;i++) std::cout << table[i] << " ";

std::cout << "\n";

return 0;

}

```

• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### #9 Fischerspooner

Fischerspooner

CC Regular

• Just Joined
• 27 posts

Posted 11 December 2006 - 10:30 AM

Thanks, I'll study it:)
• 0

### #10 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 12 December 2006 - 06:24 PM

Here's an (possibly) interesting thing to think about: What are the time/space advantages/costs of each method of rotating an array?
• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### Also tagged with one or more of these keywords: pseudocode, array, element

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download