如何使用C中的函数生成随机浮点数组

时间:2015-11-09 19:42:06

标签: c arrays function pointers random

我已经就question问了一个程​​序。我得到了答案,但我有一个新问题。我写的程序不起作用,我不知道为什么。

该函数工作正常(我猜)但我想确保打印数组中每个元素的值,看看它们是否正确(我将需要它用于其他目的)。

我尝试使用 printf 的简单 指令,但数组似乎是空的。问题可能与我要打印的值的内存地址有关。

请看一下,欢迎任何建议!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

int * rand_gen(int N, float fl_value); // Function declaration

int main()  // Main function
{

int N;  // Declaration: Number of trials
printf("Number of trials: ");
scanf("%d", &N);    // Asks for Number of trials

float alpha = 0.3;
float beta  = 0.4;
float gamma = 0.5;
float delta = 0.6;

int i;  // Index
int seed = time(NULL); // Random number generator seed (based on current time)
srand(seed);

// Populate arrays
float *bin_array_alpha[] = { rand_gen( N, alpha ) };
float *bin_array_beta[]  = { rand_gen( N, beta )  };
float *bin_array_gamma[] = { rand_gen( N, gamma ) };
float *bin_array_delta[] = { rand_gen( N, delta ) };



// Here I would like to print the elements of the arrays, something like:

for ( i=0 ; i<N ; i++)
{
    printf("\nbin_array_alpha[%d] = %f",i,bin_array_alpha[i]);
    printf("\nbin_array_beta[%d]  = %f",i,bin_array_beta[i]);
    printf("\nbin_array_gamma[%d] = %f",i,bin_array_gamma[i]);
    printf("\nbin_array_delta[%d] = %f",i,bin_array_delta[i]);
    printf("\n");
}


// Free the memory
static const size_t m = sizeof(bin_array_alpha)/sizeof(bin_array_alpha[0]);

for ( size_t i = 0 ; i < m ; ++i )
{
    free(bin_array_alpha[i]);
    free(bin_array_beta[i]);
    free(bin_array_gamma[i]);
    free(bin_array_delta[i]);

    bin_array_alpha[i] = NULL;
    bin_array_beta[i]  = NULL;
    bin_array_gamma[i] = NULL;
    bin_array_delta[i] = NULL;
}

printf("\n");

return(0);
}


// Function: generate an array populated by: array[j] = rand()*fl_value
int * rand_gen(int N, float fl_value)
{
    int *array;
    array = (int *)malloc(sizeof(float)*N);
    if(array == NULL)
    {
        printf("\nRun out of memory!\n");
        exit(1);
    }

    int j;
    float x;
    for( j = 0 ; j < N ; j++ )
    {
        x = rand(); // Generates a random number
        x = x/RAND_MAX; // 0 < x < 1
        x = x * fl_value;
        array[j] = x; // array[j] = x * fl_value
    }

    return array;
}

结果始终相同(例如N = 3):

bin_array_alpha[0] = 0.000000
bin_array_beta[0]  = 0.000000
bin_array_gamma[0] = 0.000000
bin_array_delta[0] = 0.000000

bin_array_alpha[1] = 0.000000
bin_array_beta[1]  = 0.000000
bin_array_gamma[1] = 0.000000
bin_array_delta[1] = 0.000000

bin_array_alpha[2] = 0.000000
bin_array_beta[2]  = 0.000000
bin_array_gamma[2] = 0.000000
bin_array_delta[2] = 0.000000

再次感谢你!

1 个答案:

答案 0 :(得分:2)

你的问题在于浮点数组的分配,你不是简单地与int混淆,但分配技术是错误的:

float **rand_gen(int N, float fl_value)
{
    float **array;
    array = (float **)malloc(sizeof(float *) * N + 1);
    if (array == NULL)
    {
        printf("\nRun out of memory!\n");
        exit(1);
    }
    int i;
    for (i = 0; i < N; i++)
    {
        array[i] = (int *)malloc(sizeof(float));
        array[i][0] = 0;
    }
    array[N] = NULL;

    int j;
    float x;
    for (j = 0; j < N; j++)
    {
        x = rand(); // Generates a random number
        x = x / RAND_MAX; // 0 < x < 1
        x = x * fl_value;
        *(array[j]) = x; // array[j] = x * fl_value
    }

    return array;
}

float数组是指向内存区域的指针,其中每个元素都是指向float的指针。

这意味着您必须更正声明:

float **rand_gen(int N, float fl_value); // Function declaration

以及主要用法:

float **bin_array_alpha = rand_gen(N, alpha);
float **bin_array_beta = rand_gen(N, beta);
float **bin_array_gamma = rand_gen(N, gamma);
float **bin_array_delta = rand_gen(N, delta);

printf也是如此:

printf("\nbin_array_alpha[%d] = %f", i, *bin_array_alpha[i]);

相反,为了释放内存,你需要不仅添加每个浮点数而且还添加内存基数:

free(bin_array_alpha