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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started