how can i fix this double *myarray = new double[lenght]; because i get lenght as an undeclare identifier please help thank you

#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 *myarray = new double[lenght];

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

system("pause");
return 0;
}

//Find Max:
double find_max(double arr[], int length)
{
double max = 0;
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] < min)
{
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];
cout <<setprecision(4)<<fixed;
}
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;
}
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````#include #include #include 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 *myarray = new double[lenght]; for (i = 0; i < length; i++) { cout << "Enter number " << endl; cin >> myarray[i]; } cout << find_max(myarray,length) << endl; cout << find_min(myarray,length) <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] < min) { 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]; cout <

That'll start us off on the right note :D
closed account (3qX21hU5)
you misspelled length.....

 ``123`` ``````int length; cout << "How many values do you want to enter? \n"; cin >> length;``````

`double *myarray = new double[lenght];`

Also use code tags when posting code in the forums (Hint: Code tags are the <> under Format: off to your right when replying) it help keep the code formatted so we can read it better.
Last edited on
I'm thinking, if I read somewhere correctly, that * is only pointing an object that you are passing by reference, so anywhere you have in your cout statements myarray, since you pointed to that variable *myarray, you must put an ampersand infront of myarray...

 ``1234567`` ``````cin >> &myarray[i]; } cout << find_max(&myarray,length) << endl; cout << find_min(&myarray,length) <
Last edited on
closed account (3qX21hU5)
@Alistjazz Actually he would not need to add the reference operator in his functions because they don't need to actually change anything in the parameters that they are passed. All they are doing is running calculation on the them and returning a new variable with the answers.

Also I can't seem to tell what you want to do with the find_length function (This is probably because I have been up looking at code for the last 24 hours), so not sure if it is running right at runtime or not. But everything else seems to be in order other then some minor text formatting so the user can tell what the results mean ect.

And dont use `system("pause");` ;p use something like this instead

 ``12345`` `````` std::cout << "Press ENTER to continue..."; std::cin.ignore( std::numeric_limits::max(), '\n' ); return 0; }``````
Last edited on
Another reason you don't need to pass myarray by reference is it is a pointer anyway. Passing a pointer by value only copies the pointer itself, not the object pointed to, so the overhead associated with passing by value is quite minimal. There's nothing wrong with passing a pointer by reference, but it's not really as useful as it is with larger objects, unless you are intending to manipulate the pointer itself and not the data pointed too. On a side note, array names are seen by the compiler as a pointer to the first element of the array so when passing the array to a function you can use array or pointer notation. IE:

 ``12`` ``````void someFunc(double myArray[]); void someFunc(double* myArray);``````

These are exactly the same function declarations.
Last edited on
i found my mistake thank you
+1 for Raezzor : Just learned something

Lol I didn't see the mistake he misspelled length
Topic archived. No new replies allowed.