In the previous article, we have discussed Java Program to Find Scalar Multiplication of a Matrix
In this article we are going to see how we can write a program to find out the largest element in each row of the matrix in JAVA language.
Java Program to Find Largest Element in Each Row of 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 A
represents 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=Aij
wherei=0
andj=0
,A01=aij
wherei=0
andj=1
and like this. - Here we have started
row
value from 0 andcolumn
value from 0.
Let’s see different ways to find Largest Element in Each row of a Matrix.
Method-1: Java Program to Find Largest Element in Each Row of a Matrix By Static Initialization of Array Elements
Approach:
- Initialize and declare two arrays one of size 3×3 with elements and another 1D array of size 3.
- Use two for loops to iterate the rows and columns .
- Inside the for loops compare and store the largest among elements of the row in the 1D array.
- Print the result.
Program:
public class matrix{ public static void main(String args[]) { // Initializing the 3X3 matrix i.e. 2D array int arr[][] = {{19,25,32},{40,54,62},{70,20,60}}; //Array to hold largest element in each row of the matrix int temp[] = new int[3]; int row, col; System.out.print("The matrix elements are : "); printMatrix(arr); //Finding the largest element in each row for(row=0;row<3;row++) { temp[row]=arr[row][0]; for(col=1;col<3;col++) if(arr[row][col]>temp[row]) temp[row] = arr[row][col]; } for(int i = 0; i<3;i++) System.out.print("\nLargest element in "+(i+1)+"th row is "+temp[i]); } // 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]+" "); } } System.out.print("\n"); } }
Output: The matrix elements are : 19 25 32 40 54 62 70 20 60 Largest element in 1th row is 32 Largest element in 2th row is 62 Largest element in 3th row is 70
Method-2: Java Program to Find Largest Element in Each Row of a Matrix By Dynamic Initialization of Array Elements
Approach:
- Initialize and declare two arrays one of size 3×3 and another 1D array of size 3.
- Ask the user for input and store them in the one array using two for loops.
- Use two for loops to iterate the rows and columns .
- Inside the for loops compare and store the largest among elements of the row in the 1D array.
- 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]; //Array to hold largest element in each row of the matrix int temp[] = new int[3]; int row, col ,n; // Taking matrix input System.out.println("Enter matrix elements : "); 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); //Finding the largest element in each row for(row=0;row<3;row++) { temp[row]=arr[row][0]; for(col=1;col<3;col++) if(arr[row][col]>temp[row]) temp[row] = arr[row][col]; } for(int i = 0; i<3;i++) System.out.print("\nLargest element in "+(i+1)+"th row is "+temp[i]); } // 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]+" "); } } System.out.print("\n"); } }
Output: Enter matrix elements : The matrix elements are : 91 72 36 44 29 87 67 56 17 Largest element in 1th row is 91 Largest element in 2th row is 87 Largest element in 3th row is 67
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output
Related Java Programs: