Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help to write a C++ program that \"rotates” the first n elements of the a

ID: 3683204 • Letter: I

Question

I need help to 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

Explanation / Answer

Program:

#include<iostream>
#include <cmath>
using namespace std;

int main()
{
   int array[8],rotations,temp;
   cout<<"Enter 8 integer values to fill the array : "<<endl;
   for(int i=0;i<8;i++)
   {
       cin>>array[i];
   }
   cout<<"Enter the number of rotations to perform between 1 to 7 "<<endl;
   cin>>rotations;
   //validate value of rotations
   while(rotations <0 || rotations > 7)
   {
       cout<<"Rotations should between 1 and 7 both inclusive.Enter correct value again : "<<endl;
       cin>>rotations;
   }
   //Run the rotation number of times
   for(int i=0;i<rotations;i++)
   {
       //assign first value of array to temp
       temp=array[0];
       for(int j=1;j<8;j++)
       {
           array[j-1]=array[j];
       }
       //Assign temp to last value of array
       array[7]=temp;
   }
   cout<<"The content of new array is given below : "<<endl;
   //Output the final array
   for(int i=0;i<8;i++)
       cout<<array[i]<<" ";
}

Output:

Enter 8 integer values to fill the array :
1 2 3 4 5 6 7 8
Enter the number of rotations to perform between 1 to 7
2
The content of new array is given below :
3 4 5 6 7 8 1 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote