C Program To Divide/Split An Array Into Two At Specified Position

Lets write a C program to split or divide an array into two arrays at specified position.

Example: Expected Input/Output

Enter 10 integer numbers
-5
-4
-3
-2
-1
0
1
2
3
4
Enter position to split the array in to Two
4

Elements of First Array -> arr1[4]
-5
-4
-3
-2

Elements of Second Array -> arr2[6]
-1
0
1
2
3
4

Visual Representation

Split array at specified position

Video Tutorial: C Program To Divide/Split An Array Into Two At Specified Position


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

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

Source Code: C Program To Divide/Split An Array Into Two At Specified Position

#include<stdio.h>

#define N 10

int main()
{
    int a[N], arr1[N], arr2[N], i, pos, k1 = 0, k2 = 0;

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

    printf("Enter position to split the array in to Two\n");
    scanf("%d", &pos);

    for(i = 0; i < N; i++)
    {
        if(i < pos)
            arr1[k1++] = a[i];
        else
            arr2[k2++] = a[i];
    }

    printf("\nElements of First Array -> arr1[%d]\n", k1);
    for(i = 0; i < k1; i++)
        printf("%d\n", arr1[i]);

    printf("\nElements of Second Array -> arr2[%d]\n", k2);
    for(i = 0; i < k2; i++)
        printf("%d\n", arr2[i]);

    printf("\n");

    return 0;
}

Output:
Enter 10 integer numbers
1
2
3
4
5
6
7
8
9
1
Enter position to split the array in to Two
3

Elements of First Array -> arr1[3]
1
2
3

Elements of Second Array -> arr2[7]
4
5
6
7
8
9
1

Logic To Divide/Split An Array Into Two At Specified Position

We accept N integer numbers from the user and store it inside array variable a[N]. Now we ask the user to input the position at which we need to split the array and create two separate arrays. Next, we iterate through array elements of a, using for loop(loop counter variable i is initialized to 0 and for loop executes until i < N and for each iteration of for loop i value increments by 1) and transfer all the elements of array a to array variable arr1 until i is less than user input position. Once i is greater than or equal to user input position, we transfer elements of a to array variable arr2.

Note: We’ve initialized k1 and k2 to 0, as index starts from 0. Whenever we push / insert values inside arr1 and arr2, we increment the index value of k1 and k2 respectively.

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 Split Even and Odd Elements of An Array Into Two Arrays

Lets write a C program to divide or split even and odd elements of an array into two separate arrays.

Example: Expected Input/Output

Enter 10 integer numbers
1
2
3
4
5
6
7
8
9
10

Array elements of even[5] are …
2
4
6
8
10

Array elements of odd[5] are …
1
3
5
7
9

Visual Representation

Split even and odd elements of array

Related Read:
C Program To Count Number of Even, Odd and Zeros In An Array

Video Tutorial: C Program To Split Even and Odd Elements of An Array Into Two Arrays


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

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

Source Code: C Program To Split Even and Odd Elements of An Array Into Two Arrays

Method 1

#include<stdio.h>

#define N 10

int main()
{
    int a[N], even[N], odd[N], i, k1 = 0, k2 = 0;

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

    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 == 0)
            even[k1++] = a[i];
        else
            odd[k2++] = a[i];
    }

    printf("\n\nArray elements of even[%d] are ...\n", k1);
    for(i = 0; i < k1; i++)
        printf("%d\n", even[i]);

    printf("\n\nArray elements of odd[%d] are ...\n", k2);
    for(i = 0; i < k2; i++)
        printf("%d\n", odd[i]);

    printf("\n");

    return 0;
}

Output:
Enter 10 integer numbers
10
11
12
13
14
15
16
17
18
19

Array elements of even[5] are …
10
12
14
16
18

Array elements of odd[5] are …
11
13
15
17
19

Note: We’re assigning same size to all 3 array variables, because if user enters all odd numbers, then we need to transfer all the elements to array variable odd, for that we need the size of array variable odd to be same as that of the original array. Similarly, if user enters all even numbers we’ll need to have the same size for array variable even.

Logic To Split Even and Odd Elements of An Array Into Two Arrays

First we accept all the elements of an array from the user. Next we iterate through the array elements one by one using a for loop. Inside this for loop we check each individual element, if its even or odd. If a number is perfectly divisible by 2, then its even number else its odd number. If the fetched/selected number is even number, then we copy that number into array variable even, else we copy the element into array variable odd.

Next we use for loop to display the elements of array even and odd, which will have the even and odd elements of the original array.

Method 2

#include<stdio.h>
#include

#define N 10

int main()
{
    int a[N], even[N], odd[N], i, k1 = 0, k2 = 0;

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

        if(a[i] % 2 == 0)
            even[k1++] = a[i];
        else
            odd[k2++] = a[i];
    }

    printf("\n\nArray elements of even[%d] are ...\n", k1);
    for(i = 0; i < k1; i++)
        printf("%d\t", even[i]);

    printf("\n\nArray elements of odd[%d] are ...\n", k2);
    for(i = 0; i < k2; i++)
        printf("%d\t", odd[i]);

    printf("\n");

    return 0;
}

Output:
Enter 10 integer numbers
1
2
3
4
5
6
11
13
15
16

Array elements of even[4] are …
2
4
6
16

Array elements of odd[6] are …
1
3
5
11
13
15

In above source code we check if the number input by the user is even or odd immediately after the user enters a number. We don’t wait until user inputs all the elements of the array. This way we eleminate the need for a separate for loop to check even and odd elements in the original array and then assigning it to array variables even and odd. This is the best approach, which reduces resource usage and has faster execution time.

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