A two-dimensional matrix can be represented as a one-dimensional array with nested arrays for each row. The matrixAdd method takes two 2D integer arrays of equal size as parameters, adds the elements together position by position, and returns a new 2D array containing the sums. It uses nested for loops to iterate through the rows and columns of the input arrays, perform the additions, and populate the output array before returning it.
Unit-IV; Professional Sales Representative (PSR).pptx
A matrix with M rows and N columns is actually an array of length M- e.docx
1. A matrix with M rows and N columns is actually an array of length M, each entry of which is an
array of length N. In a two-dimensional Java array, we can use the code ali] to refer to the ith
row (which is a one-dimensional array) For example, a 3x3 matrix is represented by the
following array: int my Matrix 129 28, 27 116 15, 14), 13, 2, 1) Write the matrixAdd method.
This method takes two two-dimensional Java integer arrays as parameters, adds and returns a
new two-dimensional Java integer array. Note: you can assume that the printIntArray method is
given for you in this question and the two parameters arrays are of equal length
Solution
Answer:
import java.util.*;
class matrixAdd {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter value for rows in matrix for addition ");
int horizontals = s.nextInt();
System.out.println("Enter value for columns in matrix for addition");
int verticals = s.nextInt();
int[][] a = new int[horizontals][verticals];
int[][] b = new int[horizontals][verticals];
System.out.println("Enter the first matrix");
for (int i = 0; i < horizontals; i++) {
for (int j = 0; j < verticals; j++) {
a[i][j] = s.nextInt();
}
}
System.out.println("Enter the second matrix");
for (int i = 0; i < horizontals; i++) {
for (int j = 0; j < verticals; j++) {
b[i][j] = s.nextInt();
}
}
2. int[][] c = new int[horizontals][verticals];
for (int i = 0; i < horizontals; i++) {
for (int j = 0; j < verticals; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("The result of the two matrices after addition is");
for (int i = 0; i < horizontals; i++) {
for (int j = 0; j < verticals; j++) {
System.out.print(c[i][j] + " ");
}
}
}
}