C 值未正确保存到数组中

C 值未正确保存到数组中,c,arrays,pointers,C,Arrays,Pointers,我试图计算随机生成的值的适合度,并将每个值保存在名为fitness[]的数组中。显然,代码显示数组中保存的所有值实际上是相同的,因此,计算的最小值和最大值是相同的。我是不是错过了什么重要的东西 int main() { //Declaration of variables //Variable 'numPoints' will hold the number of of points //Variable 'order' will hold the order of

我试图计算随机生成的值的适合度,并将每个值保存在名为fitness[]的数组中。显然,代码显示数组中保存的所有值实际上是相同的,因此,计算的最小值和最大值是相同的。我是不是错过了什么重要的东西

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 coefficients of the population in 1D array converted from 2D array
    int* coefficient;

    //Declare indexes for the loops
    int i, j, k, 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 array
    coefficient = (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;
            coefficient[n] = (int)(rand() % (upper - lower + 1)) + lower; 
        }

    }

    //Print the initial coefficient array in 2D format
    printf("\nCoefficient Array: \n\n");
    for (i = 0; i < (numPopulation * numCoefficient); i++)
    {
        //Convert the coefficient[] into a shape of 2D by entering new line every 'numCoefficient'
        j = i % numCoefficient;
        if (j == 0)
        {
            printf("\n");
            printf("Population %i: ", (i/numCoefficient)+1);
            printf("%i ", coefficient[i]);

        }

        else {
            printf("%i ", coefficient[i]);
        }
    }

    //
    int totSqDiff = 0;
    int minLocation = 0, maxLocation = 0;
    int* fitness = (int*)malloc(sizeof(int) * numPopulation);


    //Initialize the 
    int* yCalculated = (int*)malloc(sizeof(int) * numPoints);
    int* sqDiff = (int*)malloc(sizeof(int) * numPoints);

    //Calculate fitness of the initial population
    for (i = 0; i < numPopulation; i++)
    {
        for (j = 0; j < numPoints; j++)
        {
            for (k = 0; k < order; k++)
            {
                yCalculated[j] = coefficient[k] * pow(xData[j], k);
                sqDiff[j] = pow((yData[j] - yCalculated[j]), 2);
                totSqDiff += sqDiff[j];
            }

        }

        fitness[i] = totSqDiff/numPoints;
        totSqDiff = 0;
        printf("\n%i", fitness[i]);

        if(fitness[i] < fitness[minLocation])
        {
            minLocation = i;
        }

        if(fitness[i] > fitness[maxLocation])
        {
            maxLocation = i;
        }

    }

    printf("\n\nCurrent minimum and maximum fitness present is %i and %i.\n", fitness[minLocation], fitness[maxLocation]);

    free(yCalculated, sqDiff);

}

你为什么不在调试器中运行你的程序,然后一步一步地检查代码来发现问题呢?嗯。。。什么是免费的(yCalculated,sqDiff)应该是什么
free
只接受一个参数。
Select the order of the polynomial fit.: 2
The fit will be drawn in the order of 2.

Select the desired number of population: 5
The function will now generate the 5 numbers of points.

How many points will be provided?: 1
The function will now generate the 1 numbers of points.

Please enter the x coordinate of P(1).
1
Please enter the y coordinate of P(1).
1
What is the estimated absolute value of the coefficient?: 3
The coefficient will be initialized between -3 and 3.


Coefficient Array:


Population 1: -1 -3 2
Population 2: -3 1 -3
Population 3: 2 -3 -1
Population 4: -1 -3 2
Population 5: 0 -2 0
20
20
20
20
20

Current minimum and maximum fitness present is 20 and 20.