// Write a program that left rotates an array. The program inputs an integer array // of size 10, the elements of the array and an integer k. The array must be left // rotated k places. Note that k can be negative, in which case, the array should // be right rotated k time // Author:rahule@cse.iitk.ac.in #include main() { int a[10],i,j,k,temp; //entering array and k printf("enter 10 elements :"); for(i=0;i<10;i++) scanf("%d",&a[i]); printf("\nEnter k :"); scanf("%d",&k); k=k%10; //if k id greater than 10,make it less than 10 if(k<0) //k negative means right shift ,or left shift 10+k times k=10+k; for(i=k-1;i>=0;i--) //loop to shift first k elements of the array to the end of the array { for(j=0;j<10-k;j++) //loop to swap two consicutive elements in array.After each iteration,a[i] is pushed 10-k places to right { temp=a[j+i]; //swapping using temporary variable a[j+i]=a[j+i+1]; a[i+j+1]=temp; } } printf("\n"); for(i=0;i<10;i++) printf("%d ",a[i]); printf("\n"); }