The subject requires the realization of a simple function that loops the right of an array.A is stored inN>0) Integer to move each integer loop to the rightM≥0) a position that willThe data in a is determined by (A?0??A?1???A?N?1?? ) Transformation to (A?N?M???A?N?1??A?0??A?1???a? n? M? 1?? ) (Last m number of loops moved to the front < span class= "Strut" > M position).
function Interface Definition:
int ArrayShift( int a[], int n, int m );
This is the user-passed array, the size of the array, and the a[]
number of bits to n
m
move right. The function ArrayShift
must still exist in the array after the loop is moved right a[]
.
Example of a referee test procedure:
#include <stdio.h>#define MAXN 10int ArrayShift( int a[], int n, int m );int main(){ int a[MAXN], n, m; int i; scanf("%d %d", &n, &m); for ( i = 0; i < n; i++ ) scanf("%d", &a[i]); ArrayShift(a, n, m); for ( i = 0; i < n; i++ ) { if (i != 0) printf(" "); printf("%d", a[i]); } printf("\n"); return 0;}/* 你的代码将被嵌在这里 */
Input Sample:
6 21 2 3 4 5 6
Sample output:
5 6 1 2 3 4
#include <stdio.h>#defineMAXN 10intArrayshift (intA[],intNintm);intMain () {intA[MAXN], N, m; inti; scanf ("%d%d", &n, &m); for(i =0; I < n; i++) scanf ("%d", &A[i]); Arrayshift (A, n, m); for(i =0; I < n; i++ ) { if(I! =0) printf (" "); printf ("%d", A[i]); } printf ("\ n"); return 0;}/*your code will be embedded here.*/intArrayshift (intA[],intNintm) { while(m--)//Number of moves { inttemp = a[n-1];//Keep Last one for(inti = n-1; I >=1; i--) {A[i]= A[i-1]; } a[0] =temp; }}
Exercise 8-3 Array Loop right shift (20 points)