can someone please help me understand why i keep getting an error for
double array[length]; is says that lenght it says expression must have a constant value... thank you for your help
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double find_max(double arr[], int length);
double find_min(double arr[], int length);
double find_average(double arr[], int length);
double find_length(double arr[], int length);
int main()
{
int i;
int length;
cout<<"How many values do you want to enter? \n";
cin>>length;
double array[length];
//Find Max:
double find_max(double arr[], int length)
{
double max=999999;
int j;
for (j = 0; j < length; j++)
{
if (arr[j] > max)
{
max = arr[j];
}
}
return max;
}
//Find Min
double find_min(double arr[], int length)
{
double min=999999;
int k;
for (k = 0; k < length; k++)
{
if (arr[k] < max)
{
min = arr[k];
}
}
return min;
}
//Find Average
double find_average(double arr[], int length)
{
int l;
double sum = 0;
double avg;
for (l = 0;l < length; l++)
{
sum+= arr[l];
}
avg = sum/length;
return avg;
}
//find_length
double find_length(double arr[], int length)
{
int m;
double sumsq=0;
double elength;
for (m=0;m<length;m++)
{
sumsq+=(arr[m]*arr[m]);
}
elength = sqrt(sumsq);
return elength;
}
Either that or it should be declared with no size at all:
double array[];
This will create an error as the compiler will not be able to determine how much memory to allocate. You must declare a constant value for the size of the ray in the declaration statement.
NOTE: The elements field within brackets [] which represents the number of elements the array is going to hold, must be a constant value, since arrays are blocks of non-dynamic memory whose size must be determined before execution. In order to create arrays with a variable length dynamic memory is needed, which is explained later in these tutorials.