help please urgent

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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#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;
} 

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

Your declared
1
2
3
int length;
cout << "How many values do you want to enter? \n";
cin >> length;


your problem

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...

1
2
3
4
5
6
7
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;
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

1
2
3
4
5
  std::cout << "Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::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:

1
2
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.