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