0

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

}
3
  • 1
    Can you clarify your question by (a) trimming down the code to just the relevant part (ie. just the part where things go wrong, while still keeping it compilable) and (b) showing what the output was that you got, and what output you expected instead (as well as why you expected so). Commented Nov 26, 2019 at 7:36
  • Furthermore, please ask only one question. It's best to create a separate question for how to save random double values. But first check if other questions don't already answer that, like eg. : generate random double numbers in c++ (note that while the question is for C++, the accepted answer applies equally well to C). Commented Nov 26, 2019 at 7:39
  • In addition to the expected and actual output, show also your example input corresponding to the output. Commented Nov 26, 2019 at 8:11

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh my god. How did I miss that? Thank you so much. By the way, what should I do if I want to add decimal values in the array? What part of the code must be changed to do so?
You need to change the type of the population variable to double *. Also, I am assuming the upper and lower variables refer to the minimum and maximum values of the coefficients so they also need to be double. You need to use %lf as format specifier to scanf to input double values. See this link to see your code modified to use decimal values: wandbox.org/permlink/lLmjebQV4LB8iCib Finally, don't forget to free the malloced memory when it is no longer required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.