doucettej3 Posted November 6, 2008 Share Posted November 6, 2008 This code takes an array of random size nd with random integers and then finds the smallest value in the array. Im pretty sure the rest of the code is right up to the output. Any help would be awesome. heres the code function smallest($passed_array) { //initialize the $smallest variable $smallest = $random_max; for($i = 0; $i <= $random_quantity; $i++) { if($smallest > $passed_array[$i]) { $smallest = $passed_array[$i]; } else { $smallest = $smallest; } } print_r($random_numbers); echo "<br>The Smallest number is $small"; } Link to comment https://forums.phpfreaks.com/topic/131671-need-help-wont-output/ Share on other sites More sharing options...
DeanWhitehouse Posted November 6, 2008 Share Posted November 6, 2008 where is $random_max and $random_numbers Defined, and used? Link to comment https://forums.phpfreaks.com/topic/131671-need-help-wont-output/#findComment-683906 Share on other sites More sharing options...
doucettej3 Posted November 6, 2008 Author Share Posted November 6, 2008 They are defined by the user in the input. This function is used in a switch with the function that actually creates the array Link to comment https://forums.phpfreaks.com/topic/131671-need-help-wont-output/#findComment-683912 Share on other sites More sharing options...
DeanWhitehouse Posted November 6, 2008 Share Posted November 6, 2008 Does it print the array in the function? $random_numbers If not then try printing it outside and see if it is a problem somewhere else. Link to comment https://forums.phpfreaks.com/topic/131671-need-help-wont-output/#findComment-683921 Share on other sites More sharing options...
doucettej3 Posted November 6, 2008 Author Share Posted November 6, 2008 Yea it prints the array in the other function..i removed the print from this function ..i still get the same out put...the only thing missing from the output is $small. Link to comment https://forums.phpfreaks.com/topic/131671-need-help-wont-output/#findComment-683926 Share on other sites More sharing options...
doucettej3 Posted November 6, 2008 Author Share Posted November 6, 2008 this is the output : Array ( [0] => 7 [1] => 7 [2] => 5 [3] => 13 [4] => 10 [5] => 6 [6] => 19 [7] => 12 [8] => 14 [9] => 12 [10] => 16 ) The Smallest number is Link to comment https://forums.phpfreaks.com/topic/131671-need-help-wont-output/#findComment-683929 Share on other sites More sharing options...
DeanWhitehouse Posted November 6, 2008 Share Posted November 6, 2008 The var is called $smallest not $small ? Link to comment https://forums.phpfreaks.com/topic/131671-need-help-wont-output/#findComment-683935 Share on other sites More sharing options...
kenrbnsn Posted November 6, 2008 Share Posted November 6, 2008 You don't need a function to get the smallest number in an array, just use rsort() and the smallest number is the $array[0] Example: <?php $rand_array = range(1,100); shuffle($rand_array); echo 'Suffled Array: ' . implode(', ',$rand_array) . '<br>'; $rand_array = array_slice($rand_array,rand(0,30),rand(50,100)); echo 'Sliced Array: ' . implode(', ',$rand_array) . '<br>'; sort($rand_array); echo 'Array contents: ' . implode(', ',$rand_array) . '<br>'; echo 'The smallest number is: ' . $rand_array[0]; ?> Ken Link to comment https://forums.phpfreaks.com/topic/131671-need-help-wont-output/#findComment-683948 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.