luc068.c (1615B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 08 Feb 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* 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. 9 */ 10 11 /* Let Us C, Chap- 14 (Multidimensional Arrays), Qn No.: C(h) */ 12 13 /* This file is auto-generated by a bot. */ 14 /* This code is not compiled; it is for reference only. */ 15 16 17 #include <stdio.h> 18 #include <stdlib.h> 19 20 void shift_left_two(int *p, int n); 21 void print_row(int *p, int n); 22 23 int main() 24 { 25 int mat[4][5] = { 26 {15, 30, 28, 19, 61}, 27 {1, 2, 3, 4, 5}, 28 {10, 20, 30, 40, 50}, 29 {5, 4, 3, 2, 1} 30 }; 31 int i; 32 33 printf("Original Matrix:\n"); 34 for (i = 0; i < 4; i++) 35 print_row(mat[i], 5); 36 37 // Apply shift to each row 38 for (i = 0; i < 4; i++) 39 { 40 shift_left_two(mat[i], 5); 41 } 42 43 printf("\nMatrix after shifting each row left by 2:\n"); 44 for (i = 0; i < 4; i++) 45 print_row(mat[i], 5); 46 47 return 0; 48 } 49 50 void shift_left_two(int *p, int n) 51 { 52 if (n < 2) return; // Cannot shift if less than 2 elements 53 54 int temp1 = p[0]; 55 int temp2 = p[1]; 56 int i; 57 58 // Shift elements left by 2 59 for (i = 0; i < n - 2; i++) 60 { 61 p[i] = p[i + 2]; 62 } 63 64 // Place the first two elements at the end 65 p[n - 2] = temp1; 66 p[n - 1] = temp2; 67 } 68 69 void print_row(int *p, int n) 70 { 71 int i; 72 for (i = 0; i < n; i++) 73 { 74 printf("%d ", p[i]); 75 } 76 printf("\n"); 77 }