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; } }




Reply With Quote
