Reputation: 31
I've tried to save the randomly generated values in the array named population, but when I tried to display the values in the array, I figured out that it only saves the values every first number of each column, which is not what I intended. Also, how can I alter this code to save random double values?
#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
}
Upvotes: 0
Views: 84
Reputation: 683
There is a semicolon at the end of the for loop writing data to the population buffer as shown below:
for (col = 0; col < numCoefficient; col++);
This semicolon truncates the for loop statement and the body enclosed in braces is not part of the for loop and hence, is executed only once. That is why you'd get only one valid value inside the population buffer. You can fix this by removing the semicolon as below.
for (col = 0; col < numCoefficient; col++)
{
n = (row * numCoefficient) + col;
population[n] = (int)(rand() % (upper - lower + 1)) + lower;
printf("%i", population[n]);
}
And for generating random double values, follow the link mentioned in one of the comments on your query.
Upvotes: 1