C Program To Shift Elements of An Array by n Position

Write a C program to shift elements of an array by n positions or rotate the array elements n times.

Example: Expected Input/Output

Input/Output
Enter 5 integer numbers
1
2
3
4
5
Enter the number of positions to shift
1
Enter the direction of shifting …
LEFT: 1 and RIGHT: 0
1
Array after shift operation …
2
3
4
5
1

Visual Representation

Shifting elements of an array

Video Tutorial: C Program To Shift Elements of An Array by n Position


[youtube https://www.youtube.com/watch?v=Fz-oyhu9Ccc]

YouTube Link: https://www.youtube.com/watch?v=Fz-oyhu9Ccc [Watch the Video In Full Screen.]

Source Code: C Program To Shift Elements of An Array by n Position

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, temp, pos, dir;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Enter the number of positions to shift\n");
    scanf("%d", &pos);

    printf("Enter the direction of shifting ...\n");
    printf("LEFT: 1 and RIGHT: 0\n");
    scanf("%d", &dir);

    while(pos)
    {
        if(dir)
        {
            temp = a[0];
            for(i = 0; i < N - 1; i++)
                a[i] = a[i + 1];

            a[N - 1] = temp;
        }
        else
        {
            temp = a[N - 1];
            for(i = N - 1; i > 0; i--)
                a[i] = a[i - 1];

            a[0] = temp;
        }

        pos--;
    }

    printf("Array after shift operation ...\n");
    for(i = 0; i < N; i++)
        printf("%d\n", a[i]);

    printf("\n");

    return 0;
}

Output 1:
Enter 5 integer numbers
1
2
3
4
5
Enter the number of positions to shift
2
Enter the direction of shifting …
LEFT: 1 and RIGHT: 0
0
Array after shift operation …
4
5
1
2
3
Output 2:
Enter 5 integer numbers
1
2
3
4
5
Enter the number of positions to shift
2
Enter the direction of shifting …
LEFT: 1 and RIGHT: 0
1
Array after shift operation …
3
4
5
1
2

Logic To Shift Elements of An Array by n Position

First we ask the user to input N integer numbers and store it inside array variable a[N]. We then ask the user to input the number of positions to shift the elements of the array, and then the direction of shifting. If user inputs 1, then its LEFT shift, if user inputs 0, then its RIGHT shift operation.

Left Shift Operation

            temp = a[0];
            for(i = 0; i < N - 1; i++)
                a[i] = a[i + 1];

            a[N - 1] = temp;

If elements are shifted to left by 1 position, the first element which is present at a[0] will be lost. So we preserve it by transferring value present at a[0] to temp. Next we write a for loop.

We initialize 0 to i and iterate the “for loop” until i is less than (N – 1). Less than (N – 1) means it excludes the last element of the array present at index (N – 1) and that is because we’ll be storing the value present in variable temp to a[N – 1], after the execution of “for loop” completes.

Inside for loop, we transfer the value present at a[i + 1] to a[i]. That way we will be shifting elements of array to left by one position.

Right Shift Operation

            temp = a[N - 1];
            for(i = N - 1; i > 0; i--)
                a[i] = a[i - 1];

            a[0] = temp;

If elements are shifted to right by 1 position, the last element which is present at a[N – 1] will be lost. So we preserve it by transferring value present at a[N – 1] to temp. Next we write a for loop.

We initialize i to (N – 1) and iterate the “for loop” until i is greater than 0 and for each iteration we decrement the value of i by 1. Inside the “for loop” we assign a[i – 1] to a[i]. That is, we keep assigning the value backwards by 1 position. After completion of “for loop” execution, we assign the value of temp to the first index, which is a[0].

In “for loop” condition, we’re checking until i is greater than 0. That means, we’re not checking for i is equal to 0. That is because, at the end of “for loop” we assign the value present in temp to a[0].

While Loop
We put the whole logic of moving the elements to right and left inside of a while loop. While loop executes until variable pos is not equal to zero. Inside “while loop” we keep decrementing the value of i by 1 for each iteration. This way, we keep shifting the elements of the array by 1 position for every iteration of “while loop”. So if user wants to shift the elements to left by 2 positions, the entire “left shifting code/logic” will execute twice – moving the elements of the array 2 positions left.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Shift Variable Values Circularly To Right

Given three variables x, y, z write a function to circularly shift their values to right. In other words if x = 5, y = 8, z = 10, after circular shift y = 5, z = 8, x = 10. Call the function with variables a, b, c to circularly shift values.

Analyze The Above Problem Statement

1. We need to write a function which receives 3 numbers.
2. Inside the function we need to swap the values of 3 variables circularly to the right.

i.e., value of x to y, value of y to z, and value of z to a.

Related Read:
Basics of Pointers In C Programming Language
Swap 2 Numbers Using a Temporary Variable: C

Very Important Note:

Any address preceded by a * (Indirection operator) will fetch the value present at that address.

Video Tutorial: C Program To Shift Variable Values Circularly To Right


[youtube https://www.youtube.com/watch?v=VWh8WKUT5Fc]

YouTube Link: https://www.youtube.com/watch?v=VWh8WKUT5Fc [Watch the Video In Full Screen.]


Source Code: C Program To Shift Variable Values Circularly To Right

#include<stdio.h>

void shift_right(int*, int*, int*);

int main()
{
    int x, y, z;

    printf("Enter 3 numbers\n");
    scanf("%d%d%d", &x, &y, &z);

    printf("Before shifting right: x = %d, y = %d and z = %d\n", x, y, z);

    shift_right(&x, &y, &z);

    printf("After shifting right: x = %d, y = %d and z = %d\n", x, y, z);

    return 0;
}

void shift_right(int *a, int *b, int *c)
{
    int temp;

    temp = *c;
    *c   = *b;
    *b   = *a;
    *a   = temp;
}

Output 1:
Enter 3 numbers
5
8
10
Before shifting right: x = 5, y = 8 and z = 10
After shifting right: x = 10, y = 5 and z = 8

Output 2:
Enter 3 numbers
2
3
1
Before shifting right: x = 2, y = 3 and z = 1
After shifting right: x = 1, y = 2 and z = 3

Logic To Shift Variable Values Circularly To Right

We ask the user to enter 3 numbers and store it inside the address of variables x, y and z. Now we pass these 3 address to the function shift_right();

Inside shift_right() function
Inside shift_right() function we shift the value present at *c to a temp variable. Then, we shift the value of *b to *c, then the value of *a to *b, and then we shift the value present in temp to *a. This shifts the values of variables x, y and z circularly to the right.

*a has the value present at the address &x;
*b has the value present at the address &y;
*c has the value present at the address &z;


int temp;

temp = *c;
*c = *b;
*b = *a;
*a = temp;

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert