Frage

To calculate average, I was able to coding. here suppose there are a number of figures.

      A
    ======
      6
      3
      3
      4
    ====== 

We know the Average formula is : 1/n*sigma(Ai) so i define in code like:

function calculate_average($arr) {
    $count = count($arr); 
    foreach ($arr as $value) {
        $total = $total + $value;
    }
    $average = ($total/$count);
    return $average;
}

$home_values_array = array("6", "3", "3","4");
$average_home_value = calculate_average($home_values_array);
echo 'Average home value: $'.number_format($average_home_value).'</p>';

My next task is calc mean deviation, here is the formula :

  A    B
==========
  6    8
  3    7
  3    6
  4    5
==========

Mean Dev =  1/n*sigma(|Ai-Bi|) 

My question is how to develop my average code to coding the mean dev calculate, or just simply, how to solve the mean dev code.

Big thanks for the help.

War es hilfreich?

Lösung

I don't know it's what you need or not.

function calculate($array_1, $array_2){
   $x = 0; $total = 0;

   foreach($array_1 as $element){
      $total += ($element - $array_2[$x]);
      $x++;
   }

   return ($total / count($array_1));
}

echo calculate(array(6, 3, 3, 4), array(8, 7, 6, 5));

Check this link too see how code run. Hope this helps

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top