# Rotating an array left by one element

Discussion in 'C++' started by ravxm, Jan 27, 2007.

1. ### ravxmGuest

I'm trying to rotate an array of ints by one element to the left. I'm a
newbie on this so please have a little patience...
I want to rotate the array so if I have 12345, after one rotation I get
23451 and after another I get 34512.
I'm getting the following output after three rotations:
12345

23452
34523
45234

Here's my initial code:

#include <iostream>
#include <cstdlib>

using namespace std;

const int MAX_SIZE=5;
int MyArray[MAX_SIZE];
void shiftLeft(int tmparr[MAX_SIZE]);
void PrintArray(int tmparr[MAX_SIZE]);
int main(int argc,char *argv[])
{
for(int i=0;i<MAX_SIZE;i++)
{
MyArray=i+1;
}
PrintArray(MyArray);
shiftLeft(MyArray);
PrintArray(MyArray);
shiftLeft(MyArray);
PrintArray(MyArray);
shiftLeft(MyArray);
PrintArray(MyArray);
system("Pause");
}

void shiftLeft(int tmparr[MAX_SIZE])
{
for(int i=0;i<MAX_SIZE;i++)
{
MyArray=tmparr[(i+1)%MAX_SIZE];

}
}

void PrintArray(int tmparr[MAX_SIZE])
{
for(int i=0;i<MAX_SIZE;i++)
{
cout<<"Array position: "<<i<<endl;
cout<<"Contents: "<<tmparr<<endl;

}
cout<<"\n\n
********************************************************"<<endl;
}
ravxm, Jan 27, 2007

2. ### Jack KleinGuest

On 26 Jan 2007 22:39:34 -0800, "ravxm" <> wrote in
comp.lang.c++:

> I'm trying to rotate an array of ints by one element to the left. I'm a
> newbie on this so please have a little patience...
> I want to rotate the array so if I have 12345, after one rotation I get
> 23451 and after another I get 34512.
> I'm getting the following output after three rotations:
> 12345
>
> 23452
> 34523
> 45234
>
> Here's my initial code:
>
> #include <iostream>
> #include <cstdlib>
>
> using namespace std;
>
> const int MAX_SIZE=5;
> int MyArray[MAX_SIZE];
> void shiftLeft(int tmparr[MAX_SIZE]);
> void PrintArray(int tmparr[MAX_SIZE]);
> int main(int argc,char *argv[])
> {
> for(int i=0;i<MAX_SIZE;i++)
> {
> MyArray=i+1;
> }
> PrintArray(MyArray);
> shiftLeft(MyArray);
> PrintArray(MyArray);
> shiftLeft(MyArray);
> PrintArray(MyArray);
> shiftLeft(MyArray);
> PrintArray(MyArray);
> system("Pause");
> }
>
> void shiftLeft(int tmparr[MAX_SIZE])
> {
> for(int i=0;i<MAX_SIZE;i++)
> {
> MyArray=tmparr[(i+1)%MAX_SIZE];
>
> }
> }

The code in your shiftLeft function would work the way you want it to
if you were creating a rotated copy of the original in a different
array. But, as you can see, it won't work when the source and
destination arrays are the same.

Just step through it like this to see:

Before the first iteration:

MyArray is 12345, i is 0, i + 1 is 1

So your code is effectively this for the very first iteration:

MyArray[0]=tmparr[1];

Since the memory pointed to by tmparr is MyArray, you write 2 over the
1, and the 1 is gone forever.

If you want to rotate an array in place, you need a temporary variable
to store each value before you overwrite it.

> void PrintArray(int tmparr[MAX_SIZE])
> {
> for(int i=0;i<MAX_SIZE;i++)
> {
> cout<<"Array position: "<<i<<endl;
> cout<<"Contents: "<<tmparr<<endl;
>
> }
> cout<<"\n\n
> ********************************************************"<<endl;
> }

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c -faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
Jack Klein, Jan 27, 2007

3. ### ravxmGuest

On Jan 27, 2:58 am, Jack Klein <> wrote:
> On 26 Jan 2007 22:39:34 -0800, "ravxm" <> wrote in
> comp.lang.c++:
>
>
>
> > I'm trying to rotate an array of ints by one element to the left. I'm a
> > newbie on this so please have a little patience...
> > I want to rotate the array so if I have 12345, after one rotation I get
> > 23451 and after another I get 34512.
> > I'm getting the following output after three rotations:
> > 12345

>
> > 23452
> > 34523
> > 45234

>
> > Here's my initial code:

>
> > #include <iostream>
> > #include <cstdlib>

>
> > using namespace std;

>
> > const int MAX_SIZE=5;
> > int MyArray[MAX_SIZE];
> > void shiftLeft(int tmparr[MAX_SIZE]);
> > void PrintArray(int tmparr[MAX_SIZE]);
> > int main(int argc,char *argv[])
> > {
> > for(int i=0;i<MAX_SIZE;i++)
> > {
> > MyArray=i+1;
> > }
> > PrintArray(MyArray);
> > shiftLeft(MyArray);
> > PrintArray(MyArray);
> > shiftLeft(MyArray);
> > PrintArray(MyArray);
> > shiftLeft(MyArray);
> > PrintArray(MyArray);
> > system("Pause");
> > }

>
> > void shiftLeft(int tmparr[MAX_SIZE])
> > {
> > for(int i=0;i<MAX_SIZE;i++)
> > {
> > MyArray=tmparr[(i+1)%MAX_SIZE];

>
> > }
> > }The code in your shiftLeft function would work the way you want it to

> if you were creating a rotated copy of the original in a different
> array. But, as you can see, it won't work when the source and
> destination arrays are the same.
>
> Just step through it like this to see:
>
> Before the first iteration:
>
> MyArray is 12345, i is 0, i + 1 is 1
>
> So your code is effectively this for the very first iteration:
>
> MyArray[0]=tmparr[1];
>
> Since the memory pointed to by tmparr is MyArray, you write 2 over the
> 1, and the 1 is gone forever.
>
> If you want to rotate an array in place, you need a temporary variable
> to store each value before you overwrite it.
>
> > void PrintArray(int tmparr[MAX_SIZE])
> > {
> > for(int i=0;i<MAX_SIZE;i++)
> > {
> > cout<<"Array position: "<<i<<endl;
> > cout<<"Contents: "<<tmparr<<endl;

>
> > }
> > cout<<"\n\n
> > ********************************************************"<<endl;
> > }--

> Jack Klein
> Home:http://JK-Technology.Com
> FAQs for
> comp.lang.chttp://c-faq.com/
> comp.lang.c++http://www.parashift.com/c -faq-lite/
> alt.comp.lang.learn.c-c++http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

That was a very nice explanation, I'll try it out, thanks
ravxm, Jan 27, 2007