Showing posts with label how to delete element in array?. Show all posts
Showing posts with label how to delete element in array?. Show all posts

Thursday, 22 March 2012

Delete element in array

Q. Write a C program to delete a value or element in array and user should guide which value is deleted.


Ans.


/*c program for delete a number in array*/
#include<stdio.h>
#include<conio.h>

#define SIZE 50
int main()
{
 int arr[SIZE];
 int i,num,index;
 printf("Enter total number of element in array : ");
 scanf("%d", &num);
 for(i=0; i<num; i++)
 {
  printf("Enter %d element : ",i+1);
  scanf("%d",&arr[i]);
 }
 printf("\nEnter element number which you want to delete : ");
 scanf("%d", &index);
 if(index >= num+1)
   printf("\nElement not found!!");
 else
 {
  for(i=index-1; i<num-1; i++)
     arr[i]=arr[i+1];
  printf("\n-- After deletion new array list --\n\n");
  for(i=0; i<num-1; i++)
    printf("\t%d\n",arr[i]);
 }
 getch();
 return 0;
}


/*************** Output *****************/
Screen shot of delete element in array




Related Programs:

  1. How to create array in C?
  2. How to calculate length of array?
  3. Insert new element in array
  4. Display array in reversing order
  5. Example of array C program