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

Write a program to implement a circular queue (perform rotation on an array). 10

ID: 3732540 • Letter: W

Question

Write a program to implement a circular queue (perform rotation on an array). 10 customers are sharing a resource and they are allocated 10 minutes each time it is their turn (They are allocated the 10 minutes when they are in the location 0 of the array int service[10];) . Fill the array with the following customer IDs: 10, 20, 30, 40, 50, 60, 70, 80, 90 , 100

Each time you perform a rotation, print the content of the array and indicate that the customer in the first cell is the one using the resource.

Reminder: CS155 lab question

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

Please find my implementation of Q2.

Please repost others in separate post.

#include <iostream>

using namespace std;

void RotatebyOne(int arr[],int n);

void Rotate(int arr[], int d, int n)
{
int i;
for (i = 0; i < d; i++)
    RotatebyOne(arr, n);
}
void RotatebyOne(int arr[],int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n-1; i++)
     arr[i] = arr[i+1];
arr[i] = temp;
}

/* utility function to print an array */
void printArray(int arr[], int size)
{
int i;

for(i = 0; i < size; i++)
    cout<< arr[i]<<endl;
}

/* Driver program to test above functions */
int main()
{
   int i,arr[8] ;
   int rotation;
   cout<<"enter Array elements"<<endl;
   for(i=0;i<8;i++)
   {
      cin>>arr[i];
   }
   cout<<"Enter the number of rotations between 1 and 7"<<endl;
   cin>>rotation;
   if(rotation<1 && rotation >7)
   {
     
      cout<<"Invalid Rotation";
      return 0;
     
   }
   Rotate(arr, 2, 8);
   printArray(arr, 8);
   return 0;
}

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