I don't understand these errors can someone explain?
error: invalid type argument of unary '' (have 'double') error: invalid type argument of unary '' (have 'double') error: invalid type argument of unary '*' (have 'double')
double getMedian(double *array, int *hours){
if (*hours <= 0) return 0;
if (*hours % 2) return (float)*array[(*hours + 1) / 2];
else{int pos = *hours / 2;
return (float)(*array[pos] + *array[pos + 1]) / 2;}}
You are already dereferencing array
with the []
operator. What you want is:
double getMedian(double *array, int *hours){
if (*hours <= 0) return 0;
if (*hours % 2) return (float)array[(*hours + 1) / 2];
else{int pos = *hours / 2;
return (float)(array[pos] + array[pos + 1]) / 2;}}
Note that writing x[y]
is shorthand for *(x + (y))
. In your code, you have essentially have the equivalent of **array
.
When you use the [] operator on the arrays or pointers, you don't have to dereference them again to get the value. you could just say,
if (*hours % 2) return (float)array[(*hours + 1) / 2];
and
return (float)(array[pos] + (array[pos + 1]) / 2);
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