请教如何用c语言去除一个数组中所有值为零的元素,而且这些零元素中有连续排列的?

能否给一个示例程序?感激不尽!
比如以下这个数组中有连续的0元素,如何去除所有的零元素?
double a[64]={4.63866e+020,1.456e+027,-7.67487e+017,9.86481e+016,0,0,-3.1101e+014,-9.38282e+010,
1.456e+027,4.60249e+033,-2.3969e+024,3.36857e+023,0,0,-9.64264e+020,-2.93898e+017,
-7.67487e+017,-2.3969e+024,1.27445e+015,-1.52231e+014,0,0,5.19276e+011,1.55469e+008,
9.86481e+016,3.36857e+023,-1.52231e+014,1.02833e+014,0,0,-4.00459e+010,-2.01495e+007,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
-3.1101e+014,-9.64264e+020,5.19276e+011,-4.00459e+010,0,0,2.17304e+008,62943,
-9.38282e+010,-2.93898e+017,1.55469e+008,-2.01495e+007,0,0,62943,19};

int del_zero(double p[],int n)
{
int i,j,len=n;
for(i=0;i<len;i++){
if(p[i]==0.0){

for(j=i;j<len;j++){
p[j]=p[j+1];
}
len--;
}
}
return len;
}
这种方法不能去除连0,求大神帮忙,给多少分都行!

 // ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <math.h>

int trimzero(double arr[], int len)
{
    int last = len;
    while (arr[--last] == 0.0 && last >= 0);
    for (int i = 0; i < last; i++)
    {
        if (arr[i] == 0.0)
        {
            arr[i] = arr[last];
            while (arr[--last] == 0.0 && last >= 0);
        }
    }
    return last + 1;
}

int _tmain(int argc, _TCHAR* argv[])
{
    double a[64] = { 4.63866e+020, 1.456e+027, -7.67487e+017, 9.86481e+016, 0, 0, -3.1101e+014, -9.38282e+010,
        1.456e+027, 4.60249e+033, -2.3969e+024, 3.36857e+023, 0, 0, -9.64264e+020, -2.93898e+017,
        -7.67487e+017, -2.3969e+024, 1.27445e+015, -1.52231e+014, 0, 0, 5.19276e+011, 1.55469e+008,
        9.86481e+016, 3.36857e+023, -1.52231e+014, 1.02833e+014, 0, 0, -4.00459e+010, -2.01495e+007,
        0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0,
        -3.1101e+014, -9.64264e+020, 5.19276e+011, -4.00459e+010, 0, 0, 2.17304e+008, 62943,
        -9.38282e+010, -2.93898e+017, 1.55469e+008, -2.01495e+007, 0, 0, 62943, 19 };
    int n = trimzero(a, 64);
    for (int i = 0; i < n; i++)
    {
        printf_s("%f\n", a[i]);
    }
    return 0;
}

463866000000000000000.000000
1456000000000000000000000000.000000
-767487000000000000.000000
98648100000000000.000000
19.000000
62943.000000
-311010000000000.000000
-93828200000.000000
1456000000000000000000000000.000000
4602490000000000100000000000000000.000000
-2396900000000000000000000.000000
336857000000000000000000.000000
-20149500.000000
155469000.000000
-964264000000000000000.000000
-293898000000000000.000000
-767487000000000000.000000
-2396900000000000000000000.000000
1274450000000000.000000
-152231000000000.000000
-293898000000000000.000000
-93828200000.000000
519276000000.000000
155469000.000000
98648100000000000.000000
336857000000000000000000.000000
-152231000000000.000000
102833000000000.000000
62943.000000
217304000.000000
-40045900000.000000
-20149500.000000
-40045900000.000000
519276000000.000000
-964264000000000000000.000000
-311010000000000.000000
Press any key to continue . . .

 // ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <math.h>

int trimzero(double arr[], int len)
{
    int x = 0;
    for (int i = 0; i < len; i++)
    {
        if (arr[i] == 0.0) continue;
        arr[x] = arr[i];
        x++;
    }
    return x;
}

int _tmain(int argc, _TCHAR* argv[])
{
    double a[64] = { 4.63866e+020, 1.456e+027, -7.67487e+017, 9.86481e+016, 0, 0, -3.1101e+014, -9.38282e+010,
        1.456e+027, 4.60249e+033, -2.3969e+024, 3.36857e+023, 0, 0, -9.64264e+020, -2.93898e+017,
        -7.67487e+017, -2.3969e+024, 1.27445e+015, -1.52231e+014, 0, 0, 5.19276e+011, 1.55469e+008,
        9.86481e+016, 3.36857e+023, -1.52231e+014, 1.02833e+014, 0, 0, -4.00459e+010, -2.01495e+007,
        0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0,
        -3.1101e+014, -9.64264e+020, 5.19276e+011, -4.00459e+010, 0, 0, 2.17304e+008, 62943,
        -9.38282e+010, -2.93898e+017, 1.55469e+008, -2.01495e+007, 0, 0, 62943, 19 };
    int n = trimzero(a, 64);
    for (int i = 0; i < n; i++)
    {
        printf_s("%f\n", a[i]);
    }
    return 0;
}

