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)
Difficulty Beginner (index: 2 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Array
  • Pointers
  • Iteration
  • Sorting (possible)
  • Recursion

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

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (luc068.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");
    }