Im working on a function that takes some values and finds the min, max, and average of the values. I'm passing everything to the function by reference and am getting some errors when I try and do basic operations like + and / Namely the error is
Invalid operands to binary expression ('double *' and 'double *')
void MinMaxAvg(double *pA, double *min, double *max, double *avg, int lines, double *total )
{
for (int i=0; i<lines; i++)
{
if ( i==0)
{
min = &pA[0];
max = &pA[0];
}
else
{
if (&pA[i] < min)
{
min = &pA[i];
}
if (&pA[i] > max)
{
max = &pA[i];
}
}
total += &pA[i]; //<-- Errors on this line
}
avg = (total / lines); // <-- Errors on this line.
}
It seems like you're getting some of the types confused there. In your example you're setting the pointers to a new value, not the value of said pointers.
The first would have to be:
*total += pA[i];
While the second should be:
*avg = (*total / lines);
In fact, you probably want to use floating point division on the second error there (some compilers are notorious for using integer divison in unexpected places):
*avg = (*total / (double)lines);
You'll still be getting errors if you do it like that, however. For example &pA[i] > ... will result in a pointer comparison, i.e. the address of the pointers will be compared. Most likely not what you want.
You're trying to add an address to a pointer, that's not a valid operation.
You probably meant:
*total += pA[i];
Your use of &pA seems very confused, as does the re-assignment of the pointers min and max.
If you have a pointer to a value (like double *min), then *min is how you access (read or write) the value being pointed at.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With