Challenge 2: Print a Matrix

Solve the following challenge to print a matrix, i.e., a two-dimensional array.

Problem Statement #

Print a two-dimensional array of size n x n as shown below in the form of a matrix.

Function Prototype #

function printMat($n)

Input #

an integer

Output #

print matrix on the console

Sample input #

$n = 3;

Sample output #

Print an array of size 3 x 3 as:

  • The diagonal of the array should be filled with 0.
  • The lower side should be filled with -1s.
  • upper side should be filled with 1s.

Make sure to put spaces between the values and print each row in the next line

0 1 1 \n-1 0 1 \n-1 -1 0 \n

Note: In order to move a value to the next line you can use PHP_EOL.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy