Print a pattern to display numbers up to 5 rows and 5 columns like 5 4 3 2 1 and on the next line 4 3 2 1 5 to the 5th row

It is a square pattern with each row having 5 columns and there are 5 rows and the pattern looks like below:

      5 4 3 2 1
      4 3 2 1 5
      3 2 1 5 4
      2 1 5 4 3
      1 5 4 3 2

      

My code looks like this to get the template, but I cannot reset the value in each one when the counter comes to 1 and shows up in the corresponding column value.

Can anyone guide me where I am lacking in logic?

<?php
  $n=5;
  $count=5;
  for($i=5;$i>=1;$i--){ 
     for($j=$count;$j>=1;$j--)
     {
        if($count>=1)
           echo $j."&nbsp;";        
     }
     $count--;
     echo "\n";
  }
?>

      

+3


source to share


12 replies


I think this is a learning exercise, so it needs to be done with loops.

<?php
$n=5;
for($i=$n; $i>=1; $i--) { 
    for($j=$n; $j>=1; $j--) {
        echo ($i + $j - 1) % $n + 1; 
        echo ' '; // cosmetics :)       
    }
    echo "\n";
}
?>

      

% is a modular operator. 6% 5 = remainder 6 divided by 5 = 1.

Examples: 1% 5 = 1; 5% 5 = 0; 12% 5 = 2.




