Task: Create a function "addAll()" that will take an array as input parameter. The function will sum all the elements in the array and then remove the first element of the array. The function should repeat this until the array is empty and then return the total. For example: For the array [1,1,1,1,1], the result should be 15 -> 5+4+3+2+1=15
<?php function addAll($Array) { $total = 0; foreach ($Array as $value) { $total += $value; } return $total; } //$remove = array_shift($sumArray); /** Given array **/ $sumArray = array(1,1,1,1,1); /** Calling function **/ $myAnswer = addAll($sumArray); /** My result **/ echo "The result is : " , $myAnswer; // result = 5 ?>
(a + b + c + d + e) + (b + c + d + e) + (c + d + e) + (d + e) + e
1 + 2b + 3c + 4d + 5e
1*$Array[0] + 2*%Array[1] + 3*$Array[2] + ... + n*$Array[n-1]
n
$k
(k+1)*$Array[$k]
addAll
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)