Answer to Question #260999 in C for Sadiya

Question #260999

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.


1
Expert's answer
2021-11-04T11:43:28-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Sadiya Afrin
04.11.21, 18:47

Thank you!!!!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS