doucettej3 Posted November 7, 2008 Share Posted November 7, 2008 i am using a function that finds the smallest value in an array. The array is of random size and has random values. The size is input by the user and the range of the values is input by the user. The output is givin me this : Array ( [0] => 15 [1] => 3 [2] => 19 [3] => 13 [4] => 9 [5] => 15 [6] => 19 [7] => 6 [8] => 18 [9] => 5 [10] => 5 ) The Smallest number is 1 one obviously isnt the smallest number because its not even in the array. Any help with the code would be great. Also it gets the array thru a switch statement with a function that creates the array. function small_array($passed_array, $random_max, $random_quantity) { //initialize the $small variable $small = $random_max; for($i = 0; $i < $random_quantity; $i++) { if($small >= $passed_array[$i]) { $small = $passed_array[$i]; } if($small < $passed_array[$i]) { $small = $small; } } echo "<br>The Smallest number is $small"; } Link to comment https://forums.phpfreaks.com/topic/131778-solved-php-math-error-for-array/ Share on other sites More sharing options...
Mchl Posted November 7, 2008 Share Posted November 7, 2008 Try: function small_array($passed_array, $random_max, $random_quantity) { //initialize the $small variable $small = $random_max; for($i = 0; $i < $random_quantity; $i++) { if($small >= $passed_array[$i]) { $small = $passed_array[$i]; } } echo "<br>The Smallest number is $small"; } for beginning. BTW: you can use min() to get smallest value from an array. Link to comment https://forums.phpfreaks.com/topic/131778-solved-php-math-error-for-array/#findComment-684524 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.