Challenge 2: Print a Matrix

Practice how 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.

Subroutine prototype

sub printMat($n)

Input

An integer.

Output

Print matrix on the console.

Sample input

$n = 4;

Sample output

Print an array of size 4 x 4 as:

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

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

Note: In order to move a value to the next line you can use "\n" and " " for space.

Create a free account to access the full course.

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