Hi,
I am having an issue with a program and I have ended up realizing I don't know how to pass an array as a parameter.
It seems that when I do it the arrays length is one inside of the function, even though outside is different.
Here is the code
include <iostream>
using namespace std;
#define length(x) (sizeof(x)/sizeof(x[0]))
int maximo(int v[])
{
int result = 0;
int tamano;
tamano = length(v);
cout << "length = " << tamano << endl;
for(int i=0; i<tamano; i++) {
if (v[i] > result)
{
result = v[i];
}
}
return result;
}
int main () {
int a1[5] = {0};
int a2[] = {2,3,4};
int a3[4] = {1, 21, 2 ,8};
cout << "array length " << length(a1) << " and the max is: " << maximo(a1) << endl;
cout << "array length " << length(a2) << " and the max is: " << maximo(a2) << endl;
cout << "array length " << length(a3) << " and the max is: " << maximo(a3) << endl;
return 0;
}
The function only tries tu return the maximum of an array of integers. The output is as follows:
length = 1
array length 5 and the max is: 0
length = 1
array length 3 and the max is: 2
length = 1
array length 4 and the max is: 1
I am a bit lost with this and I don't find the solution in the internet (I must be searching bad)
Thanks!