hw help please

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];

for (i=0;i<length;i++)
{
cout<<"Enter number "<<i+1<<endl;
cin>>array[i];
}
cout << find_max(array,length) << endl;
cout << find_min(array,length) << endl;
cout <<setprecision(4)<<fixed;
cout << find_average(array,length) << endl;
cout << find_length(array,length) << endl;

// system("pause");
return 0;
}


//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;
}
The error message is clear enough. The size of an array shall be a constant expression which value must be known at compilation time.
Either that or it should be declared with no size at all:
double array[];
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.
http://www.cplusplus.com/doc/tutorial/arrays/
That´s right, i´m sorry...
I got confused with something I saw yesterday on Youtube. It was something like this:

1
2
3
4
5
6
int array [3] = { 12, 24, 36};

// is the same as...

int array[] = { 12, 24, 36 };


But, of course, in this case I´m inizializing the array, and it obviously has three cells.
im suppose to be using a dynamic array for this program and im not sure how to set it up as a dynamic array
1
2
int myArray[5]; // Static Array with a size of 5
int *myArray = new int[myVar]; // Dynamically created array with size of myVar 


Note: The array cannot grow or shrink once created, you must allocate new memory for that (essentially do the same as above, then copy elements over).
Last edited on
Topic archived. No new replies allowed.