Spreegem Posted October 21, 2009 Share Posted October 21, 2009 So I'm working on a divide and conquer function to find the largest value in an array but I don't seem to get any value returned, when I echo theres nothing. Any ideas? Thanks a bunch in advance. <?php $array_size = 9; $numbers = array(8, 6, 18, 14, 24, 30, 12, 26, 16); //print_r($numbers); function largest($x, $y) { if ($x > $y) { echo">y<br>"; return $x; } else { echo"!>y<br>"; return $y; } } function maxArray($array_size, $first, $last) { if ($array_size == 1) { echo"=1<br>"; return largest($numbers[$first], $numbers[$last]); } else if($array_size > 1) { echo">1<br>"; return largest(maxArray((int)($array_size/2), $first, (int)($first + $last)/2), maxArray((int)($array_size/2), (int)($first + $last)/2, $last)); } } $max_value = maxArray($array_size, 0, ($array_size - 1)); echo "The largest value in the array is: $max_value"; ?> Link to comment https://forums.phpfreaks.com/topic/178488-function-not-returning-value/ Share on other sites More sharing options...
Mark Baker Posted October 21, 2009 Share Posted October 21, 2009 $numbers is not in scope within the maxArray() function. PS. Why not simply sort the array and then return the last value in the array. Link to comment https://forums.phpfreaks.com/topic/178488-function-not-returning-value/#findComment-941253 Share on other sites More sharing options...
Spreegem Posted October 21, 2009 Author Share Posted October 21, 2009 I'm supposed to solve the problem using divide and conquer. Thanks for the speedy reply I'll have to go figure that out. Link to comment https://forums.phpfreaks.com/topic/178488-function-not-returning-value/#findComment-941258 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.