Challenge 3: Print a Matrix

In this challenge, you will figure out how to print a Matrix using a two-dimensional Array.

Problem statement

A two-dimensional array can also be thought of as a matrix.

Implement a Java method to initialize & print a two-dimensional array of size n x n in the form of a matrix. You should initialize and 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 will -1s.
  • upper side should be filled with 1s.

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

Function prototype

void printMat(int n)

Sample input

int n = 3;

Sample output pictorial representation

Get hands-on with 1400+ tech skills courses.