Exercise 3: Practice to manipulate data in arrays Write a C program to create an integer array called Motion of size 5. Ask the user to enter values to the array from the keyboard. Rotate the values of the array by one position in the forward direction and display the values.
Ex: number in index 4 should move to index 3, Number in index 3 should move to index2, number index 0 should move to index 4.
Initial values 10 6 8 2 9
After rotating 6 8 2 9 10
#include<stdio.h>
#include<string.h>
int main() {
int Motion[5];
int i;
int temp;
for(i=0;i<5;++i){
printf("Enter the value %d: ",(i+1));
scanf("%d",&Motion[i]);
}
printf("Original array:\n");
for( i=0;i<5;++i){
printf("%d ",Motion[i]);
}
temp = Motion[0];
for ( i = 0; i < 4; i++){
Motion[i] = Motion[i + 1];
}
Motion[4] = temp;
printf("\n\nArray after right rotation:\n");
for(i=0;i<5;++i){
printf("%d ",Motion[i]);
}
scanf("%d",&Motion[0]);
return 0;
}
Comments
Leave a comment