1#include <stdio.h>2#include <stdlib.h>3 4 //function Declaration5 intCompactintegers (int*p,intn);6 7 intMain ()8 {9 inti;Ten intn,n_final;//N: Used to record the number of integers to be entered; N_final: used to record the length of an array after deleting 0 elements in an array One int*p;//used to point to a dynamically allocated array space A -scanf"%d", &n);//Enter the number of integers to be entered - theP=calloc (N,sizeof(int));//dynamically allocate n length to sizeof (int) Continuous space--dynamic array (number of array elements n, type int) - - for(i=0; i<n;i++)//Enter n integers -scanf"%d", p+i); + -N_final=compactintegers (P,n);//Call the function compactintegers, delete all elements of the array with a value of 0, and then move the element to the first end of the array + Aprintf"%d\n", n_final);//The length of the array after the output deletes the 0 elements in the array at - for(i=0; i<n_final;i++)//The output removes the array element after the 0 element in the array -printf"%d", * (p+i)); - - return 0; - } in - //removes all elements of the array with a value of 0, followed by elements to the first end of the array to intCompactintegers (int*p,intN) + { - inti,j; the intN_temp;//used to record the temporary length of an array during deletion of 0 elements * $N_temp=n;//the temporary length of the assigned array is the original length nPanax Notoginseng - for(i=0; i<n_temp;i++)//iterating through the elements in an array the { + if(* (p+i) = =0)//0 elements found A { the for(j=i;j<n_temp;j++)//removes all elements of the array with a value of 0, followed by elements to the first end of the array +* (P+J) =* (p+j+1); -i--; $n_temp--;//the temporary length of the array minus 1 $ } - } - the returnn_temp; -}
Delete an array of 0 elements