首页 > 解决方案 > 在数组中输入随机数时出错

问题描述

我试图将随机生成的值保存在名为 population 的数组中,但是当我尝试显示数组中的值时,我发现它只保存每列的第一个数字的值,这不是我想要的. 另外,如何更改此代码以保存随机双精度值?

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

// int *randNumsInRange(int upper, int lower, int count)
// {
//  //Declare index
//  int i;
//  //Decalre array in heap to save the randomly generated values
//  int* randNumsArray = (int*)malloc(sizeof(int) * count);
//
//  srand(time(0));
//
//  for (i = 0; i < count; i++)
//  {
//      randNumsArray[i] = (rand() % (upper - lower + 1)) + lower;
//  }
//
//  return randNumsArray;
// }

int main()
{
    //Declaration of variables

    //Variable 'numPoints' will hold the number of of points 
    //Variable 'order' will hold the order of the polynomial
    //Variable 'numPopulation' will hold the number of the population
    int numPoints, order, numPopulation;

    //Variable 'upper' and 'lower' will be used for limiting the coefficient values to certain boundaries
    int upper, lower;

    //Variable 'population' will hold the information of the population in 1D array converted from 2D array
    int* population;

    //Declare indexes for the loops
    int i, j, n;
    int row, col;

    //Variable 'iterCnt' will count the iterations, or so-called generations of the AI
    int iterCnt;

    //Initialize random number gerator by using the time of system clock in seconds
    srand(time(0));

    //Input order of the polynomial fit
    printf("Select the order of the polynomial fit.: ");
    scanf_s("%i", &order);
    printf("The fit will be drawn in the order of %u.\n\n", order);

    //Increment 1 from the value of order to include the constant
    int numCoefficient = order + 1;

    //Input number of population
    printf("Select the desired number of population: ");
    scanf_s("%i", &numPopulation);
    if (numPopulation > 20)
    {
        printf("WARNING: population that is too large can result in slower compilation\n");
    }
    printf("The function will now generate the %u numbers of points.\n\n", numPopulation);

    //Input number of points
    printf("How many points will be provided?: ");
    scanf_s("%i", &numPoints);
    printf("The function will now generate the %u numbers of points.\n\n", numPoints);

    //Initiailize heap location for the coordinates
    int* xData = (int*)malloc(sizeof(int) * numPoints);
    int* yData = (int*)malloc(sizeof(int) * numPoints);
    //can be replaced with the line below for stack:
    //int xData[100000];

    //Initialize heap location for the population which contains coefficient information in 2D array
    population = (int*)malloc(numPopulation * numCoefficient * sizeof(int));

    for (i = 0; i < numPoints; i++)
    {
        printf("Please enter the x coordinate of P(%i).\n", i + 1);
        scanf_s("%i", &xData[i]);
        printf("Please enter the y coordinate of P(%i).\n", i + 1);
        scanf_s("%i", &yData[i]);
    }

    //Get the estimated coefficient value boundary
    printf("What is the estimated absolute value of the coefficient?: ");
    scanf_s("%i", &upper);
    lower = -upper;
    printf("The coefficient will be initialized between %i and %i.\n\n", lower, upper);

    //Generate an initial population
    for (row = 0; row < numPopulation; row++)
    {
        for (col = 0; col < numCoefficient; col++);
        {
            n = (row * numCoefficient) + col;
            population[n] = (int)(rand() % (upper - lower + 1)) + lower; 
            printf("%i", population[n]);
        }

    }

    printf("\nCoefficient Array: \n\n");
    for (i = 0; i < (numPopulation * numCoefficient); i++)
    {
        printf("%i ", population[i]);
        printf("\n");
    }

    //Calculate fitness of the initial population

}

标签: carrays

解决方案


在 for 循环的末尾有一个分号,将数据写入填充缓冲区,如下所示:

for (col = 0; col < numCoefficient; col++);

这个分号截断了 for 循环语句,并且用大括号括起来的主体不是 for 循环的一部分,因此只执行一次。这就是为什么您在人口缓冲区内只能获得一个有效值的原因。您可以通过删除分号来解决此问题,如下所示。

for (col = 0; col < numCoefficient; col++)
{
    n = (row * numCoefficient) + col;
    population[n] = (int)(rand() % (upper - lower + 1)) + lower; 
    printf("%i", population[n]);
}

要生成随机双精度值,请点击查询评论之一中提到的链接。


推荐阅读