Delete an array of 0 elements

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.