C Program to Cyclically Permute the Elements of an Array

This is a C Program to cyclically permutes the elements of an array.

Problem Description

This program first accepts an array. Assume there are 4 elements in an array. It takes 2 element as a first element in an array and so on till the last element of the given array. Now here first element of an array becomes last element in an array during cyclical permutation.

Problem Solution

1. Create a one-dimentional array of some fixed size (lets say n), defining all its elements.
2. Reserve the first element of the array by assigning its value to the nth position of the array.
3. Now using for loop from 0 to size-1, with iterator i, each value at (i+1)th position is assigned to the ith position of array.
4. Because the nth position holds the value of 0th position, therefore the last element will have the value which was earlier the first element.

Program/Source Code

Here is source code of the C program to cyclically permutes the elements of an array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.     /*
  2.      * C program to cyclically permute the elements of an array A.
  3.      * i.e. the content of A1 become that of A2. And A2 contains
  4.      * that of A3 & so on as An contains A1
  5.      */
  6.  
  7.     #include <stdio.h>
  8.     void main ()
  9.     {
  10.  
  11.         int i, n, number[30];
  12.         printf("Enter the value of the n = ");
  13.         scanf("%d", &n);
  14.  
  15.         printf("Enter the numbers\n");
  16.         for (i = 0; i < n; ++i) 
  17.         {
  18.             scanf("%d", &number[i]);
  19.         }
  20.  
  21.         number[n] = number[0];
  22.         for (i = 0; i < n; ++i)
  23.         {
  24.             number[i] = number[i + 1];
  25.         } 
  26.  
  27.         printf("Cyclically permuted numbers are given below \n");
  28.         for (i = 0; i < n; ++i)
  29.             printf("%d\n", number[i]);
  30.  
  31.     }
Program Explanation

1. Create an array of integer of some certain maximum capacity (10, in this case).
2. From users, take a number N as input, which will indicate the number of elements in the array (N < = maximum capacity)
3. Iterating through for loops (from [0 to N) ), take integers as input from user and print them. These input are the elements of the array.
4. Now assign the value of first element of the array at 0th position to the nth position of the array.
5. Starting a for loop, with i as iterator from 0 to size-1, assigning each element at (i+1)th position to ith position, hence each element shifts left by one.
6. And value of last element at (n-1)th position would be assigned a value at nth position. Remember we stored the very first value of array at nth position.

advertisement
advertisement
Runtime Test Cases
Enter the value of the n = 4
Enter the numbers
3
40
100
68
Cyclically permuted numbers are given below
40
100
68
3

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.