Jump to content

Moving elements in an array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
Fischerspooner

Fischerspooner

    Newbie

  • Members
  • PipPip
  • 27 posts
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;
     }
}


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
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.

#3
Fischerspooner

Fischerspooner

    Newbie

  • Members
  • PipPip
  • 27 posts
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 :/

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
At a guess, you are trying to push beyond the beginning/end of your array. One note: t < DIM is always true.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Fischerspooner

Fischerspooner

    Newbie

  • Members
  • PipPip
  • 27 posts
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];
   }
}


#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It's possible, but you have to be careful around the beginning/end of the array.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Fischerspooner

Fischerspooner

    Newbie

  • Members
  • PipPip
  • 27 posts

WingedPanther said:

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...:)

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts

#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;

}



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

#9
Fischerspooner

Fischerspooner

    Newbie

  • Members
  • PipPip
  • 27 posts
Thanks, I'll study it:)

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Here's an (possibly) interesting thing to think about: What are the time/space advantages/costs of each method of rotating an array?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog