Rotate Left and Right

May someone please help me with this code. The task is really simple i guess.
The call rotateRight function must move all the elements in the array with
one position to the right as the last item goes to the first position. For example, before
the function call is only an array containing {1, 2, 3, 4, 5}, after calling this
must contain {5, 1, 2, 3, 4} and then another call - {4, 5, 1, 2, 3}.
The left rotating must do the same but in the oposite way. From {1,2,3,4,5} to {2,3,4,5,1}.
The final results from the code should be:


Testing right rotation:
1,2,3,4
4,1,2,3
3,4,1,2
2,3,4,1
1,2,3,4
Testing left rotation:
1,2,3,4
2,3,4,1
3,4,1,2
4,1,2,3
1,2,3,4
Testing right rotation:
1.11 2.22 3.33 4.44 5.55 6.66
6.66 1.11 2.22 3.33 4.44 5.55
5.55 6.66 1.11 2.22 3.33 4.44
4.44 5.55 6.66 1.11 2.22 3.33
3.33 4.44 5.55 6.66 1.11 2.22
2.22 3.33 4.44 5.55 6.66 1.11
1.11 2.22 3.33 4.44 5.55 6.66
Testing left rotation:
1.11 2.22 3.33 4.44 5.55 6.66
2.22 3.33 4.44 5.55 6.66 1.11
3.33 4.44 5.55 6.66 1.11 2.22
4.44 5.55 6.66 1.11 2.22 3.33
5.55 6.66 1.11 2.22 3.33 4.44
6.66 1.11 2.22 3.33 4.44 5.55
1.11 2.22 3.33 4.44 5.55 6.66
Testing right rotation:
intelligence
eintelligenc
ceintelligen
nceintellige
enceintellig
genceintelli
igenceintell
ligenceintel
lligenceinte
elligenceint
telligencein
ntelligencei
intelligence
Testing left rotation:
intelligence
ntelligencei
telligencein
elligenceint
lligenceinte
ligenceintel
igenceintell
genceintelli
enceintellig
nceintellige
ceintelligen
eintelligenc
intelligence
Testing right rotation:
are you ready to start
start are you ready to
to start are you ready
ready to start are you
you ready to start are
are you ready to start
Testing left rotation:
are you ready to start
you ready to start are
ready to start are you
to start are you ready
start are you ready to
are you ready to start



Here is the code:

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
  #include <iostream>
#include <cstring>
using namespace std;

// Replace the following definitions with your function templates rotateRight and rotateLeft:
// --------------------------------------------------------------

 void rotateRight(void * arr, unsigned n)
{
}

void rotateLeft(void * arr, unsigned n)
{
}

// --------------------------------------------------------------
// Testing code / Please do not modify the code below this line

template <typename T> void test(T * arr, unsigned n, const char *sp)
{
    for (int dir = 0; dir < 2; ++dir)
    {
        cout << "\nTesting " << (dir ? "left": "right") << " rotation:\n";
        for (unsigned r = 0; r <= n; ++r)
        {
            for (unsigned i = 0; i < n; ++i)
                cout << arr[i] << (i == n - 1 ? "" : sp);
            cout << endl;
            if (r < n)
                dir ? rotateLeft(arr, n) : rotateRight(arr, n);
        }
    }
    cout << endl;
}

int main()
{
    int inum[] = { 1, 2, 3, 4 };
    test(inum, sizeof(inum) / sizeof(*inum), ",");
    float fnum[] = { 1.11, 2.22, 3.33, 4.44, 5.55, 6.66 };
    test(fnum, sizeof(fnum) / sizeof(*fnum), "   ");
    char word[] = "intelligence";
    test(word, strlen(word), "");
    const char *phrase[] = { "are", "you", "ready", "to", "start" };
    test(phrase, sizeof(phrase) / sizeof(*phrase), " ");
    return 0;
}

Last edited on
That's a LOT of code for the problem.
This example takes hello world! and gives ello world!h
the other way is similar.

**I used the 0 end of string byte as a temporary and put it back here.

1
2
3
4
5
6
7
8
9
10
11

int main()
{
   char hw[] = "hello world!";
   char ln = strlen(hw);
   hw[ln] = hw[0];
   memmove(hw, &hw[1],ln);
   hw[ln] = 0;
   cout << hw;
   
}
Last edited on
Thank you for your help!
The rotate functions have to be templates too, using void* as an argument isn't good enough.
If the actual position of the elements doesn't have to change then all you need is to shift a cursor along the array and start printing from there. The rest is simple modulo arithmetic to establish the rotation, whatever the templated array is.

eg
1
2
3
4
5
void rotate_left(int* anArray, size_t aLimit, int& aCursor)
{
    aCursor = (aCursor + 1) % aLimit;
    print(anArray, aLimit, aCursor);
}


1
2
3
4
5
6
7
8
void print(int* anArray, size_t aLimit, int aCursor)
{
    for (int i = 0; i < aLimit; i++)
    {
        std::cout << anArray[(i + aCursor) % aLimit] << ' ';
    }
    std::cout << '\n';
}
Last edited on
The inputs are number arrays and char arrays, you have to use templates.
The inputs are number arrays and char arrays, you have to use templates.
Well, stamp my foot! Why don't you? I have, because I can read, but left it to OP.


one two three four five six 
No. of elements in array: 6

one two three four five six 
two three four five six one 
three four five six one two 
four five six one two three 
five six one two three four 
six one two three four five 
six one two three four five 
five six one two three four 
four five six one two three 
three four five six one two 
two three four five six one 
one two three four five six 
Topic archived. No new replies allowed.