如何将多个数组从函数传递到main?

时间:2017-02-05 02:25:46

标签: c arrays pointers

我不习惯在C中使用指针或编码。我已经创建了一个void函数,在其中我定义了几个数组并使用我的Input参数填充它们。现在我需要在我的主要部分使用这些数组,但不确定如何调用它们。以下是我的功能代码:

void showCDF(int Integer_Array[], int length){ //Calculates the CDF and stores it in "y_values"     
    int i = 1;    // i initialized at 1
    int y_axis[20];
    int size_of_x = Integer_Array[length-1]- Integer_Array[0]+1;    // size of x is the difference between the first and 
    int x_axis[size_of_x];            // last element of input array.
    x_axis[0] = Integer_Array[0];     // first element of x is the first element of input array
    //printf("%d ", x_axis[0]);       // print out the first element
    while (x_axis[i-1]<Integer_Array[length-1]){    // while previous element of x < last element of Input
        x_axis[i]= x_axis[i-1] + 1;    // adding the previous value of x to 1 store as this value
        //printf("%d ", x_axis[i]);    // print out all consecutive elements of x
        i +=1;
    }

    printf("\n");
    for (i=0;i<21;i++){
        y_axis[i] = 5*i;
        //printf("%d \n",y_axis[i]);
    }
    int y_values[size_of_x + 1];    // size of y is dependant on the size of x
    for (int j=0; j < size_of_x +1 ; j++){
        float n = 0;    // Count to show total, initialized at 0
        int i= 0;
        while (x_axis[j] >= Integer_Array[i] && i<length){  // While x at j is greater than or equal to values of 
            n ++;    //  Integer_array at i, AND i is less than length.
            i ++;    // Then increase the count and i
        }            //  else:
        y_values[j]=(n/length)*100;     // Then corresponding y at j= % of n / length (or the cdf)
        //printf( "%d ", y_values[j]);  // print out the cdf
    }
}       

在我的主要我调用我的函数和变量y_axis,y_value,x_axis就像这样:

int y_axis[20]; 
showCDF(integerArray, NUMBER_ENTRIES);
for (i=0;i<21;i++){ 
    printf("%d \n", y_axis[i]);
}

但这只是给我一个像我这样的胡言乱语的数字:

0 0 0 0 0 0 0 0 0 0 0 0 1 0 4199677 0 0 0 0 0 825307441

任何帮助将不胜感激!

0 个答案:

没有答案