C program to delete a specified element from an array | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Wednesday, February 5, 2020

C program to delete a specified element from an array | My CS Tutorial

C program to delete a specified element in an array.
In this tutorial we will solve a problem to delete or remove a specified number from an array.

Here we will write a c program source code to delete a element from an array.

This program will accept the range or size of an array that entered by user through the keyboard.
After it we will enter the elements of this array.

Now we will enter an element to delete or remove from given array.

After these steps compiler check and execute the conditions and print a new array elements list without deleted element.


PROGRAM CODE


#include<stdio.h>
#include<conio.h>
void main()
{
    int x,size,position,deleteitem,flag=0;
    int array[50],temp[50-1];
    clrscr();
    
    printf("\n ENTER THE SIZE OF THIS ARRAY:\n ");
    scanf("%d",&size);
    
    printf("\n ENTER NUMBERS IN THIS ARRAY:\n ");
    
    for(x=0;x<size;x++)
    {
        scanf("%d",&array[x]);
    }

    printf("\n ENTER A ELEMENT OR NUMBER TO BE DELETED FROM THIS ARRAY:\n ");
    scanf("%d",&deleteitem);
    
        for(x=0;x<size;x++)   

  //find position of a number

    {
        if(array[x]==deleteitem)
        {
            position=x;
            flag=1;
        }

    }
    if(flag==1)
    {
        for(x=0;x<size-1;x++)      

   //deleting number in array

        {
        if(x<position)
        {
           temp[x]=array[x];
           }
        if(x>=position)
        {
         temp[x]=array[x+1];
        }

        }

        printf("\n ARRAY AFTER DELETING GIVEN ELEMENT:\n ");
        for(x=0;x<size-1;x++)
        {
        printf("%d\n ",temp[x]);
         }
    }
    else
    {
        printf("\n ELEMENT MISSING.... GIVEN NUMBER IS  NOT FOUND IN THIS ARRA:\n ");
    }
    getch();
}


OUTPUT:





Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment