Write a program that will delete all negative number by using Pointer. You have to declare a
pointer array and dynamically allocate memory to input elements of array.
#include<stdio.h>
#include<stdlib.h>
void delete(int n,int *a,int pos){
int i,j;
if(pos<=n){
for(i=pos-1;i<n;i++){
j=i+1;
*(a+i)=*(a+j);
}
}
else{
printf("Invalid Input");
}
}
int main(){
int *arr,num,p;
printf("Enter the size of array:");
scanf("%d",&num);
arr=(int*)malloc(sizeof(int)*num);
printf("Enter array elements:\n");
for(int i=0;i<num;i++){
scanf("%d",(arr+i));
}
printf("\nArray elements before deletion: ");
for(int i=0;i<num;i++){
printf("%d \t",(*(arr+i)));
}
int count=0;
for(int i=0;i<num;i++){
if((*(arr+i))<0){
delete(num,arr,p);
count++;
}
}
printf("\nArray elements after deletion:\n");
for(int i=0;i<num-count;i++){
printf("%d\n",(*(arr+i)));
}
return 0;
}
Comments
Thank you!!!!
Leave a comment