fewkesy Posted February 9, 2011 Share Posted February 9, 2011 Trying to code a first fit algorithm (explanation here). It doesn't output everything correctly at the end. There probably is an easier way to achieve this, but I need to do it using do and while statements. From the output I only get the first array, none of the others display. function first_fit(&$arr, &$max) { $bin = array(array(), array(), array(), array(), array(), array(), array(), array(), array() ,array() ); $i=0; $j=0; do { do { if (array_sum($bin[$j])+$arr[$i] <= $max) {$bin[$j][] = $arr[$i]; $k=1; } $j++; } while($k = 0); $i++; $j=0; } while($i<=9); outputarray($bin[0]); outputarray($bin[1]); outputarray($bin[2]); outputarray($bin[3]); outputarray($bin[4]); outputarray($bin[5]); outputarray($bin[6]); outputarray($bin[7]); outputarray($bin[8]); outputarray($bin[9]); } The function outputarray() just prints the array in a nicely formatted way. Any help would be much appreciated. Link to comment https://forums.phpfreaks.com/topic/227202-help-with-do-while-statement/ Share on other sites More sharing options...
btherl Posted February 10, 2011 Share Posted February 10, 2011 Where do you reset $k back to 0? On a side note, you should remove the "&" in front of $arr and $max at the top of your code, unless you really do need to alter the value of those variables outside the function. Link to comment https://forums.phpfreaks.com/topic/227202-help-with-do-while-statement/#findComment-1172112 Share on other sites More sharing options...
fewkesy Posted February 10, 2011 Author Share Posted February 10, 2011 Thanks for the response. I'd forgot to reset $k, so I put it after the $j reset, however it still did not solve the problem. I am aware of the ampersands and they have been put there on purpose due to other parts of the script. Any more ideas? If it helps $arr is an array of 10 numeric values, and $max is a single numeric value. Link to comment https://forums.phpfreaks.com/topic/227202-help-with-do-while-statement/#findComment-1172118 Share on other sites More sharing options...
btherl Posted February 10, 2011 Share Posted February 10, 2011 I would add some debugging statements. Each time you add to a bin, print out "Adding block of size {$arr[$i]} to bin $j\n". And also print out each time you consider a bin and a block. Printing out the values of $i, $j and $k at each step woudl be good too. Link to comment https://forums.phpfreaks.com/topic/227202-help-with-do-while-statement/#findComment-1172121 Share on other sites More sharing options...
btherl Posted February 10, 2011 Share Posted February 10, 2011 I was also a bit confused as to why this wasn't working so I looked into it further.. it turns out the problem is in this line: while ($k = 0); I'll leave it to you to work out what is wrong with it and why it makes the script act that way Link to comment https://forums.phpfreaks.com/topic/227202-help-with-do-while-statement/#findComment-1172125 Share on other sites More sharing options...
fewkesy Posted February 10, 2011 Author Share Posted February 10, 2011 Thanks, can't believe it was that simple. Just in case anyone does wonder solved by changing the line to while ($k == 0;) Link to comment https://forums.phpfreaks.com/topic/227202-help-with-do-while-statement/#findComment-1172211 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.