WAP for delete an element at desired position in an array?
#include <stdio.h>
#include <conio.h>
int main()
{
int arr[200];
int i, n, pos;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element position to delete : ");
scanf("%d", &pos);
if(pos==n+1 || pos<0)
{
printf("Invalid position! Please enter position between 1 to %d", n);
}
else
{
for(i=pos-1; i<n-1; i++)
{
arr[i] = arr[i+1];
}
n--;
}
printf("\nElements of array after delete are : ");
for(i=0; i<n; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}
#include <stdio.h>
#include <conio.h>
int main()
{
int arr[200];
int i, n, pos;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element position to delete : ");
scanf("%d", &pos);
if(pos==n+1 || pos<0)
{
printf("Invalid position! Please enter position between 1 to %d", n);
}
else
{
for(i=pos-1; i<n-1; i++)
{
arr[i] = arr[i+1];
}
n--;
}
printf("\nElements of array after delete are : ");
for(i=0; i<n; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}
No comments:
Post a Comment