Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

As you learned from this class there is no such thing as circular array since an array is a continuous block of memory. Its last

As you learned from this class there is no such thing as "circular array" since an array is a continuous block of memory. Its last index does not loop back to its beginning. For this problem you will simulate the effect of a circular array in that you'd be able to shift all of its elements to the RIGHT by M elements, where M is an integer >= 0.

Write the following function taking in an integer array (arr[]), its length (N), and the number of elements to right-shift (M):

void rightShiftElements(int arr[], int N, int M) 

Examples:

input: {1,2}, 2, 1

resulting arr[]: {2,1}

input: {1}, 1, 0

resulting arr[]: {1}

input: {1}, 1, 12

resulting arr[]: {1}

input: {1, 2, 3, 4, 5, 6}, 6, 3

resulting arr[]: {4,5,6,1,2,3}

input: {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}, 45, 56

resulting arr[]: {35,36,37,38,39,40,41,42,43,44,45,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}

How to test (example):

int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; int size = sizeof(arr)/sizeof(arr[0]); rightShiftElements(arr, size, 1); cout << "["; for (int i=0; i 

Constraints / Assumptions:

Your inputs come straight from main(...) NOT cin, getline(...), etc., inside the above given function you have to write

This problem tests your understanding of how array works in C/C++

For this problem you will operate your solution directly on arr[]. DO NOT create another array in rightShiftElements() for this problem. This is the reason why rightShiftElements(...) is a void function. NOTE: recall array is passed into every function by "pointer", namely your function actually has a handle to where the original array is located. Any changes made to the array inside your function changes the original array outside of the function.

Each right shift moves all of the elements to the right by one index, with the exception of the last element which moves into index 0

The arr[] array is not empty, or N > 0

The number of elements to right shift M >= 0

There is nothing to print or return in the function, which should simply satisfy the logic requirement by shifting elements to right M times

arr[], when passed into the function, is decayed to a pointer that points to the first element of the array which means if you make modification to it in the function the original arr[] in main() will also change

Your main() function isn't graded; only the above referenced function is

Failure to follow the same exact function signature receives -1 logic point deduction

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions

Question

What is polarization? Describe it with examples.

Answered: 1 week ago