较大数组

时间:2016-07-30 22:29:24

标签: c arrays

我正在尝试创建一个新数组,该数组的索引号将是一个符合某种条件的更大数组。

例如,有一大堆0和1,我想要一个列出每个索引的数组:

  int A[] = {1,1,0,0,1,1,0,0,0,0};    
  int B[4];    
    ...      
  for(i = 0;i < 4;i++)
  {
     printf("%d \n",B[i]);
  }

结果:

0
1
4
5

在小规模(对于上面),我通过使用嵌套的for循环和break命令来工作:

//nested loop 
//reading index
#include <stdio.h>

int main()
{
    int a,b;
    int c[10] = {-1, -1, 0, 0, -1, 1, 0, 0, 0, 0};
    int d[4];

    b = 0;
    for(a = 0;a < 5;)
    {
        for(;b < 10;b++)
        {
            if(c[b] == -1)
            {
                d[a] = b;
                break;
            }

        }
        b++;
        a++;
    }

    for(a = 0;a < 4;)
    {
        printf("%d \n",d[a]);
        a++;
    }
}

当我尝试大规模地实现更大的阵列集时,问题出现了(其中,我不知道第二个阵列需要多大)。这就是我所拥有的:

 #include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define N 283

int im_index_finder(int inarray[N - 2],int min_or_max);

void main()
{
    double t[N], d[N], velocity[N], averagevelocity[N - 1], n_d[N];
    double dx[N - 1];
    int b[N - 1], d_b[N - 2];
    int i,j,k,a,c,imax_index,imin_index;
    int imax[N - 2], imin[imin_index];

    FILE *fid;

    fid = fopen("squatsedit.csv","r");

    //      Reading the data from csv file, velocity, and average velocity

    for(i = 0;i < N;)
    {
        fscanf(fid, "%lf, %lf", &t[i], &d[i]);
        //printf("%g  %g\n", t[i], d[i]);
        velocity[i] = d[i] / t[i];
        i++;
    }

    for(i = 1;i < N;)
    {
        averagevelocity[i - 1] = (d[i] - d[i-1])/(t[i] - t[i-1]);
        i++;
    }

    for(i = 0;i < N;)
    {
        n_d[i] = d[i];
        i++;
    }

    //      Filtering of the distance data

    for(i = 0;i < 2;)
    {
        for(j = 2;j < N;)
        {
            k = j - 1;
            n_d[k] = ( n_d[k - 1] + n_d[k] + n_d[k + 1] )/3;
            j++;
        }
        i++;
    }

    //      start of extrema replica

    for(i = 1;i < N;)
    {
        dx[i - 1] = (n_d[i] - n_d[i-1]);
        i++;
    }

    for(i = 0;i < N - 1;)
    {                                   //checking peaks for maxima and minima
        if(dx[i] > 0)
        {
            b[i] = 1;                   //b[i] = 1 for positive changes in slope
        }
        else
        {
            b[i] = 0;                   //b[i] = 0 for negative changes in slope
        }
        i++;
    }

    for(i = 1;i < N - 1;)
    {
        d_b[i - 1] = b[i] - b[i - 1]; 
        i++;
    }

    imax_index = im_index_finder(d_b,1);
    imin_index = im_index_finder(d_b,0);

    c = 0;
    for(a = 0;a < imax_index;)
    {
        for(;c < N - 2;c++)
        {
            if(d_b[c] == -1)
            {
                imax[a] = c + 1;
                break;
            }
        }
        c++;
        a++;
    }

    c = 0;
    for(a = 0; a < imin_index;)
    {
        for(;c < N - 2;c++)
        {
            if(d_b[c] == 1)
            {
                imin[a] = c + 1;
               break;
            }
        }
        c++;
        a++;
    }

    printf("Our squatsedit is:\n");
    for(i = 0;i < 5;)
    {
        printf("%g   %g \n",t[i],d[i]);
        i++;
    }
    printf("...\n");
    printf("Our velocities are:\n");
    for(i = 0; i < 5;)
    {
        printf("%e \n",velocity[i]);
        i++;
    }
    printf("...\n");
    printf("Our average velocities are:\n");

    for(i = 0;i < 5;)
    {
        printf("%f \n",averagevelocity[i]);
        i++;
    }
    printf("...\n");
    printf("Our filtered distances are:\n");
    for(i = 0;i < 5;)
    {
        printf("%f \n",n_d[i]);
        i++;
    }
    printf("...\n");
    printf("Then some differences are:\n");
    for(i = 0;i < 5;)
    {
        printf("%f \n",dx[i]);
        i++;
    }
    printf("...\n");
    printf("Our positve and negative detector says:\n");
    for(i = 0;i < 5;)
    {
        printf("%d \n",b[i]);
        i++;
    }
    printf("...\n");
    printf("Our xb equivalent is:\n");
    for(i = 0;i < 10;)
    {
        printf("%d \n",d_b[i]);
        i++;
    }
    printf("...\n");
    printf("our imax_index is %d \n",imax_index);
    printf("our imin_index is %d \n",imin_index);
    for(i = 0;i < imax_index;)
    {
        printf("%d \n",imax[i]);
        i++;
    }
    //printf("our imin is:\n");
    //for(i = 0;i < imin_index;)
    //{
    //    printf("%d \n",imin[i]);
    //    i++;
    //}

    fclose(fid);

}

int im_index_finder(int inarray[N - 2],int min_or_max)
{
    int a;
    int max_index = 0;
    int min_index = 0;

    for(a = 0;a < N - 2;)
    {
        if(inarray[a] == -1)
        {
            max_index++;
        }
        else if(inarray[a] == 1)
        {
            min_index++;
        }
        a++;
    }

    if(min_or_max = 1)
    {
        return max_index;
    }
    else
    {
        return min_index;
    }
}

然而,当它显示时,imin数组的最后七个元素应该是imax数组的开始元素。

当我注释掉imin循环时,imax正确呈现,但我也需要imin,有人能帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

你在imin []的声明中使用了imin_index,但imin_index是什么价值?

int i,j,k,a,c,imax_index,imin_index;
int imax[N - 2], imin[imin_index];

那么imin []的大小是多少?

答案 1 :(得分:0)

1)遍历主数组以查找与您的条件匹配的元素数。 2)动态分配大小等于该计数的数组。 3)再次循环第一个数组并将匹配的元素移动到动态分配的第二个数组。