Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Rotating an array left by one element (http://www.velocityreviews.com/forums/t460089-rotating-an-array-left-by-one-element.html)

ravxm 01-27-2007 06:39 AM

Rotating an array left by one element
 
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]=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[i]=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[i]<<endl;

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


Jack Klein 01-27-2007 06:58 AM

Re: Rotating an array left by one element
 
On 26 Jan 2007 22:39:34 -0800, "ravxm" <ravxmm@gmail.com> 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]=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[i]=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[i]<<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/~a...FAQ-acllc.html

ravxm 01-27-2007 07:06 AM

Re: Rotating an array left by one element
 


On Jan 27, 2:58 am, Jack Klein <jackkl...@spamcop.net> wrote:
> On 26 Jan 2007 22:39:34 -0800, "ravxm" <rav...@gmail.com> 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]=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[i]=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[i]<<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/~a...FAQ-acllc.html




That was a very nice explanation, I'll try it out, thanks



All times are GMT. The time now is 09:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.