This is a C Program to Split an Array from Specified Position & Add First Part to the End.
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.
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.
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.
* C program to read an array, accept a key & split the array.
* Add the first half to the end of second half.
void main ()
int i, n, a, j;
printf("Enter the value of n\n");
printf("enter the numbers\n");
for (i = 0; i < n; ++i)
printf("Enter the position of the element to split the array \n");
for (i = 0; i < a; ++i)
number[n] = number;
for (j = 0; j < n; ++j)
number[j] = number[j + 1];
printf("The resultant array is\n");
for (i = 0; i < n; ++i)
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.
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.