Write a method named matrixAdd that accepts a pair of two-dimensional arrays of
ID: 3528182 • Letter: W
Question
Write a method named matrixAdd that accepts a pair of two-dimensional arrays of integers as parameters, treats the arrays as 2D matrices and adds them, returning the result. The sum of two matrices A and B is a matrix C where for every row i and column j, Cij = Aij + Bij. You may assume that the arrays passed as parameters have the same dimensions. this is the coding I have so far: publicint[][]matrixAdd(int[][]a,int[][]b) { intnumOfRows=a.length;//numberofrows intnumOfCols=a[0].length;//numberofcolumnsinfirstrow int[][]c=newint[numOfRows][numOfCols]; if (a.length == 0) { return new int[][]{}; } for(inti=0;i<numOfRows;i++) { for(intj=0;j<numOfCols;j++) { System.out.println(a[i][j]+0); } } returnc; } These are my expected returns: {{6, 7, 9}, {4, 3, 6}} {}Explanation / Answer
public static int [][] matrixAdd(int [][]array1, int [][]array2) { int [][] c = new int[array1.length][array1[0].length]; for(int row=0; rowRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.