Delete All Negative Number by Using Pointer (Problem 1.3)

Question 1.3 #: 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.

Solution: 


#include<stdio.h>
int main(){

int n,i,*p,j;
printf("Enter How Numbar: ");
scanf("%d",&n);
printf("\n----------------------------\n");
 p = (int*)malloc(n* sizeof(int));

 for(i=0;i<n;i++){
    printf("Enter Value: ");
    scanf("%d",&p[i]);

 }
 printf("\n----------------------------\n");
 printf("Array Elemants Are: \n");
 for(i=0;i<n;i++){
   printf("%d\t",p[i]);
 }
 printf("\n----------------------------\n");
 int c=0;
 for(i=0;i<n;){
    if(p[i]<0){
            c++;
        for(j=i;j<n;j++){
            p[j]=p[j+1];
        }
    }
    else{
        i++;
    }

 }
 printf("\n----------------------------\n");
 printf("After Delete All Negative Number: \n");
 for(i=0;i<n-c;i++){
    printf("%d\t",p[i]);
 }
}


After Run:

Enter How Number: 4

----------------------------
Enter Value: -3
Enter Value: -4
Enter Value: 2
Enter Value: 5

----------------------------
Array Elements Are:
-3      -4      2       5
----------------------------
After Delete All Negative Number:
2       5
Process returned 0 (0x0)   execution time : 11.464 s
Press any key to continue.







Previous Post Next Post