0

Here is the code I am using:

<?php
function Median($dataPoints) {
    return Quartile_50($dataPoints);
}

function Quartile_25($dataPoints) {
    return Quartile($dataPoints, 0.25);
}

function Quartile_50($dataPoints) {
    return Quartile($dataPoints, 0.5);
}

function Quartile_75($dataPoints) {
    return Quartile($dataPoints, 0.75);
}

function Quartile($dataPoints, $Quartile) {
    sort($dataPoints);
    $pos = (count($dataPoints)-1) * $Quartile;

    $base = floor($pos);
    $rest = $pos - $base;

    if( isset($dataPoints[$base+1]) ) {
        return $dataPoints[$base] + $rest * ($dataPoints[$base+1] - $dataPoints[$base]);
    } else {
        return $dataPoints[$base];
    }
}

function Average($dataPoints) {
    return array_sum($dataPoints)/ count($dataPoints);
}

function StdDev($dataPoints) {
    if(count($dataPoints) < 2) {
        return;
    }
    $avg = Average($dataPoints);

    $sum = 0;
    foreach($dataPoints as $value) {
        $sum += pow($value - $avg, 2);
    }
    return sqrt((1/(count($dataPoints)-1))*$sum);
}

And here is the error I am receiving:

Fatal error: Uncaught Error: Unsupported operand types in C:\xampp\htdocs\arraytest.php:62 Stack trace: #0 C:\xampp\htdocs\arraytest.php(45): Quartile(Array, 0.25) #1 C:\xampp\htdocs\arraytest.php(84): Quartile_25(Array) #2 {main} thrown in C:\xampp\htdocs\arraytest.php on line 62

Here is line 62:

return $dataPoints[$base] + $rest * ($dataPoints[$base+1] - $dataPoints[$base]);

$dataPoints is my array so I thought it was because I could not subtract two arrays. I still received an error when I replaced the subtraction function with array_diff(). Am I inputting the Quartile() function correctly?

Thanks for the help

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

0 Answers0