Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started