Shifting array elements to the right: why doesn't this work?

I'm pretty sure my code doesn't work 100%, but I don't even know how to check. I'm supposed to right a function that takes an integer array (arr[]), its length (N), and the number of elements to right-shift (M}. I'm ignoring the parameters of the function right now because I just want to focus on getting a function that works. I'm not sure how to properly declare M either.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
int main ()
{
int main() 
{ 
const int N {10}; 
int n {3}; 
int arr[N] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA}; 

for(int i=0; i<M && i<N-n; ++i) arr[M+n-i-1] = arr[M-i-1]; 
for(int i=0; i<n; ++i) arr[i] = 0; 

}

  return 0;
}
Neither the code nor the algorithm make any sense. First, your code doe not declare M but uses it. and what is n? Why iterate up to N-n ?

Youi can't right-shift the elements starting from 0 and going up. Think about it:
You start with 1,2,3,4,5,6,7,8,9,A
Shift the first value 3 to the right
arr[0] moves to arr[3], so you now have
1,2,3,1,5,6,7,8,9,A
You just lost any knowledge of the 4 that used to be arr[3]
Topic archived. No new replies allowed.