-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshift_array.c
48 lines (42 loc) · 954 Bytes
/
shift_array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
void shift_left(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 1; i < n; i++)
{
arr[i - 1] = arr[i];
}
arr[n - 1] = temp;
}
void shift_right(int arr[], int n)
{
int i, temp;
temp = arr[n - 1];
for (i = n - 2; i >= 0; i--)
{
arr[i + 1] = arr[i];
}
arr[0] = temp;
}
int main(void)
{
int array[50];
int n, i;
printf("Enter the size of the array : ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("You have entered the following array : \n");
for (i = 0; i < n; i++)
printf("%4d", array[i]);
printf("\n");
//shift_left(array,n);
shift_right(array, n);
printf("After shift : \n");
for (i = 0; i < n; i++)
printf("%4d", array[i]);
printf("\n");
return 0;
}