Sunday, 28 June 2020

Delete a number by given position from array program | C Language

Q : Write a program to delete an element from a given location of an array of integers.

Answer : 


#include<stdio.h>
#include<conio.h>
#define MAX 50
/*============================
     Girfa Student Help
     Remove from array by given index
==============================*/

void main()
{
     int ar[MAX],n,pos,i;
     clrscr();
     printf("How many number you want to enter>> ");
     scanf("%d",&n);
     if(n>MAX)
           printf("Array capacity is %d",MAX);
     else
     {
           for(i=0;i<n;i++)
           {
                printf("Enter %d'st position number>> ",i+1);
                scanf("%d",&ar[i]);
           }
           clrscr();
           printf("Array before delete\n");
           for(i=0;i<n;i++)
                printf("\t%d",ar[i]);
           printf("\nEnter position number which you want to delete>> ");
           scanf("%d",&pos);
           if(pos>MAX)
                printf("Deletion not possible position is out of range");
           else
           {
                for(i=pos-1;i<n;i++)
                     ar[i]=ar[i+1];
                printf("\nArray after delete\n");
                for(i=0;i<n-1;i++)
                     printf("\t%d",ar[i]);
           }
     }
     getch();
}


No comments:

Post a Comment