0001 #include <iostream> 0002 #include "UniPrint/print_int_array.h" 0003 0004 int shift0 ( int* A, int n, int k ); // 0005 int shift1 ( int* A, int n, int k ); // 0006 int shift2 ( int* A, int n, int k ); //ð 0007 0008 int main ( int argc, char* argv[] ) { 0009 if ( argc < 3 ) { printf ( "Usage: %s <array_length> <shift>\n", argv[0] ); return 1;} 0010 int n = atoi ( argv[1] ); if ( n < 0 ) return 1; 0011 int k = atoi ( argv[2] ); if ( k < 0 || k > n ) return 1; 0012 printf ( "Shift A[%d] with k = %d ...\n", n, k ); 0013 int* A = ( int* ) malloc ( sizeof ( int ) * n ); 0014 printf ( "\n== shift algorithm #0 ========\n" ); 0015 for ( int i = 0; i < n; i++ ) A[i] = i; print ( A, n ); 0016 printf ( "== move *%3d\n", shift0 ( A, n, k ) ); print ( A, n ); printf ( "\n" ); 0017 printf ( "\n== shift algorithm #1 ========\n" ); 0018 for ( int i = 0; i < n; i++ ) A[i] = i; print ( A, n ); 0019 printf ( "== move *%3d\n", shift1 ( A, n, k ) ); print ( A, n ); printf ( "\n" ); 0020 printf ( "\n== shift algorithm #2 ========\n" ); 0021 for ( int i = 0; i < n; i++ ) A[i] = i; print ( A, n ); 0022 printf ( "== move *%3d\n", shift2 ( A, n, k ) ); print ( A, n ); printf ( "\n" ); 0023 free ( A ); 0024 return 0; 0025 }