In: Computer Science
Write a program in C that takes the length and the integers to be stored in an array and shifts array by N positions.
Example:
Input the number of elements to store in the array (max 10) : 5
Input 5 integers to be stored :
Index - 0 : 12
Index - 1 : 29
Index - 2 : 68
Index - 3 : 32
Index - 4 : 97
Input number of shifts : 2
Expected Output :
The given array is : 12 29 68 32 97
After 3 shifts the array becomes: 32 97 12 29 68
CODE :
OUTPUT :
Raw_Code :
#include<stdio.h>
int main(){
int N,arr[10],shifts,j,temp,i;
printf("Input the number of elements to store in the
array(max 10) : ");
scanf("%d",&N);
//taking number of elements
printf("Input %d integers to be stored :\n",N);
for(i=0;i<N;i++){
printf("index - %d: ",i);
scanf("%d",&arr[i]); //taking
array elements as input
}
printf("input number of shifts : ");
scanf("%d",&shifts);
//taking number of shifts as
input
printf("The given array is : ");
for(i=0;i<N;i++){
printf("%d ",arr[i]);
//pringing original array
}
for(i=0;i<shifts;i++){
temp = arr[N-1];
//shifting
the elements using nested loop
for(j=N-1;j>0;j--){
arr[j] =
arr[j-1];
}
arr[0] = temp;
}
printf("\nAfter %d shifts the array becomes :
",shifts);
for(i=0;i<N;i++){
printf("%d ",arr[i]);
//printing array after shifting
}
printf("\n");
return 0;
}
*******Comment me for any Queries and Rate me up********