In the previous article, we have seen Java Program to Find Product of Sum of First Column and Last Column
In this article we are going to see how we can write a program to calculate the average of matrix elements in JAVA language.
Java Program to Find Average of Matrix Elements
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 average of matrix elements.
Method-1: Java Program to Find Average of Matrix Elements By Static Initialization of Array Elements
Approach:
- Initialize and declare an array of size 3×3 with elements.
- Use two for loops to iterate the rows and columns and then add each element to the sum variable.
- Then find the average by dividing the sum by 9(As in a 3*3 matrix there are total 9 elements).
- Print the average.
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[][] = {{10,20,30},{40,50,60},{70,80,90}}; int row, col, sum = 0; System.out.print("The matrix elements are : "); // 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]+" "); } } //finding the sum of elements for(row=0;row<3;row++) for(col=0;col<3;col++) sum+=arr[row][col]; System.out.print("\nThe sum of all the elements of matrix is : "+sum); //Finding the average of all elements of matrix //There are total 9 elements in a 3*3 matrix //so we are dividing the sum by 9 int avg=sum/9; System.out.print("\nThe average of all the elements of matrix is : "+avg); } }
Output: The matrix elements are : 10 20 30 40 50 60 70 80 90 The sum of all the matrix elements is : 450 The average of all the matrix elements is : 50
Method-2: Java Program to Find Average of Matrix Elements 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 and input the array elements.
- Similarly use two for loops to add each element and store in sum variable.
- Then find the average by dividing the sum by 9(As in a 3*3 matrix there are total 9 elements).
- Print the average.
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], sum = 0; System.out.println("Enter the 3x3 matrix elements : "); int row, col; // 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 : "); // 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]+" "); } } //finding the sum of elements for(row=0;row<3;row++) for(col=0;col<3;col++) sum+=arr[row][col]; System.out.print("\nThe sum of all the elements of matrix is : "+sum); //Finding the average of all elements of matrix //There are total 9 elements in a 3*3 matrix //so we are dividing the sum by 9 int avg=sum/9; System.out.print("\nThe average of all the elements of matrix is : "+avg); } }
Output: Enter the 3x3 matrix elements : The matrix elements are : 9 5 1 7 4 8 2 6 3 9 5 1 7 4 8 2 6 3 The sum of all the elements of matrix is : 45 The average of all the elements of matrix is : 5
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Related Java Programs: