Rotating an array left by one element

R

ravxm

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;
}
 
J

Jack Klein

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;
}
 
R

ravxm

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top