| jxovi10 (10) | |
|
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; } | |
|
|
|
| vlad from moscow (3108) | |
| The error message is clear enough. The size of an array shall be a constant expression which value must be known at compilation time. | |
|
|
|
| Marcos Modenesi (27) | |
Either that or it should be declared with no size at all:double array[];
| |
|
|
|
| ryancb06 (26) | |||
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.
| |||
|
|
|||
| Marcos Modenesi (27) | |||
|
That´s right, i´m sorry... I got confused with something I saw yesterday on Youtube. It was something like this:
But, of course, in this case I´m inizializing the array, and it obviously has three cells. | |||
|
|
|||
| jxovi10 (10) | |
| im suppose to be using a dynamic array for this program and im not sure how to set it up as a dynamic array | |
|
|
|
| Volatile Pulse (1329) | |||
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
|
|||