jabsalud Posted April 23, 2013 Share Posted April 23, 2013 helo i'm doing my logical exercises and i'm getting hard to solve this output using for loops 2 1 4 3 6 6 8 9 i need your help could you please post your codes? i need to understand nested loops. Link to comment Share on other sites More sharing options...
HartleySan Posted April 23, 2013 Share Posted April 23, 2013 You mean you need to output the following integers in the following order? 2 1 4 3 6 6 8 9 Could you please better explain what you want? Thank you. 1 Link to comment Share on other sites More sharing options...
Antonio Conte Posted April 23, 2013 Share Posted April 23, 2013 You need to find the correct mathematic formula. Simple as that. I'm not really sure you need nested loops for this, but math is in no way my strong suit. That's my feeling though. 1 Link to comment Share on other sites More sharing options...
jabsalud Posted April 23, 2013 Author Share Posted April 23, 2013 @Hartley San, yes that is exactly. could you please help me to code it? im doing it in for loops but with no success. thanks mate. Link to comment Share on other sites More sharing options...
margaux Posted April 23, 2013 Share Posted April 23, 2013 Its not entirely clear what you are trying to do - if its just outputting the integers you could set up an array and loop through it. $integers = array(2,1,4,3,6,6,8,9); foreach ($integers as $integer) { echo '<p>' . $integer . '</p>'; } 2 Link to comment Share on other sites More sharing options...
Antonio Conte Posted April 23, 2013 Share Posted April 23, 2013 I don't know if it that's simple, margaux, but maybe it is? Do you only need these numbers, or is there a pattern you want to find here? If it's simply the numbers, then this will suffice: $integers = array(2,1,4,3,6,6,8,9); $size = count($integers); for ($i = 0; $i < $size; $i++) { echo $integers[$i] . ' '; } 1 Link to comment Share on other sites More sharing options...
HartleySan Posted April 23, 2013 Share Posted April 23, 2013 I think margaux and Antonio have more or less hinted at the answer. I don't see any other way to do it without adding a ton of logic within the for loop. I searched Google for the number 21436689, but got nothing of interest. Also, I don't really see any pattern. The only patterns I see are the following: - The first and second numbers are reversed. - The same for the third and the fourth. - The fifth and sixth are both 6. - The seventh and eighth are one more than their position. That would require a bunch of logic in a for loop to get there though. Link to comment Share on other sites More sharing options...
jabsalud Posted April 24, 2013 Author Share Posted April 24, 2013 im sorry mates i gave a wrong output, let's change it to this 0+ -2 4+ -4 8+ -6 12+ -8 create a loop that will increment by two and another loop that will increment by four, use for loop. please help me with this example. thanks mates. Link to comment Share on other sites More sharing options...
HartleySan Posted April 24, 2013 Share Posted April 24, 2013 Why does this feel like we're doing your homework assignment for you? I'm not going to give you the answer, but if you look at the spread between values, the answer should become apparent. In other words, here's the spread between successive values: 0 2 6 8 12 14 18 20 Link to comment Share on other sites More sharing options...
jabsalud Posted April 24, 2013 Author Share Posted April 24, 2013 no its not an assignment. i have tried it but the output is this +2 0 4 8 12 +4 0 4 8 12 +6 0 4 8 12 +8 0 4 8 12 so what i am asking is i cant separate the loop that will increment by 4. what i wanted to do is to alternate them by two's as i have given the output. its not an assignment trust me. all im asking is for your help i need the actual code to compare to my answer. anyone of you could help me? its just an example and part of my studying the "nested loops". Link to comment Share on other sites More sharing options...
StephenM Posted April 24, 2013 Share Posted April 24, 2013 im sorry mates i gave a wrong output, let's change it to this 0+ -2 4+ -4 8+ -6 12+ -8 create a loop that will increment by two and another loop that will increment by four, use for loop. please help me with this example. thanks mates. The following worked for me: for ($i=0, $j=-2 ; $i<=12 && $j >= -9 ; $i+=4, $j-=2){ echo '+'.$i. ' '. $j. ' '; } Link to comment Share on other sites More sharing options...
StephenM Posted April 24, 2013 Share Posted April 24, 2013 Would it be appropriate to start a thread with coding challenges? [just a thought - perhaps separate threads depending on difficulty/experience ] Link to comment Share on other sites More sharing options...
HartleySan Posted April 24, 2013 Share Posted April 24, 2013 Sure, go ahead, StephenM. Sounds exciting. Also, jabsalud, sorry for making the wrong assumption. The way you phrased everything made it sound like it was a school assignment or something. That's why I was a bit apprehensive to answer before. Anyway, StephenM, if your solution works, great, but I think the following is much simpler: <?php for ($i = 0; $i < 8; $i++) { echo (($i % 2 === 0) ? $i * 2 : -$i - 1) . '<br>'; } Link to comment Share on other sites More sharing options...
StephenM Posted April 24, 2013 Share Posted April 24, 2013 Sure, go ahead, StephenM. Sounds exciting. Also, jabsalud, sorry for making the wrong assumption. The way you phrased everything made it sound like it was a school assignment or something. That's why I was a bit apprehensive to answer before. Anyway, StephenM, if your solution works, great, but I think the following is much simpler: <?php for ($i = 0; $i < 8; $i++) { echo (($i % 2 === 0) ? $i * 2 : -$i - 1) . '<br>'; } Yes, in fact no need for 2 variables looping but there are probably at least a dozen ways to do it. for ($i=0; $i >= -8; $i-= 2) { echo $i.'<br>'; if ((abs($i) > 0) && (abs($i) <8)) {echo '+'.(abs($i)*2).'<br>';} } Link to comment Share on other sites More sharing options...
StephenM Posted April 24, 2013 Share Posted April 24, 2013 Yes, in fact no need for 2 variables looping but there are probably at least a dozen ways to do it. for ($i=0; $i >= -8; $i-= 2) { echo $i.'<br>'; if ((abs($i) > 0) && (abs($i) <8)) {echo '+'.(abs($i)*2).'<br>';} } Sure, go ahead, StephenM. Sounds exciting. Although, I'm not sure where to find them. I'll have a look around --> only worth it if a bit of interest on the forum. Link to comment Share on other sites More sharing options...
jabsalud Posted April 24, 2013 Author Share Posted April 24, 2013 Sure, go ahead, StephenM. Sounds exciting. Also, jabsalud, sorry for making the wrong assumption. The way you phrased everything made it sound like it was a school assignment or something. That's why I was a bit apprehensive to answer before. Anyway, StephenM, if your solution works, great, but I think the following is much simpler: <?php for ($i = 0; $i < 8; $i++) { echo (($i % 2 === 0) ? $i * 2 : -$i - 1) . '<br>'; } thanks stephenM and hartleysan i really appreciate your works, your codes work perfect thanks again Link to comment Share on other sites More sharing options...
Recommended Posts