C Program to Split the Array and Add First Part to the End

This is a C Program to Split an Array from Specified Position & Add First Part to the End.

Problem Description

This program will split an array from specified position & add first part to the end. This program first accepts an array. Then splits an array according to the user specification. Now it becomes 2 parts & then add first part of an array at the end of a second part.

Problem Solution

1. Create an array of some certain size and define its elements in sorted fashion.
2. Take the position from users as input from where you want to split the array.
3. Run a for loop the position entered by user times, in which the element is one by one shifted towards left, and the leftmost element (i.e the first element of the array) is shifted to the nth position of the array.
4. One by one leftwards shifting is again done by a loop from 0 to n(size), where the value at (i+1)th position is assigned to the ith position of array. Thus, the element at nth position (where we stored value of 0th position) is assigned to (n-1)th position of array.

Program/Source Code

Here is source code of the C program to split an array from specified position & add first part to the end. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.  
  2.     /*
  3.      * C program to read an array, accept a key & split the array.
  4.      * Add the first half to the end of second half.
  5.      */
  6.  
  7.     #include <stdio.h>
  8.     void main ()
  9.     {
  10.  
  11.         int number[30];
  12.         int i, n, a, j;
  13.  
  14.         printf("Enter the value of n\n");
  15.         scanf("%d", &n);
  16.  
  17.         printf("enter the numbers\n");
  18.         for (i = 0; i < n; ++i)
  19.             scanf("%d", &number[i]);
  20.  
  21.         printf("Enter the position of the element to split the array \n");
  22.         scanf("%d", &a);
  23.  
  24.         for (i = 0; i < a; ++i) 
  25.         {
  26.  
  27.             number[n] = number[0];
  28.             for (j = 0; j < n; ++j) 
  29.             {
  30.                 number[j] = number[j + 1];
  31.             }
  32.  
  33.         }
  34.  
  35.         printf("The resultant array is\n");
  36.  
  37.         for (i = 0; i < n; ++i) 
  38.         {
  39.             printf("%d\n", number[i]);
  40.         }
  41.  
  42.     }
Program Explanation

1. Declare an array of capacity 20, taking size from users, define all the element of the array but in sorted fashion.
2. Take from users as input the position from where you want to split the array.
3. Now, start a nested for loop, where the outer loop runs the position entered by user times. At each turn the array elements are shifted towards left, shifting the leftmost array element to the nth position of the array (where n is the size of the array)
4. The inner loop shifts all the array elements leftwards one by one by assigning the element at (i+1)th position to the ith position, keeping the element at 0th position to the nth position, so that after this loop ends, the first element of the array becomes the last element.
5. Executing this nested loop will provide us with an array having split up by the position entered by user.
6. Print the array and exit.

advertisement
advertisement
Runtime Test Cases
Enter the value of n
4
enter the numbers
3
678
345
876
Enter the position of the element to split the array
3
The resultant array is
876
3
678
345

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

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.