Challenge 2: Print a Matrix
Practice how to print a matrix, i.e., a two-dimensional array.
We'll cover the following
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.
Sample Output Array
Coding challenge
Try to implement the solution by yourself once you have understood the problem statement clearly. Referring to the solution part should be your last resort.
Good luck!
sub printMat {#write the code for making and printing the matrix here#use can use \n to move numers to next line in the matrix#use " " to add space between numbers in matrixprint "Write your code below"; #comment out this line when you start writing cod}