[M-10]
Write a function that moves array elements N positions left. The first N entries
are discarded, and the last N elements are set to zero.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Shift Left 7 Positions: 7 8 9 10 11 12 13 14 15 16 17 18 19 0 0 0 0 0 0 0
Here is a testing framework:
#include <stdio.h> #include <stdlib.h> /* Puzzle D38 -- shift array elements N positions left; */ /* the last N elements get 0; */ /* the first N elements are discarded */ void shiftLeftNArray( int size, int arr[], int shift ) { } void fillArrayInOrder( int size, int arr[] ) { int j; for ( j=0; j<size; j++ ) { arr[j] = j; } } void printArray( int size, int arr[] ) { const int N = 10; int j; for ( j=0; j<size; j++ ) { if ( j%N == N-1 ) printf("%4d\n", arr[j] ); else printf("%4d ", arr[j] ); } } int main() { const int SIZE = 20; int x[ SIZE ]; int shift = 7; fillArrayInOrder( SIZE, x ); printArray( SIZE, x ); printf("\nShift Left %d Positions:\n", shift); shiftLeftNArray( SIZE, x, shift ); printArray( SIZE, x ); printf("\n\n"); return 0; }