Infinite loop problem need help plz

Thread Solved

Join Date: Nov 2008
Posts: 3
Reputation: Frosty[tnt] is an unknown quantity at this point 
Solved Threads: 0
Frosty[tnt] Frosty[tnt] is offline Offline
Newbie Poster

Infinite loop problem need help plz

 
0
  #1
Nov 24th, 2008
Hey everybody this is my first post here and just learning PHP and need some help on an infinite loop problem. The code is supposed to find how many ways you can change a $1 dollar bill into coins using dollar coin, half dollars, quarters, dimes, nickels, and pennies. But i am having a problem because it is timimg out my browser and i asked my instructor and he said i have a infinite loop. I honestly dont have a clue what to look what when i comes to this.

Heres the code:
  1. <?php
  2.  
  3. //Setting all coins to 0
  4. $loopCounts = 0;
  5. $numDollars = 0;
  6. $numHalfs = 0;
  7. $numQuarters = 0;
  8. $numDimes = 0;
  9. $numNickels = 0;
  10. $numPennies = 0;
  11.  
  12. //What the max number of each coin can be to make a $1.00
  13. $maxDollars = 1;
  14. $maxHalfs = 2;
  15. $maxQuarters = 4;
  16. $maxDimes = 10;
  17. $maxNickels = 20;
  18. $maxPennies = 100;
  19.  
  20. //The value of what each coin is worth
  21. $valDollar = 100;
  22. $valHalf = 50;
  23. $valQuarter = 25;
  24. $valDime = 10;
  25. $valNickel = 5;
  26. $valPenny = 1;
  27.  
  28.  
  29. print("<table border=\"2\"> ");
  30. print("<tr><td>Number</td><td><strong>Dollars</strong></td><td>
  31. <strong>HalfDollars</strong></td><td><strong>Quarters</strong></td><td><strong>Dimes</strong></td><td><strong>Nickels</strong></td><td><strong>Pennies</strong></td></tr>");
  32.  
  33. //Beginning of the loops
  34. FOR ($numDollars =0; $numDollars <= $maxDollars; $numDollar++)
  35. {
  36. FOR ($numHalfs =0; $numHalfs <= $maxHalfs; $numHalfs++)
  37. {
  38. FOR ($numQuarters =0; $numQuarters <= $maxQuarters; $numQuarters++)
  39. {
  40. FOR ($numDimes =0; $numDimes <= $maxDimes; $numDimes++)
  41. {
  42. FOR ($numNickels =0; $numNickels <= $maxNickels; $numNickels++)
  43. {
  44. FOR ($numPennies =0; $numPennies <= $maxPennies; $numPennies++)
  45. {
  46. $loopCounts = $loopCounts + 1;
  47.  
  48. $coinSum = ($numDollars * $valDollar) + ($numHalfs * $valHalf) + ($numQuarters * $valQuarter) + ($numDimes * $valDime) + ($numNickels * $valNickel) + ($numPennies * $valPenny);
  49.  
  50. IF ($coinSum == 100)
  51. {
  52. $goodCombo = $goodCombo + 1;
  53. print("<tr><td>$goodCombo</td><td>$numDollars</td><td>$numHalfs</td><td>$numQuarters</td><td>$numDimes</td><td>$numNickels</td><td>$numPennies</td></tr>");
  54. }//end if
  55. }//end FOR loop for Pennies
  56. }//end FOR loop for Nickels
  57. }//end FOR loop for Dimes
  58. }//end FOR loop for Quarters
  59. }//end FOR loop for Half Dollars
  60. }//end FOR loop for Dollar Coin
  61.  
  62. print("<p>There are $loopCounts you can make change for One Dollar</p>");
  63. print("</table>");
  64.  
  65. ?>
It would be a big help if yall could help me look for it because I have know idea what to look for. Thank you for your time.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Infinite loop problem need help plz

 
0
  #2
Nov 24th, 2008
i had an idea that the way you positioned the for loops was making it repeat itself way too many times. so i reversed the setup and it worked. it was kind of a guess to be honest, but an educated one at least. loops like that can really mess with your head when trying to figure them out.

  1. <?php
  2.  
  3. //Setting all coins to 0
  4. $loopCounts = 0;
  5. $numDollars = 0;
  6. $numHalfs = 0;
  7. $numQuarters = 0;
  8. $numDimes = 0;
  9. $numNickels = 0;
  10. $numPennies = 0;
  11. //What the max number of each coin can be to make a $1.00
  12. $maxDollars = 1;
  13. $maxHalfs = 2;
  14. $maxQuarters = 4;
  15. $maxDimes = 10;
  16. $maxNickels = 20;
  17. $maxPennies = 100;
  18. //The value of what each coin is worth
  19. $valDollar = 100;
  20. $valHalf = 50;
  21. $valQuarter = 25;
  22. $valDime = 10;
  23. $valNickel = 5;
  24. $valPenny = 1;
  25.  
  26. print("<table border=\"2\"> ");
  27. print("<tr><td>Number</td><td><strong>Dollars</strong></td><td>
  28. <strong>HalfDollars</strong></td><td><strong>Quarters</strong></td><td><strong>Dimes</strong></td><td><strong>Nickels</strong></td><td><strong>Pennies</strong></td></tr>");
  29. for ( $numPennies = 0; $numPennies <= $maxPennies; $numPennies++ ) {
  30. for ( $numNickels = 0; $numNickels <= $maxNickels; $numNickels++ ) {
  31. for ( $numDimes = 0; $numDimes <= $maxDimes; $numDimes++ ) {
  32. for ( $numQuarters = 0; $numQuarters <= $maxQuarters; $numQuarters++ ) {
  33. for ( $numHalfs = 0; $numHalfs <= $maxHalfs; $numHalfs++ ) {
  34. for ( $numDollars = 0; $numDollars <= $maxDollars; $numDollars++ ) {
  35. $sum = ($numDollars * $valDollar) + ($numHalfs * $valHalf) + ($numQuarters * $valQuarter) + ($numDimes * $valDime) + ($numNickels * $valNickel) + ($numPennies * $valPenny);
  36. if ( $sum == 100 ) {
  37. $goodCombo = $goodCombo + 1;
  38. print("<tr><td>$goodCombo</td><td>$numDollars</td><td>$numHalfs</td><td>$numQuarters</td><td>$numDimes</td><td>$numNickels</td><td>$numPennies</td></tr>");
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }
  45. }
  46. print("<p>There are $goodCombo you can make change for One Dollar</p>");
  47. print("</table>");
  48.  
  49. ?>

also, there are 293 ways according to the script, in which was proven by a simple google search, so your code works.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 3
Reputation: Frosty[tnt] is an unknown quantity at this point 
Solved Threads: 0
Frosty[tnt] Frosty[tnt] is offline Offline
Newbie Poster

Re: Infinite loop problem need help plz

 
0
  #3
Nov 24th, 2008
Thanks bossman I really appreciate it!!! The code now works wonders and I also found from comparing code that my $numDollar++ was not $numDollars++, which is what it needed to be haha. But anyways thanks again for your time boss
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Infinite loop problem need help plz

 
0
  #4
Nov 24th, 2008
Nice catch there. I didn't see that. maybe if I examined the code some more i could of found that without rewriting the code. so it didn't have to reversed after all. That probably should of been the first thing i looked at.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 3
Reputation: Frosty[tnt] is an unknown quantity at this point 
Solved Threads: 0
Frosty[tnt] Frosty[tnt] is offline Offline
Newbie Poster

Re: Infinite loop problem need help plz

 
0
  #5
Nov 25th, 2008
Ahh you straight man, I still appreciate your help man. Well come here for all of my help in coding and yall are very qucik on your replies and are very nice. Thanks again man.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC