| | |
Infinite loop problem need help plz
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 3
Reputation:
Solved Threads: 0
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:
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.
Heres the code:
PHP Syntax (Toggle Plain Text)
<?php //Setting all coins to 0 $loopCounts = 0; $numDollars = 0; $numHalfs = 0; $numQuarters = 0; $numDimes = 0; $numNickels = 0; $numPennies = 0; //What the max number of each coin can be to make a $1.00 $maxDollars = 1; $maxHalfs = 2; $maxQuarters = 4; $maxDimes = 10; $maxNickels = 20; $maxPennies = 100; //The value of what each coin is worth $valDollar = 100; $valHalf = 50; $valQuarter = 25; $valDime = 10; $valNickel = 5; $valPenny = 1; print("<table border=\"2\"> "); print("<tr><td>Number</td><td><strong>Dollars</strong></td><td> <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>"); //Beginning of the loops FOR ($numDollars =0; $numDollars <= $maxDollars; $numDollar++) { FOR ($numHalfs =0; $numHalfs <= $maxHalfs; $numHalfs++) { FOR ($numQuarters =0; $numQuarters <= $maxQuarters; $numQuarters++) { FOR ($numDimes =0; $numDimes <= $maxDimes; $numDimes++) { FOR ($numNickels =0; $numNickels <= $maxNickels; $numNickels++) { FOR ($numPennies =0; $numPennies <= $maxPennies; $numPennies++) { $loopCounts = $loopCounts + 1; $coinSum = ($numDollars * $valDollar) + ($numHalfs * $valHalf) + ($numQuarters * $valQuarter) + ($numDimes * $valDime) + ($numNickels * $valNickel) + ($numPennies * $valPenny); IF ($coinSum == 100) { $goodCombo = $goodCombo + 1; print("<tr><td>$goodCombo</td><td>$numDollars</td><td>$numHalfs</td><td>$numQuarters</td><td>$numDimes</td><td>$numNickels</td><td>$numPennies</td></tr>"); }//end if }//end FOR loop for Pennies }//end FOR loop for Nickels }//end FOR loop for Dimes }//end FOR loop for Quarters }//end FOR loop for Half Dollars }//end FOR loop for Dollar Coin print("<p>There are $loopCounts you can make change for One Dollar</p>"); print("</table>"); ?>
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.
also, there are 293 ways according to the script, in which was proven by a simple google search, so your code works.
PHP Syntax (Toggle Plain Text)
<?php //Setting all coins to 0 $loopCounts = 0; $numDollars = 0; $numHalfs = 0; $numQuarters = 0; $numDimes = 0; $numNickels = 0; $numPennies = 0; //What the max number of each coin can be to make a $1.00 $maxDollars = 1; $maxHalfs = 2; $maxQuarters = 4; $maxDimes = 10; $maxNickels = 20; $maxPennies = 100; //The value of what each coin is worth $valDollar = 100; $valHalf = 50; $valQuarter = 25; $valDime = 10; $valNickel = 5; $valPenny = 1; print("<table border=\"2\"> "); print("<tr><td>Number</td><td><strong>Dollars</strong></td><td> <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>"); for ( $numPennies = 0; $numPennies <= $maxPennies; $numPennies++ ) { for ( $numNickels = 0; $numNickels <= $maxNickels; $numNickels++ ) { for ( $numDimes = 0; $numDimes <= $maxDimes; $numDimes++ ) { for ( $numQuarters = 0; $numQuarters <= $maxQuarters; $numQuarters++ ) { for ( $numHalfs = 0; $numHalfs <= $maxHalfs; $numHalfs++ ) { for ( $numDollars = 0; $numDollars <= $maxDollars; $numDollars++ ) { $sum = ($numDollars * $valDollar) + ($numHalfs * $valHalf) + ($numQuarters * $valQuarter) + ($numDimes * $valDime) + ($numNickels * $valNickel) + ($numPennies * $valPenny); if ( $sum == 100 ) { $goodCombo = $goodCombo + 1; print("<tr><td>$goodCombo</td><td>$numDollars</td><td>$numHalfs</td><td>$numQuarters</td><td>$numDimes</td><td>$numNickels</td><td>$numPennies</td></tr>"); } } } } } } } print("<p>There are $goodCombo you can make change for One Dollar</p>"); print("</table>"); ?>
also, there are 293 ways according to the script, in which was proven by a simple google search, so your code works.
![]() |
Similar Threads
- Someone Plz Help!! (C++)
Other Threads in the PHP Forum
- Previous Thread: User based look & feel customization of site
- Next Thread: How can i have attractive Menu Bar for php
| Thread Tools | Search this Thread |
5.2.10 action apache api array beginner binary broken cakephp checkbox class classes cms code cron curl database date destroy display dynamic echo echo$_get[x]changingitintovariable... email encode error fcc file files folder form forms function functions google header howtowriteathesis href htaccess html if-else image images include insert ip javascript joomla limit link local login mail memberships menu mlm mod_rewrite multiple multipletables mysql mysqlquery neutrality oop open passwords paypal pdf php provider query radio random record remote rss script search server sessions sockets source space sql strip_tags syntax system table template thesishelp tutorial update upload url validator variable video voteup web window.onbeforeunload=closeme; youtube






