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;
}
int length;
cout << "How many values do you want to enter? \n";
cin >> length;
your problem
double *myarray = newdouble[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.
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...
@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
1 2 3 4 5
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
}
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: