Skip to content

luc068.c

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.

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 08 Feb 2026
License MIT License (See the LICENSE file for details)

Actions

Raw View on GitHub

💡 You can print or save this file by opening Raw and using your browser.

Source Code

#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");
}