CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: How do I complete my code to shift array elements to the right?

1. Junior Member
Join Date
Apr 2016
Posts
1

## How do I complete my code to shift array elements to the right?

I've asked on other sites but have not gotten an answer. So I started an account to ask this question here. I need to write a function that takes an integer array (arr[]), its length (N) and the number of elements to right shift (M). My current code does not work, but I think I might be close to solving this problem.

Code:
```#include <iostream>
#include <string>
#include <vector>
using namespace std;

/**
* PURPOSE: Takes in an integer array (arr[]), its length (N), and the number of elements to right-shift (M):
* PARAMETERS:
*     arr[] , integer array
*     N , its length
*     M , number of elements to right shift
* RETURN VALUES:
*     the new array after right shifting the elements.
*/

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

int main (void)
{
int arr []= {1, 2, 3, 4, 5};

shiftright( arr, 5);

for ( int i=0; i<5; i++)
{
cout << arr[i] << ' ';
}

return(0);

}

void rightShiftElements (int arr[], int N, int M)
{
for (int m = (N-1); m>=1; m--){
int temp = arr[N-1];

for (int i=(N- 1); i>=1; i--)
{
arr[i] = arr[i-1] ;
}
arr[0] = temp;
}

}```

2. ## Re: How do I complete my code to shift array elements to the right?

you have non-matching signatures:
void rightShiftElements (int arr[], int N);
vs
void rightShiftElements (int arr[], int N, int M)

Also, you are calling it with the wrong name:
shiftright( arr, 5);

Fixing these issues means your code works and is correct in the case where you want to shift for exactly 1 element, which is good.

Start by fixing that, and then take it to the next step, shifting by M elements at once.

3. ## Re: How do I complete my code to shift array elements to the right?

1. What are these "functions" you refer to in your code snippet:
• void rightShiftElements (int arr[], int N);
• void rightShiftElements (int arr[], int N, int M)
• shiftright( arr, 5);

Code:
`int arr []= {1, 2, 3, 4, 5};`
How would it look like after the shifting?

4. ## Re: How do I complete my code to shift array elements to the right?

My current code does not work,
Have you used the debugger to debug the code and see where the execution deviates from that expected? Have you tried doing this with a pen and paper to develop the algorithm required? Trace through the code and then you'll see where is the issue.

Why are you including <vector> and <string> when a vector/string is not used in the code?

You don't need to pass the number of elements to rightShiftElements. From your existing non-fixed code
Code:
```#include <iostream>
using namespace std;

template<size_t N>
void rightShiftElements(int (&arr)[N], int M)
{
for (int m = (N - 1); m >= 1; m--) {
int temp = arr[N - 1];

for (int i = (N - 1); i >= 1; i--)
arr[i] = arr[i - 1];

arr[0] = temp;
}
}

int main(void)
{
int arr[] = { 1, 2, 3, 4, 5 };

rightShiftElements(arr, 3);

for (const auto& a : arr)
cout << a << ' ';

return 0;
}```
Also note that I'm changed your loop to display the contents of arr so that it will work with however many elements there are in arr.

5. ## Re: How do I complete my code to shift array elements to the right?

This approach creates binary bloat, and increases compile times. What you want to do is merely use the template interface to forward to a non-template interface.

But even then, things like c++17's std::size kind of make this obsolete, and arguably, you shouldn't be dealing with arrays to begin with. Worst case scenario, you also have std::array. IMO, it's not something you want to promote with easy to use interfaces.

The *only* place I'd arguably see this approach is being worth using, is for functions that deal with manifest constants (think raw strings). Typically, microsoft's "_s" variants make good use of this: strncpy_s has overloads that infers the target buffer size if it can.

6. ## Re: How do I complete my code to shift array elements to the right?

and arguably, you shouldn't be dealing with arrays to begin with
yep!

c++17's std::size kind of make this obsolete
Can't wait!

but in the meantime if a c-style array is used as a function parameter I really, really, really don't like having to pass the number of elements as function parameter(s) IMO having some binary bloat and an increased compile time is worth it for not having to do this.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•