Delete an element in an Array
Q. Write a C program to delete an element in an array.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[24],i,n,position;
clrscr();
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the integer: ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
printf("Enter the delete position of an element in an array: ");
scanf("%d", &position);
if (position < n+1)
{
for (i = position - 1; i < n - 1; i++)
{
a[i] = a[i+1];
}
printf("After deleted array of an elements: ");
for (i = 0; i < n - 1; i++)
printf("%d ", a[i]);
}
else
{
printf("Array of element deletion is not possible.");
}
getch();
}
Q. Write an algorithm to delete an element in an array.
1. Start
2. Initialize an array `a` of size 24 (or more depending on requirement)
3. Input the number of elements `n` from the user
4. Input the `n` elements into array `a`
5. Input the `position` to delete from the array (1-based index)
6. Check if `position` is valid:
- If `position >= 1` and `position <= n`, proceed
- Else print "Array element deletion is not possible" and stop
7. If valid:
- For `i` from `position - 1` to `n - 2` (0-indexed):
- Assign `a[i] = a[i+1]` (Shift elements left to overwrite the deleted element)
8. Reduce size of array by 1 (conceptually `n = n - 1`)
9. Print the updated array elements from index 0 to n-1
10. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com