C Program To Delete Element of An Array at Specified Position

Write a C program to delete element of an array at user specified position. Show a confirmation message before deleting the element from specified position.

Related Read:
C Program To Shift Elements of An Array by n Position

Example: Expected Input/Output

Enter 5 integer numbers
10
12
14
16
18

Enter the position of the element to be deleted
2

You want to delete element 14 at position 2?
Yes: 1, No: 0
1

Array after deleting the specified element …
10
12
16
18

Visual Representation

delete element of an array at specified position

Video Tutorial: C Program To Delete Element of An Array at Specified Position


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

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

Source Code: C Program To Delete Element of An Array at Specified Position

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, pos, flag = 0;

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

    do
    {
        printf("\nEnter the position of the element to be deleted\n");
        scanf("%d", &pos);

        if(pos >= N)
            printf("\nPlease enter position within the range/size of the array\n");
        else
        {
            printf("\nYou want to delete element %d at position %d?\n", a[pos], pos);
            printf("Yes: 1, No: 0\n");
            scanf("%d", &flag);
        }
    }while(flag == 0);

    for(i = pos; i < (N - 1); i++)
        a[i] = a[i + 1];

    printf("\nArray after deleting the specified element ...\n");
    for(i = 0; i < (N - 1); i++)
        printf("%d\n", a[i]);

    printf("\n");

    return 0;
}

Output 1:
Enter 5 integer numbers
1
5
9
7
3

Enter the position of the element to be deleted
4

You want to delete element 3 at position 4?
Yes: 1, No: 0
0

Enter the position of the element to be deleted
2

You want to delete element 9 at position 2?
Yes: 1, No: 0
1

Array after deleting the specified element …
1
5
7
3

Output 2:
Enter 5 integer numbers
1
2
3
4
5

Enter the position of the element to be deleted
15

Please enter position within the range/size of the array

Enter the position of the element to be deleted
0

You want to delete element 1 at position 0?
Yes: 1, No: 0
1

Array after deleting the specified element …
2
3
4
5

Logic To Delete Element of An Array at Specified Position

We ask the user to enter N integer numbers and store it inside array variable a[N]. Next we ask the index position of the element which has to be deleted from the array. Once we’ve these inputs from the user, we show a confirmation message to the user before deleting the specified element. Here user can either choose “1” to delete the element or choose “0” to re-select the element to be deleted.

If user selects “1” and chooses to delete the selected element, then we initialize i to the position of the element to be deleted. We iterate the for loop until i is less than (N – 1), and for each iteration we increment the value of i by 1.

Note: Observe the condition i < (N – 1), which is similar to writing i <= (N – 2). That is because we don’t want to swap/insert the last element of the array with some garbage value present outside the array limit/size.

Inside the for loop
Inside the ‘for loop’ we assign the element present in the next index to the current index element selected by i. i.e,. a[i] is assigned the value present at a[i + 1]. This way the value or the element present at the user selected position is lost, and the index N – 1 and N – 2 will have same elements. While displaying the array elements we leave the last index element, indicating deletion of 1 element from the array – so the array size has been obviously reduced by 1.

Note: Here array variable size isn’t reduced, but we simply do not display the last element of the array and create the illusion of reducing the size of array by 1.

Explanation With Example

If a[5] = {10, 12, 14, 16, 18};
Delete element/number at position: 2
Element at user specified position: 14

    for(i = pos; i < (N - 1); i++)
        a[i] = a[i + 1];

We initialize i to user specified position, which is present in variable pos. Iterate this for loop until i in less than (N – 1), for each iteration increment the value of i by 1.

iposa[i]a[i + 1]a[i] = a[i + 1]
22a[2] = 14a[3] = 16a[2] = 16
32a[3] = 16a[4] = 18a[3] = 18
42a[4] = 18

Now that index i is 4 and the array size is N = 5. So 4 < (5 – 1) is 4 < 4 which returns false, so the control exits the for loop. Using another “for loop” we display the array elements from index 0 to N – 2. i.e., from index i to i < (N – 1) or i <= (N – 2).

So the array elements after execution of above logic:
a[4] = {10, 12, 16, 18};

That’s how we successfully delete a element/number from user specified position 2.

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 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