luc068.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 08 Feb 2026
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
Given an array p[5], write a function to shift it circularly left by two positions. Call this function for a 4 x 5 matrix and get its rows left shifted.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <stdlib.h>
void shift_left_two(int *p, int n);
void print_row(int *p, int n);
int main()
{
int mat[4][5] = {
{15, 30, 28, 19, 61},
{1, 2, 3, 4, 5},
{10, 20, 30, 40, 50},
{5, 4, 3, 2, 1}
};
int i;
printf("Original Matrix:\n");
for (i = 0; i < 4; i++)
print_row(mat[i], 5);
// Apply shift to each row
for (i = 0; i < 4; i++)
{
shift_left_two(mat[i], 5);
}
printf("\nMatrix after shifting each row left by 2:\n");
for (i = 0; i < 4; i++)
print_row(mat[i], 5);
return 0;
}
void shift_left_two(int *p, int n)
{
if (n < 2) return; // Cannot shift if less than 2 elements
int temp1 = p[0];
int temp2 = p[1];
int i;
// Shift elements left by 2
for (i = 0; i < n - 2; i++)
{
p[i] = p[i + 2];
}
// Place the first two elements at the end
p[n - 2] = temp1;
p[n - 1] = temp2;
}
void print_row(int *p, int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%d ", p[i]);
}
printf("\n");
}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
49
50
51
52
53
54
55
56
57
58
59
60
61
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
49
50
51
52
53
54
55
56
57
58
59
60
61