Split the array from particular position
C program to split the array from particular position.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void read_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:",i+1);
scanf("%d",&a[i]);
}
}
void print_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:%d\n",i+1,a[i]);
}
}
void main()
{
int arr[MAX], arr1[MAX], arr2[MAX];
int n, i, j, pos;
printf("Enter the number of elements in the array\t:");
scanf("%d",&n);
printf("Enter the elements of the array\n");
read_array(arr, n);
printf("Enter the position from where array is to be splitted\t:");
scanf("%d",&pos);
for(i=0;i<pos;i++)
{
arr1[i] = arr[i];
}
j=0;
for(i=pos;i<n;i++)
{
arr2[j] = arr[i];
j++;
}
printf("\nFirst array\n");
print_array(arr1, pos);
printf("\nSecond array\n");
print_array(arr2, n-pos);
getch();
}
Output
Enter the number of elements in the array :8
Enter the elements of the array
Element 1 :12
Element 2 :23
Element 3 :34
Element 4 :45
Element 5 :56
Element 6 :67
Element 7 :78
Element 8 :89
Enter the position from where array is to be splitted :5
First array
Element 1 :12
Element 2 :23
Element 3 :34
Element 4 :45
Element 5 :56
Second array
Element 1 :67
Element 2 :78
Element 3 :89