Question: I am trying to write code that does what the following C++ code does but without indexing: void shift(int values[], int size) { int temp

I am trying to write code that does what the following C++ code does but without indexing:

void shift(int values[], int size) { int temp = values[size - 1] int temp1;

for (int i = 0; i < size; i++) { temp1 = values[i]; values[i] = temp; temp = temp1; } }

If the array for example was 0,1,2,3,4 the function should make the array 4,0,1,2,3 but the code below makes it 4,0,1,3,2

I need help fixing the code. DO NOT CHANGE HOW THE FOR LOOP IS WRITTEN! If you change it to for (int i = 0; i < n; ++i) I WILL MARK IT WRONG. I need to use pointers and traversal by pointers.

void slideRight(int arr[], int n){ int *temp = arr + (n - 1); int temp1 = 0;

for ( int *ptr = arr; ptr < arr + n; ++ptr) { temp1 = *(arr + *ptr); *(arr + *ptr) = *temp; *temp = temp1; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!