Delete array element using One Dimensional Array in C


This program is a C program that demonstrates how to delete an element from an array at a specific position. The program begins by declaring some variables: i, j, t, a, n, m, s, b. i and j are used as loop counters, t is used to store the position at which the element needs to be deleted, a is the array, n is the limit of the array, m and s are not used in this program and b is a temporary array used to store the elements of the original array after the element at position t has been deleted.

The program then prompts the user to enter the limit of the array and the values of the elements in the array. The values are stored in the array a. The program then prints out the given values of the array. The program then prompts the user to enter the position of the element that needs to be deleted. It then uses a for loop to iterate over the elements in the array a. For each element, if the current index (i) is not equal to the position of the element to be deleted (t), the element is added to the temporary array b at the next index (j). After the for loop, another for loop is used to copy the elements of the temporary array back to the original array a. Finally, the program prints out the updated values of the array, which now no longer includes the element that was deleted.

Source Code

#include<stdio.h>
int main()
{
  int i,j=0,t,a[10],n,m,s,b[10];
  printf("\nEnter the Limit:");
  scanf("%d",&n);
  printf("\nEnter the values:");
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  printf("\nGiven values are:");
  for(i=0;i<n;i++)
  {
    printf("\na[%d]=%d",i,a[i]);
  }
  printf("\nEnter the position to be deleted:");
  scanf("%d",&t);
  for(i=0;i<n;i++)
  {
    if(i!=t)
    {
      b[j]=a[i];
      j++;
    }
  }
  for(i=0;i<n;i++)
  {
    a[i]=b[i];
  }
  printf("\nUpdated value is:");
  for(i=0;i<n-1;i++)
  {
    printf("\na[%d]=%d",i,a[i]);
  }
  return 0;
}
To download raw file Click Here

Output

Enter the limit:5
Enter the values:1
2
3
4
5
Given values are:
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5
Enter the position to be deleted:3
Updated value is:
a[0]=1
a[1]=2
a[2]=3
a[4]=4
a[5]=5

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C