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