English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

2007-01-27 07:24:17 · 1 answers · asked by Anonymous in Computers & Internet Programming & Design

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 {
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 {
MyArray[i]=tmparr[(i+1)%MAX_SIZE];

}

}

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

2007-01-27 07:25:26 · update #1

1 answers

For the left shift.

temp = MyArray[0] ;
for(i = 1; i < MAX_SIZE-2; i++) {
MyArray[i] = MyArray[i+1] ;
}
MyArray[MAX_SIZE-1] = temp;

2007-01-27 07:32:22 · answer #1 · answered by feanor 7 · 0 0

fedest.com, questions and answers