I built a function to generate a random 2-d array in C++. I was hoping that I could set the size of the array at compile time, so I included variables for the number of rows and columns in the array. However, when I try to compile the function, I get an error about the storage size of the array is not constant. This seems to do with the the static keyword that I have to add to the array definition, so that I can return the pointer from the function. I was not sure if there is a way around this error? Any suggestions.
double * generate_random_array(int rows, int cols, double lower_, double upper_){
static double test_array[rows][cols];
for (int i = 0; i < sizeof test_array / sizeof test_array[0]; i++) {
for (int j = 0; j < sizeof test_array[0] / sizeof(double); j++) {
test_array[i][j] = generate_random_numbers(lower_, upper_);
}
}
return(test_array);
}
rowsandcolsare not known at compile time, you can callgenerate_random_arraywith whatever arguments you want.