Array rotation

I'm trying to make my program rotate mylist[] to the left. I'm having an issue with the for loop and storing a temp value.

the output should be like this
0 0
1 1
2 2
3 3
after rotation
0 1
1 2
2 3
3 0
any help would be appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>

using namespace std;
void getlist (int [], int &);
void putlist (const int [], int);
void leftRotate (int [], int);

const int MAXSIZE = 50;
int i, index;
float average;
int mylist [MAXSIZE];

int main ( void )
{
    
    getlist (mylist, index);
    putlist (mylist, index);
    leftRotate (mylist, index);
    
    system ("PAUSE");
    return 0;
}
void getlist (int mylist[], int & index)    
{
    
    cout << "Enter the number of Array Elements, Maximum " << MAXSIZE << endl;
    cin >> index;
    
    for (i = 0; i < index; i ++)
        {
        cout << "Enter the next array element\n";
        cin >> mylist [i];
        }
}

void putlist (const int mylist[], int index)
{    
    cout << "Array elements\n";
    for (i = 0; i < index; i ++)
    cout << i << " " << mylist [i] << endl;
}    


void leftRotate (int mylist[], int index)
{
     int temp;
     cout << "Left rotation of array\n";
     cout << "Array rotation:\n";
     temp = mylist[0];
     mylist[0] = mylist[i];
     mylist[i] = temp;
     for (i = index-1; i >= 0; i--)
     cout << i << ' ' << mylist[i] << endl;;
}
You can try the rotate STL algorithm: http://www.cplusplus.com/reference/algorithm/rotate/
There is explained how the algorithm works if you want to make your own implementation
I addition to what Bazzy suggested, the std::rotate works with arrays as well as STL containers. The example uses a vector specifically.

What's really interesting about rotate is that it might not work the way you would think. Notice how in your attempt, you named the function rotateLeft. Std::rotate requires that you specify a parameter indicating which parameter you wish to have as the new beginning. As far as the user is concerned it make no difference if there is really a left or right rotate since it could be done simply be swapping elements. If you want to rotate left by 1, then you select myArray + 1 as the middle argument. I've provided two examples below that use c arrays for a full and partial rotate of the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <algorithm>
#include <vector>

using std::cout;
using std::endl;

int main () {
    const int SIZE(9);
    
    // Notice how the rotations will result in 1, 2, 3, 4, 5, 6, 7, 8, 9
    
    // create an array of 9 elements and rotate the entire array.
    int myArray[SIZE] = { 7, 8, 9, 1, 2, 3, 4, 5, 6,  };
    std::rotate(myArray, myArray + 3, myArray + SIZE);

    // create an array of 9 elements and rotate the first 6 elements.
    int myOtherArray[SIZE] = { 2, 3, 4, 5, 6, 1, 7, 8, 9 };
    std::rotate(myOtherArray, myOtherArray + 5, myOtherArray + 6);

  // print out content:
  cout << "myArray contains:";
  for (int i = 0; i < SIZE; ++i)
    cout << " " << myArray[i];

  cout << endl;

  cout << "myOtherArray contains:";
  for (int i = 0; i < SIZE; ++i)
    cout << " " << myOtherArray[i];

  cout << endl;

  return 0;
}
Last edited on
Topic archived. No new replies allowed.