-
array function errors
I'm writing a function that returns the maximum element of an array. I made an array that had 10 elements. For some reason I keep getting these errors and im not sure why. Any help would be greatly appreciated, thank you.
These are my errors:
Code:
max.cpp(25) : error C2065: 'size' : undeclared identifier
(26)error C2065: 'a' : undeclared identifier
(26) : error C2109: subscript requires array or pointer type
And the following is my code.
Code:
#include <iostream>
using namespace std;
int max (void);//prototype
int main()
{
const int size=10;
int a[size]={1,2,3,4,5,6,7,8,9,10};
max();//call
cout<<"The max number is"<<max<<endl;
return 0;
}
int max(void)//function that returns maximum element of array
{
int MAX=10;
int x=MAX;
for (int i=0; i<size;i++)
if (a[i]==x)
return x;
}
-
because both a and size are local variables to main(). max() can not see nor does it know anything about those two variables. Simple make the following change:
in main() :
Code:
max();//call
cout<<"The max number is"<<max<<endl;
To:
Code:
cout<<"The max number is"<<max(a, size)<<endl;
and change the header for max() to:
Code:
int max (int a[], int size)
-
Ok Thantos I made those changes and now I get a warning:
Code:
warning C4715: 'max' : not all control paths return a value
I have no idea what that means. Here's my edited code.
Code:
#include <iostream>
using namespace std;
int max (int a[], int size );//prototype
int main()
{
const int size=10;
int a[size]={1,2,3,4,5,6,7,8,9,10};
cout<<"The max number is"<<max(a, size);//call
return 0;
}
int max(int a[], int size)//function that returns maximum element of array
{
int MAX=10;
int x=MAX;
for (int i=0; i<size;i++)
if (a[i]==x)
return x;
}
-
Ok what that error means is this:
It is possible that given an unknown array there will never be a value that matches the value of x. Your current function is setup only to return a value if and only if it finds a match. You need to have a return with a specified value at the end of the function to handle the cases where no match is found.
Now that said you should change your max() function to act truely as a max. The following is a fairly easy method for doing it:
Code:
int max (int a[], int size)
{
int max = a[0];
for (int i=1; i < size; i++)
if ( max < a[i] )
max = a[i];
return max;
}
-
It just means that from the point of view of your compiler, it is possible that the values could be such that your if statement in max() never yields true, and that your function will not return a value. As it says, it's just a warning, but if you want to fix the problem use this:
Code:
if (a[i]==x) return x
else return 0
And then just add something to report an error if the result is 0
-
For his loop you do not want to do that sean. If you did that it would return on the first iteration of the loop regardless of its value
-
Code:
int max = a[0];
for (int i=1; i < size; i++)
if ( max < a[i] )
max = a[i];
return max;
Ok thantos, I made the change using the code you provided above and it worked perfectly. I have a small question though. I experimented some and put int max=a[9] since 10 is the biggest element in the array and it worked as well. Is setting it to a[0] more foolproof than a[9] or does it really make any difference at all? Thank you both for your help.
-
Well a[9] worked for you because it was the largest. The reason you use a[0] though is because of this:
1) Regardless of the size a[0] will be valid
2) You have to step through the array one element at a time (unless you know its sorted but thats a different story) as such the first element you encounter will be the max, min, average, mean, etc until you look at another element. So it makes since to initalize your max with that value.
-
Ok that makes sense. Thank you Thantos.
-
Though it seems you are probably doing this as a learning exercise, which is a good thing, just to let you know that there is something already available that will do the same thing you are attempting here. Keep on learning by doing like you are... but file this away in memory for the future.
Code:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
const int size = 10;
int a = {1,2,3,4,5,6,7,8,9,10};
cout << "The max number is " << *max_element(a,a+size) << endl;
return 0;
}
-
Interesting. I will keep that in mind for the future. Thank you very much.