463866000000000000000.000000
1456000000000000000000000000.000000
-767487000000000000.000000
98648100000000000.000000
-311010000000000.000000
-93828200000.000000
1456000000000000000000000000.000000
4602490000000000100000000000000000.000000
-2396900000000000000000000.000000
336857000000000000000000.000000
-964264000000000000000.000000
-293898000000000000.000000
-767487000000000000.000000
-2396900000000000000000000.000000
1274450000000000.000000
-152231000000000.000000
519276000000.000000
155469000.000000
98648100000000000.000000
336857000000000000000000.000000
-152231000000000.000000
102833000000000.000000
-40045900000.000000
-20149500.000000
-311010000000000.000000
-964264000000000000000.000000
519276000000.000000
-40045900000.000000
217304000.000000
62943.000000
-93828200000.000000
-293898000000000000.000000
155469000.000000
-20149500.000000
62943.000000
19.000000
Press any key to continue . . .
确保顺序但是效率略低的算法

int del_zero(double p[],int n)
{
int i;
int j;
int len=n;

for(i=0;i<len;i++)
{
    if(p[i]>=-DBL_EPSILON||p[i]<=DBL_EPSILON)
    {
        for(j=i;j<len;j++)
        {
            p[j]=p[j+1];
        }
        len--;
    }
}
return len;

}

我试过了 ok

int del_zero(double p[], int n)
{
int i = 0, j, len = n;

while (i < len){
    if (p[i] == 0.0){
        for (j = i; j < len; j++){
            p[j] = p[j + 1];
        }
        len--;
    }
    else
        i++;
}

return len;

}

已修改,测试OK

这是优化连续为0时效率的改进版。

int del_zero(double p[], int n)
{
int i = 0, j, k, len = n;

while (i < len){
    if (p[i] == 0.0){
        j = 1;
        while (i + j < len){
            if (p[i + j] == 0.0)
                ++j;
            else
                break;
        }
        for (k = i; k < len; k++){
            p[k] = p[k + j];
        }

        len -= j;
    }
    else
        ++i;
}

return len;

}

如果还需要优化性能可以写在评论里。。

int del_zero(double p[], int n)
{
int i = 0, j = 0, len = n;

while (i < len){
    if (p[i] != 0.0)
        p[j++] = p[i];
    i++;
}

return j;

}

我看了上面几种方法,只有泡泡鱼的做法是对的吧,浮点数判断0值,是不能直接与0.0比较的。
要根据你需要的精度,设置一个精度的值,然后在精度范围之内的,都应该视为0值。

如下:
const float ZERO_EPSILON = 0.000001;
if (x > - ZERO_EPSILON && x < ZERO_EPSILON )
{为零!}

这样比较才对。

可以用STL的unique函数,

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    double *p=unique(a,a+64); 
    //p指向a数组中去重后的末尾元素
    for(double* t=a;t!=p,t++){
        cout<<*t<<" ";
    }
    return 0;
}


效率约为O(n)的方法

 #include <cstdio>
#include <iostream>
using namespace std;

int main(int argc, char **argv) {
    double a[64]={4.63866e+020,1.456e+027,-7.67487e+017,9.86481e+016,0,0,-3.1101e+014,-9.38282e+010,
1.456e+027,4.60249e+033,-2.3969e+024,3.36857e+023,0,0,-9.64264e+020,-2.93898e+017,
-7.67487e+017,-2.3969e+024,1.27445e+015,-1.52231e+014,0,0,5.19276e+011,1.55469e+008,
9.86481e+016,3.36857e+023,-1.52231e+014,1.02833e+014,0,0,-4.00459e+010,-2.01495e+007,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
-3.1101e+014,-9.64264e+020,5.19276e+011,-4.00459e+010,0,0,2.17304e+008,62943,
-9.38282e+010,-2.93898e+017,1.55469e+008,-2.01495e+007,0,0,62943,19};
    double *x = a; //指向被存储的位置 
    double *y = a; //指向之后非0的位置 
    double *e = a + 64; //指向数组重点 
    while(y < e) {
        *x = *y;
        ++y; ++x;
        while(*y == 0) ++y;
    }
    for(double *s = a; s < x; ++s) cout << *s << endl;
    return 0;
}

int del_zero(double p[],int n)
{
int i;
int j;
int len=n;

for(i=0;i {
if(p[i]>=-DBL_EPSILON||p[i]<=DBL_EPSILON)
{
for(j=i;j<len;j++)
{
p[j]=p[j+1];
}
len--;
}
}
return len;
}