This is a simple choice from modular arithmetic ( http://en.wikipedia.org/wiki/Modular_arithmetic ). Each new line is nothing more than a simple add operation, but in a 5-mod number set (for each line, starting with the lowest number). And I think this is the best and one line solution to this problem. Moreover, the operation is simple and not time consuming.

Respectfully!

+5


source


You can try this:

$str = "5 4 3 2 1";
$arr_str = explode(' ', $str);
$imax_loop = count( $arr_str);
for($i = 0; $i < $imax_loop; $i++)
{
    echo implode(" ",$arr_str) . "\n";

    // Get the first element and pop it off
    $head_elm = array_shift($arr_str);
    // append the first element at the end of the array
    array_push( $arr_str, $head_elm);
}

      



Output

5 4 3 2 1
4 3 2 1 5
3 2 1 5 4
2 1 5 4 3
1 5 4 3 2

      

+3


source


+2


source


If we change your set from:

  5 4 3 2 1
  4 3 2 1 5
  3 2 1 5 4
  2 1 5 4 3
  1 5 4 3 2

      

to

  4 3 2 1 0
  3 2 1 0 4
  2 1 0 4 3
  1 0 4 3 2
  0 4 3 2 1

      

it looks much bigger (some MOD number 5) ... so your loop might just print (N% 5) +1

so what is N?

instead of looking at i and j as decreasing. perhaps we can look at them as increasing and subtracting from some number.

Let's say i goes from 0-4 and j goes from 0-4. (don't forget to take a look at the second block, since after the MODULUS operation we will be doing +1.

Suppose we want to add i and j together and subtract them from some number, initially 4 might look good since 4- (0 + 0) gives us the right first cell, but once we get to the end in the second row we will do 4- (1 + 4) and negative negative numbers cause modulus problems. (Mathematics and programming languages ​​often differ in their interpretations.

9 mod 5 is also 4 and we will never subtract more than 8 (when i and j are both 4), so 9 seems like a better bet.

PSEUDOCODE

for i from 0 to 4
  for j from 0 to 4
    print ( ( 9-(i+j) ) % 5 ) + 1
    if j<4 print " "
  loop
  print newline
loop

      

To make the loop flexible for sizes other than 5:

PSEUDOCODE

for i from 0 to SIZE-1
  for j from 0 to SIZE-1
    print ( ( ( (2*SIZE)-1) - (i+j) ) % SIZE ) + 1
    if j<SIZE-1 print " "
  loop
  print newline
loop

      

+2


source


The question is actually very simple, and the OP had most of the correctness. The conditions in its inner loop just needed to be reworked and it took an if condition to add "5" to the output to prevent overflow if numbers slipped into a negative range

<?php
  $n=5;
  $count=0;

  for($i=5; $i>=1; $i--){

     // You still want to loop through here 5 times, because you need to print out 5 numbers
     for($j=5; $j>=1; $j--)
     {
        // The fun part is augmenting your output so the numbers don't ever slip below 0 or negative.
        // This is already partially achieved in the fact that the range of your values is 5.
        // So if the value slips below the desired threshold, incrementing it by your range will bring it back into desired range
        $out = $j - $count;
        if ($out <= 0) {
          $out += 5;
        }
        echo $out."&nbsp;";
     }
     $count++;

     echo "\n";
  }
?>

      

+1


source


$ str = '54321';

echo $ str. "\ n";

for ($ = 1; $ i & ; = 4; $ i ++) {

$a=$str[0];


$var = substr($str,1);


echo $str=$var.$a;


echo "\n";

      

}

and if you don't want to use substr function try this

  $str = '54321';
  $len = strlen($str);

  $s= '';
  for ($i=0;$i<$len;$i++){  
          print_r($str);
          print_r($s);
          $s = $s.$str[$i];
          $str[$i] = '';  
          echo '<br>';
  }

      

+1


source


Changed the code a bit:

<?php
  $limit=5;
  $count=5;
  for($i=5;$i>=1;$i--){ 
     for($j=$limit;$j>=1;$j--)
     {
         $count = ($count>=1)?$count:5;  
         echo $count--."&nbsp;";        
     }
      if($count<1)
        $count=$i-1;
      else
          $count--;
     echo "<br>";
  }
?>

      

DEMO

Output:

5 4 3 2 1 
4 3 2 1 5 
3 2 1 5 4 
2 1 5 4 3 
1 5 4 3 2 

      

0


source


I will try to explain where you can improve your logic for the above code. You want 5 rows and 5 columns of values ​​ie 25 values ​​in this template. Your outer loop for

is executed 5 times and counts 5 lines that work.

However, the inner loop of for

ie:

$count = 5; //initially and gets reduced by 1 on every iteration
for($j=$count;$j>=1;$j--)
 {
    if($count>=1)
       echo $j."&nbsp;";

 }

      

The above code should produce the following output since you end up as $j

hits 1:

   5 4 3 2 1
   4 3 2 1
   3 2 1 
   2 1 
   1

      

I understand why you are getting this output. This is because you don't care about the second half of the issue. So, if you want to expand your logic, think of the output as two parts (for each line):

  • List of numbers starting from 5, i.e. changed variable $counter

    but ending in 1.
  • Another list of numbers that always start with 5 that would account for the other half and therefore get the expected result:

      //1st half    //2nd half
      5 4 3 2 1
      4 3 2 1       5
      3 2 1         5 4
      2 1           5 4 3
      1             5 4 3 2   
    
          

So, just add another loop for

/ while

after your existing loop should take care of it. You might want to try something like this:

 $n=5;


 for($i=5;$i>=1;$i--){
   $count=5 - $i;
   $k = 5;

   //Takes care of the firs half
    for($j=$i;$j>=1;$j--) //because $i gets decreased every time any way
    {
       if($i>=1)
       echo $j."&nbsp;";

    }

   //Takes care of the second half
     while($count > 0){
       echo $k."&nbsp;";
       $k--;
     }
  echo "\n";
  }

      

0


source


Step one: appreciate that PHP doesn't have a built-in array_rotate that can run at the speed of the C language.

Step two: use the version of array_rotate from the comments on the PHP site, which will work as a last resort. (This version is probably terrible because of array_shift if used on large arrays, but meh in our case)

function array_rotate(&$arr) {
  $elm = array_shift($arr);
  array_push($arr, $elm);
  return $elm; // return the moved element value
}

      

Step three:

$a = [ 5, 4, 3, 2, 1 ];
for ($i=0; $i<5; $i++) {
  echo join(' ',$a) . PHP_EOL;
  array_rotate($a);
}

      

Step four:

Step five: profit!

0


source


try this

$n=5;
$arr = array();
for($j=$n;$j >0;$j--)
{
for($k = $n;$k>0;$k--)
{
    echo $k. " ";
}
echo implode(" ",$arr);
array_push($arr,$n);
echo '<br/>';
$n--;   
}

      

0


source


$n=5;
for($i=$n;$i>=1;$i--)
{
  for($j=$i;$j>=1;$j--)
  {
    echo "$j";
  }
  if($i<$n)
  {
    for($k=$n;$k>$i;$k--)
    {
      echo "$k";
    }
  }
  echo"<br>";
}

      

0


source


for ($i=0;$i<=33;$i++){
    echo"<br>".$i;
    for ($j = 0; $j < $i; $j++) {
        echo $j;
    }    
    }

      

-1


source







All Articles