The problem is only the value at index 0 seems to be passed.
I am trying to have a function take an array of unsigned integers and determine the min, max and mean.
I'm passing in the reference to min, max, and mean to extract the values later on.
I would really appreciate any help with this! Thank you.
My code:
void Calculate(unsigned int array[], unsigned int &min, unsigned int &max, unsigned int &mean)
{
min = array[0];
max = array[0];
int size = sizeof(array) / sizeof(int); //get length of array
for(int i = 0; i < size; i++){
mean += array[i];
if (array[i] < min)
min = array[i];
if (array[i] > max)
max = array[i];
}
mean /= size;
}
void setup()
{
Serial.begin(9600);
unsigned int min = 0, max = 0, mean = 0;
unsigned int data[] = {13, 88, 65, 9, 108, 43};
Calculate(data, min, max, mean);
Serial.print("Min is: ");
Serial.println(min);
Serial.print("Max is: ");
Serial.println(max);
Serial.print("Mean is: ");
Serial.println(mean);
}
void loop()
{
}
I did get it to work by having my data array as a global variable, and not pass it at all. That's not what I'm looking for though.
When you pass an array to a function you are actually only passing a pointer to it. As a result the size of the array is not known to the function and using sizeof() in the function will only give you the size of the pointer. If you need the size of the array in the function you must pass it as a parameter
When you pass an array to a function you are actually only passing a pointer to it. As a result the size of the array is not known to the function and using sizeof() in the function will only give you the size of the pointer. If you need the size of the array in the function you must pass it as a parameter
You can use templates to handle the size correctly. The compiler will determine the length of the array, and then pass it on as an argument to your function:
PieterP:
You can use templates to handle the size correctly. The compiler will determine the length of the array, and then pass it on as an argument to your function:
So that will not work with dynamically allocated arrays as the compiler has no idea of the size at compile time. Is there a solution for that?
sterretje:
So that will not work with dynamically allocated arrays as the compiler has no idea of the size at compile time. Is there a solution for that?
Note: I'm a C programmer, not a C++ programmer.
The template function won't work, no, because templates are all handled at compile time.
But there's nothing stopping you from calling the function with the size directly.
And if you're serious about dynamic arrays, you probably have some kind of RAII wrapper, so you can just add another overload for the Calculate function:
I do appreciate @Pieter being one of most knowledgeable contributors here in the area of advanced C++ topics. Especially templates, which appear to be his weapon of choice. I always study his post to increase my abilities in using that feature (although, I still find the syntax to be butt-ugly).
However, they aren't the right answer to questions from newbies. In that case, @UKHeliBob's Solution is the way to go.