w
a m in Java to store the numbers in a 4*4 matrix in a
D D
Amay. Find the sum of the numbers of each row
mothe numbers of cach column of the matrix by using an
postament
Sample Output
The numbers of the matrix are
51 31 30 29
$9. 41 76
The son of the elements of each row
The of the elements of Ist row60
The sum of the elements of 2nd row = 149
The sum of the elements of 3rd row 131
The sum of the elements of 4th row 258
The sum of the elements of each column
The sum of the elements of Ist column = 156
The sum of the woments of 2nd column 155
The sum of the elements of 3rd column - 135
The
clements of 4th column = 152
Answers
Summation within Matrix - Java
We need a Java program to store a 4x4 matrix and then display the sum of elements in each row as well as each column.
We will first start with user input of all the elements. This requires two loops, one running inside the other. We take the input using Scanner.
After the input, we will display the matrix once.
Then, we run loops again to find sum of elements in each row and each column. We store these sums in two arrays. And then we print them out.
Program Code - MatrixElementsSum.java
import java.util.Scanner; //Importing Scanner
public class MatrixElementsSum
{
public static void main(String[] args)
{
//Create Scanner Object
Scanner sc = new Scanner(System.in);
System.out.println("---Initiating Input of the Elements of the 4x4 Matrix---");
//Initialise a 4x4 int type matrix
int mat[][] = new int[4][4];
//Run loops to take elements input
for(int i = 0; i < 4; i++) //Rows
{
for(int j = 0; j < 4; j++) //Columns
{
System.out.print("Enter element of Row ["+(i+1)+"], Column ["+(j+1)+"]: ");
mat[i][j] = sc.nextInt(); //Taking User Input
}
System.out.println(); //Printing a blank line after each row
}
//Display the Matrix to the screen
System.out.println("\nHere is the Matrix: ");
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
//Print the elements with a tab spacing
System.out.print(mat[i][j]+"\t");
}
System.out.println(); //Printing a new line after each row
}
//Initialise two arrays of length 4
//These will contain the sum of elements in rows and columns
int rowSum[] = new int[4];
int columnSum[] = new int[4];
//Initiate Loops to calculate Sum
for(int i = 0; i < 4; i++)
{
rowSum[i] = 0;
columnSum[i] = 0;
for(int j = 0; j < 4; j++)
{
rowSum[i] += mat[i][j]; //Extract row i, column j
columnSum[i] += mat[j][i]; //Extract row j, column i
}
}
//Print out the sum of elements in each row
System.out.println("\n---The Sum of Rows---");
for(int i = 0; i < 4; i++)
{
System.out.println("Sum of elements in Row ["+(i+1)+"] = "+rowSum[i]);
}
//Print out the sum of elements in each column
System.out.println("\n---The Sum of Columns---");
for(int i = 0; i < 4; i++)
{
System.out.println("Sum of elements in Column ["+(i+1)+"] = "+columnSum[i]);
}
}
}
Output
---Initiating Input of the Elements of the 4x4 Matrix---
Enter element of Row [1], Column [1]: 1
Enter element of Row [1], Column [2]: 2
Enter element of Row [1], Column [3]: 3
Enter element of Row [1], Column [4]: 4
Enter element of Row [2], Column [1]: 5
Enter element of Row [2], Column [2]: 6
Enter element of Row [2], Column [3]: 7
Enter element of Row [2], Column [4]: 8
Enter element of Row [3], Column [1]: 9
Enter element of Row [3], Column [2]: 10
Enter element of Row [3], Column [3]: 11
Enter element of Row [3], Column [4]: 12
Enter element of Row [4], Column [1]: 13
Enter element of Row [4], Column [2]: 14
Enter element of Row [4], Column [3]: 15
Enter element of Row [4], Column [4]: 16
Here is the Matrix:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
---The Sum of Rows---
Sum of elements in Row [1] = 10
Sum of elements in Row [2] = 26
Sum of elements in Row [3] = 42
Sum of elements in Row [4] = 58
---The Sum of Columns---
Sum of elements in Column [1] = 28
Sum of elements in Column [2] = 32
Sum of elements in Column [3] = 36
Sum of elements in Column [4] = 40