php - Creating an N x N array of integers that "staircase" and "wrap around"

Php - Creating an N x N array of integers that "staircase" and "wrap around"

To create an N x N array of integers that forms a "staircase" pattern and "wraps around" in PHP, you can use nested loops to populate the array accordingly. Here's how you can achieve this:

function createStaircaseArray($size) { $array = array(); $counter = 1; for ($i = 0; $i < $size; $i++) { $array[$i] = array(); for ($j = 0; $j < $size; $j++) { if ($j <= $i) { $array[$i][$j] = $counter++; } else { $array[$i][$j] = $counter - $size + $j + 1; } } } return $array; } // Example usage $size = 5; // Change this to adjust the size of the array $array = createStaircaseArray($size); // Display the array foreach ($array as $row) { echo implode("\t", $row) . "\n"; } 

This function createStaircaseArray generates an N x N array where each element follows a "staircase" pattern. The pattern starts with 1 and increases by 1 for each subsequent element. When the end of a row is reached, the pattern "wraps around" to the beginning of the next row.

For example, if $size is set to 5, the output would be:

1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16 21 22 23 24 25 

Adjust the value of $size to generate arrays of different sizes.

Examples

  1. PHP code to generate an N x N staircase array with wrapping:

    • Description: This PHP code generates an N x N array of integers where the values form a staircase pattern that wraps around the array edges.
    <?php function generateStaircaseArray($n) { $array = []; for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { $array[$i][$j] = ($i + $j) % $n + 1; } } return $array; } $n = 5; // Change the value of $n to adjust the array size $staircaseArray = generateStaircaseArray($n); print_r($staircaseArray); ?> 
  2. PHP code to print an N x N staircase array:

    • Description: This PHP code prints out the N x N staircase array generated by the function in a visually readable format.
    <?php function printStaircaseArray($array) { foreach ($array as $row) { foreach ($row as $value) { echo $value . " "; } echo "\n"; } } $n = 5; // Change the value of $n to adjust the array size $staircaseArray = generateStaircaseArray($n); // Assuming generateStaircaseArray function is defined elsewhere printStaircaseArray($staircaseArray); ?> 
  3. PHP code to create a wrapped staircase array visualization:

    • Description: This PHP code visualizes the generated N x N staircase array with wrapping, showing how the staircase pattern wraps around the edges of the array.
    <?php function visualizeStaircaseArray($array) { foreach ($array as $row) { foreach ($row as $value) { echo str_pad($value, 4, " ", STR_PAD_LEFT); } echo "\n"; } } $n = 5; // Change the value of $n to adjust the array size $staircaseArray = generateStaircaseArray($n); // Assuming generateStaircaseArray function is defined elsewhere visualizeStaircaseArray($staircaseArray); ?> 
  4. PHP code to generate an N x N staircase array without wrapping:

    • Description: This PHP code generates an N x N array of integers where the values form a staircase pattern without wrapping around the array edges.
    <?php function generateNonWrappingStaircaseArray($n) { $array = []; for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { $array[$i][$j] = ($i + $j < $n) ? $i + $j + 1 : ($i + $j) % $n + 1; } } return $array; } $n = 5; // Change the value of $n to adjust the array size $staircaseArray = generateNonWrappingStaircaseArray($n); print_r($staircaseArray); ?> 
  5. PHP code to print a non-wrapping N x N staircase array:

    • Description: This PHP code prints out the N x N non-wrapping staircase array generated by the function in a visually readable format.
    <?php function printNonWrappingStaircaseArray($array) { foreach ($array as $row) { foreach ($row as $value) { echo $value . " "; } echo "\n"; } } $n = 5; // Change the value of $n to adjust the array size $staircaseArray = generateNonWrappingStaircaseArray($n); // Assuming generateNonWrappingStaircaseArray function is defined elsewhere printNonWrappingStaircaseArray($staircaseArray); ?> 
  6. PHP code to create a non-wrapped staircase array visualization:

    • Description: This PHP code visualizes the generated N x N non-wrapping staircase array, displaying the staircase pattern without wrapping around the edges.
    <?php function visualizeNonWrappingStaircaseArray($array) { foreach ($array as $row) { foreach ($row as $value) { echo str_pad($value, 4, " ", STR_PAD_LEFT); } echo "\n"; } } $n = 5; // Change the value of $n to adjust the array size $staircaseArray = generateNonWrappingStaircaseArray($n); // Assuming generateNonWrappingStaircaseArray function is defined elsewhere visualizeNonWrappingStaircaseArray($staircaseArray); ?> 
  7. PHP code to generate an N x N staircase array with descending pattern:

    • Description: This PHP code generates an N x N array of integers where the values form a descending staircase pattern that wraps around the array edges.
    <?php function generateDescendingStaircaseArray($n) { $array = []; for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { $array[$i][$j] = ($i + $j < $n) ? $n - ($i + $j) : $n - (($i + $j) % $n); } } return $array; } $n = 5; // Change the value of $n to adjust the array size $staircaseArray = generateDescendingStaircaseArray($n); print_r($staircaseArray); ?> 
  8. PHP code to print a descending N x N staircase array:

    • Description: This PHP code prints out the N x N descending staircase array generated by the function in a visually readable format.
    <?php function printDescendingStaircaseArray($array) { foreach ($array as $row) { foreach ($row as $value) { echo $value . " "; } echo "\n"; } } $n = 5; // Change the value of $n to adjust the array size $staircaseArray = generateDescendingStaircaseArray($n); // Assuming generateDescendingStaircaseArray function is defined elsewhere printDescendingStaircaseArray($staircaseArray); ?> 

More Tags

mysql-8.0 summary next-images angular-material-7 datatrigger nexus3 sonarqube-scan runtime.exec mysql-error-1071 email-attachments

More Programming Questions

More General chemistry Calculators

More Fitness Calculators

More Chemical thermodynamics Calculators

More Entertainment Anecdotes Calculators