Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当不再需要内存时,将其删除。 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <time.h> // int *ran_C_Arrays - Fatal编程技术网

当不再需要内存时,将其删除。 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <time.h> // int *ran

当不再需要内存时,将其删除。 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <time.h> // int *ran,c,arrays,C,Arrays,当不再需要内存时,将其删除。 #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

当不再需要内存时,将其删除。
#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

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