find the maximum i & j

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hkvega01
Forum Newbie
Posts: 1
Joined: Fri Mar 18, 2011 1:23 am

find the maximum i & j

Post by hkvega01 »

pickle | Please use [ syntax=php ], [ syntax=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


find the maximum i & j, but not exceed amount , any ideas?

Code: Select all

$amount = 20000;   
$max_area = 1000;    

for ($i=0; $i<=100; $i+=0.1) {    
  
        for ($j=0; $j<=100; $j+=0.1) {     
          
                $area = $i*$j;       
                $cost = $i*1200 + ($i+$j*2) * 2500;  
                
                if($cost > $amount) {       
                        break;       
                }       
        }   
}

pickle | Please use [ syntax=php ], [ syntax=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: find the maximum i & j

Post by pickle »

I'm assuming you want to break out of both your for loops if $cost > $amount. If so, you need: break 2;. As it is, you will only break out of the inner for() loop, but your outer for() loop continues (which then restarts your inner for() loop).

If you change that to break 2;, then both $i & $j will be at their maximum values right before that break.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply