Page 1 of 1

find the maximum i & j

Posted: Fri Mar 18, 2011 1:28 am
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.

Re: find the maximum i & j

Posted: Fri Mar 18, 2011 10:07 am
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.