See line 91 of code,
array<double,size1> poundData; //PROBLEM HERE array declared
Global constant size1 can be used to represent integer value 5 everywhere else in code but returns an error if used for declaring the array length.
If size1 is replaced with 5 on this line, code runs perfectly, but need size1 to be used for flexibility. Cannot see why it won't allow size1 to be used, but am very much a beginner so may be something very basic i'm missing.
#include<iostream>
#include <cstdlib>
#include<time.h>
#include <array>
using namespace std;
const int size1 = 5; // declare global constant
double randValInRange (double min,double max,int size1,int i); //Declare functions
array<double,size1> fillArray(double min,double max,int size1);
int main (int argc, char *argv[])
{
if (argc >= 3) //Determine if there are at least 3 command line arguments
{
double min;
double max;
double input1;
double input2;
input1 = atof (argv[1]);
input2 = atof (argv[2]);
if (input1 <= 0.0||input2 <= 0.0) { // Determine that both inputs are positive
cout << " \n min and max must both be positive values\n";
return EXIT_SUCCESS;
}
if (input1 < input2){
min = input1;
max = input2;
}else{
min = input2;
max = input1;
}
cout << min << " lbs is min \n";
cout << max << " lbs is max \n\n";
srand(static_cast<unsigned int>(time(nullptr)));
// std::
array<double,size1> poundData;
poundData = fillArray(min,max,size1);
for (int i=0;i<size1;i++) {
cout<<poundData[i]<<" ";
cout<<"\n";
}
} else {
cout << "Incorrect number of arguments - please call as follows: " <<argv[0] << " min max\n";
}
return 0;
}
double randValInRange (double min,double max,int size1,int i ){
double scale = 100000.0;
double range = max - min;
double rangeScaled = range*scale;
int rangeScaledInt = (static_cast<int>(rangeScaled));
double minScaled = min * scale;
int minScaledInt = (static_cast<int>(minScaled));
int randIntScaled = rand()%(rangeScaledInt);
int outputValueScaled = minScaledInt + randIntScaled;
double outputValueScaledD = static_cast<double>(outputValueScaled);
double outputValue = outputValueScaledD/scale;
return outputValue;
}
array<double,size1> fillArray (double min,double max,int size1) //function with return type array
{
array<double,size1> poundData; //**PROBLEM HERE** array declared
for (int i=0;i<size1;i++) {
double outputValue = randValInRange (min,max,size1,i);
poundData[i] = outputValue;
}
return poundData;
}