In the previous article, we have seen Java Program to Find the Row having Maximum 0’s in a Binary Matrix
In this article we are going to see how we can count the sorted rows of a matrix in JAVA language.
Java Program to Count all the Sorted Rows in a Matrix
A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.
Let’s understand it in more simpler way.
| A00 A01 A02 |
Matrix A = | A10 A11 A12 |
| A20 A21 A22 | 3*3
Matrix Arepresents a 3*3 matrix.- ‘
A‘ represents the matrix element - ‘
Aij‘ represents the matrix element at it’s matrix position/index. - ‘
i‘ represents the row index - ‘
j‘ represents the column index - Means
A00=Aijwherei=0andj=0,A01=aijwherei=0andj=1and like this. - Here we have started
rowvalue from 0 andcolumnvalue from 0.
Let’s see different ways to count all the sorted rows in a matrix.
Method-1: Java Program to Count all the Sorted Rows in a Matrix By Static Initialization of Array Elements
Approach:
- Initialize an array of size 3×3 with values.
- Show the array to the user.
- Use two for loops to iterate the row and column index.
- Check if the rows are in increasing order, if yes then increment the result variable.
- Check if the rows are in decreasing order, if yes then increment the result variable.
- Print the result.
Program:
import java.util.Scanner;
public class Matrix
{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);
// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = {{1,2,3},{6,5,4},{7,9,8}};
int row, col ;
System.out.print("The matrix elements are : ");
printMatrix(arr);
System.out.println("\nThe number of sorted rows in the matrix are : "+sortedRows(arr));
}
// Method to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
}
//sortedRows() method to find the number rows in sorted order
static int sortedRows(int arr[][])
{
// Variable to store result
int res = 0,row,col;
// Starts from left side of the matrix
for(row=0;row<3;row++)
{
for(col=0;col<2;col++)
// Checks if the elements are not in increasing order
if(arr[row][col+1]<=arr[row][col])
break;
// If the row is sorted increments the result
if(col==2)
res++;
}
// Starts from right side of the matrix
for(row=0;row<3;row++)
{
for(col=2;col>0;col--)
// Checks if the elements are not in decreasing order
if(arr[row][col-1]<=arr[row][col])
break;
if(col==0)
res++;
}
return res;
}
}
Output: The matrix elements are : 1 2 3 6 5 4 7 9 8 The number of sorted rows in the matrix are : 2
Method-2: Java Program to Count all the Sorted Rows in a Matrix By Dynamic Initialization of Array Elements
Approach:
- Initialize an array of size 3×3.
- Ask the user for input.
- Use two for loops to iterate the rows and columns to input the array elements.
- Show the array to the user.
- Use two for loops to iterate the row and column index
- Check if the rows are in increasing order, if yes then increment the result variable.
- Check if the rows are in decreasing order, if yes then increment the result variable.
- Print the result.
Program:
import java.util.Scanner;
public class matrix
{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);
// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = new int[3][3];
int row, col ;
System.out.print("Enter the 3x3 matrix elements :");
// Loop to take user input
for(row=0;row<3;row++)
for(col=0;col<3;col++)
arr[row][col] = scan.nextInt();
System.out.print("The matrix elements are:");
printMatrix(arr);
System.out.println("\nThe number of sorted rows in the matrix are: "+sortedRows(arr));
}
// Function to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
}
static int sortedRows(int arr[][])
{
// Variable to store result
int res = 0,row,col;
// Starts from left side of the matrix
for(row=0;row<3;row++)
{
for(col=0;col<2;col++)
// Checks if the elements are not in increasing order
if(arr[row][col+1]<=arr[row][col])
break;
// If the row is sorted increments the result
if(col==2)
res++;
}
// Starts from right side of the matrix
for(row=0;row<3;row++)
{
for(col=2;col>0;col--)
// Checks if the elements are not in decreasing order
if(arr[row][col-1]<=arr[row][col])
break;
if(col==0)
res++;
}
return res;
}
}
Output: Enter the 3x3 matrix elements :The matrix elements are: 1 2 3 4 5 6 9 8 7 1 2 3 4 5 6 9 8 7 The number of sorted rows in the matrix are: 3
Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.
Related Java Programs: