Java program to print matrix in Z form
In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form.
What is a Matrix in Java?
Technically referring, a matrix is a rectangular, two-dimensional array that contains either integers, symbols, or statements as its fundamental elements. The elements in the matrix are arranged in rows and columns.
For an instance, m x n matrix refers to a matrix containing 'm' number of rows and 'n' number of columns. 'a ij', which denotes that the element an is represented in the ith row and jth column, can be used to represent individual values in the matrix, which are referred to as elements.
Look at the example below to understand how a matrix actually looks like
Example:
a[][]= { 7, 8, 9,
4, 5, 6,
1, 2, 3}
The above example is a matrix or it can also be termed as two-dimensional array, with 3 rows and 3 columns. The representation of the above matrix elements in Z form shall certainly include the entire first row of the matrix, the right diagonal and the entire last row of the matrix too.
Now let us discuss how to print the elements of a matrix in Z from.
The very first row, right diagonal, and then last row of the matrix are all represented in the Z form. The columns and the rows must match for the matrix to be presented in its Z form.
We must print the matrix's components in Z form given a two - dimensional array of order n*n.
Input:
{ [1, 2, 3,]
[4, 5, 6]
[7, 8, 9]}
Output:
{ [ 3]
[ 5 ]
[7 ] }
Input:
{[1, 2, 3, 4,]
[5, 6, 7, 8,]
[9, 8, 7, 6,]
[5, 4, 3, 2,] }
Output:
{[ 4,]
[ 7, ]
[ 8, ]
[5, ] }
The above is how the matrix can be represented in Z form.
Now, let us look at the flow of program that prints the elements of a given matrix in Z form.
File name: Zform1.java
import java.util.Scanner;
public class Zform1
{
//displaying the matrix
static void printMatrix(int arr[][])
{
int row, col;
for(row=0; row<3;row++)
{
System.out.print("\n");
for(col=0; col<3;col++)
{
//displaying the elements
System.out.print(arr[row][col]+"\t");
}
}
}
static void printZMatrix(int arr[][])
{
int row, col;
//displays the first row (first elements of all the columns) of matrix
for(col=0;col<3;col++)
System.out.print(arr[0][col]+"\t");
System.out.print("\n");
//displays the elements present in the right diagonal of the given matrix
//row begins with indexing 1 since we must not to display the starting and ending //element
for(row=1; row<2; row++)
for(col=0;col<3;col++)
//verifies if the sum of rows and columns is equal to 2
//If true it dislplays the first row and first column element
if(row+col==2)
System.out.print("\t"+arr[row][col]);
System.out.print("\n");
//displays the final row of matrix.
for(col=0;col<3;col++)
System.out.print(arr[2][col]+"\t");
}
public static void main(String args[])
{
//creating a matrix of 3*3
int arr[][] = {{8,7,6},{1,2,3},{6,9,1}};
System.out.print("The elements in the matrix are:");
printMatrix(arr);
System.out.println("\n The elements of matrix in Z-form: ");
//function calling
printZMatrix(arr);
}
}
Output
The elements in the matrix are:
8 7 6
1 2 3
6 9 1
The elements of matrix in Z-form are:
8 7 6
2
6 9 1
Let’s go through another example
File name: Zform2.java
// Java program to display the elements in matrix in Z form
public final class Zform2 {
public static int MAX = 100;
// Function to print a square matrix in Z form
public static void printZform(int[][] mat, int n)
{
int i;
// Displays all elements in first row except the last one
for (i = 0; i < n - 1; i++) {
System.out.print(mat[0][i]);
System.out.print(" ");
}
// Displays the right diagonal from the given matrix
for (i = 0; i < n - 1; i++) {
System.out.print(mat[i][n - i - 1]);
System.out.print(" ");
}
// Displays the last row of the matrix
for (i = 0; i < n; i++) {
System.out.print(mat[n - 1][i]);
System.out.print(" ");
}
}
public static void main(String[] args)
{
int[][] m = { { 11,12,13,14},
{ 23,24,25,26},
{ 33,34,35,36},
{ 47,48,46,49} };
printZform(m, 4);
}
}
Output:
11 12 13 14 25 34 47 48 46 49