Write a C program that rotates the first n elements of the a

Write a C++ program that \"rotates” the first n elements of the array a, k positions to the end of the array (right of array). Your program should declare an integer array that holds 8 numbers. Ask the user to fill the array then ask the user for the number of rotations which should between 1 and 7. Validate this input and continue with rotation if it is only between 1 and 7. Finally, print the content of the new array.

Tess your program using the following:

Array a holds the following values: 22, 33, 44, 55, 66, 77, 88, 99

The number of rotations k = 5

The output will be:

{ 77, 88, 99, 22, 33, 44, 55, 66}.

Hint (how to do that):

Start simple

For example, when k=1, this would transform the array

{22, 33, 44, 55, 66, 77, 88, 99}

into

{ 33, 44, 55, 66, 77, 88, 99, 22}

Which is logically done as follows:

The array contains the following data

0

1

2

3

4

5

6

7

22

33

44

55

66

77

88

99

Step 1: place 22 (content of location 0) into a temp variable

Step 2: shift all remaining cells one to the left starting with left cells first. Shifting means moving the content of cell 1 to cell 0; moving the content of cell 2 to cell 1 and so on. You can use a for loop to perform the shifting. These actions will produce the following array

0

1

2

3

4

5

6

7

33

44

55

66

77

88

99

99

Note here that cell 6 and cell 7 both have the same value.

Step 3: place the content of the temp variable in the last location of the array which is cell 7. This action will produce the following:

0

1

2

3

4

5

6

7

33

44

55

66

77

88

99

22

Once you have this working for one rotation. You can plug all of the related code in a for loop that loops k times.

0

1

2

3

4

5

6

7

22

33

44

55

66

77

88

99

Solution

#include<iostream>
#include<conio.h>
using namespace std;

#define MAX 8
int arrData[MAX];

void getData()
{
   cout << \"Please Enter Elements \ \";
   for (int i = 0; i < MAX; i++)
   {
       cout << \"arrData[ \" << i << \" ] = \";
       cin >> arrData[i];
   }
}

void ViewData()
{
   cout << \"The data is :\ \";
   for (int i = 0; i < MAX; i++)
   {
       cout << \"\ arrData[ \" << i << \" ] = \" << arrData[i];      
   }
}

void Rotate()
{
   int RotateCount = 0;

   cout << \"\ Please Enter rotate number(1 to 7): \";
   cin >> RotateCount;

   if (RotateCount < 1 || RotateCount > 7)
   {
       cout << \"\ Please enter valid input (1 to 7) \ \";
       return;
   }

   int *ptrList = NULL;   // to store tem data
   ptrList = new int[RotateCount];

   if (ptrList == NULL)
   {
       cout << \" \ Memory allocation failed\ \";
       return;
   }
  
   for (int i = 0; i < RotateCount; i++)
   {
       ptrList[i] = arrData[i];  
   }
  
   int j = 0;
   for (int i = RotateCount; i < MAX; i++)
   {
       arrData[j] = arrData[i];
       j++;
   }

   for (int i = 0; i < RotateCount; i++)
   {
       arrData[j] = ptrList[i];
       j++;
   }

   if (ptrList != NULL)
   {
       delete ptrList;
       ptrList = NULL;
   }
}

int main()
{
   // Get data from user
   getData();
   ViewData();
   Rotate();
   ViewData();

   getch();
   return 0;
}

Write a C++ program that \
Write a C++ program that \
Write a C++ program that \
Write a C++ program that \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site