I'm having some trouble with the question below, not entirely sure if its write as I can't figure out how to call the method in the driver method to print the result.
(Question) Create a Method with header:
public static int[][] multiplyArrays(int[][] a, int[][] b)
The above method will multiply two 2D arrays referred to by a and b, and return a 2D array
reference of a × b
. Multiplying two 2D array work on the following algebraic expression to
find out the resultant array: result[i][j]= a[i][k] * b[k][j]
Hint: In this case, inside the method, declare a 2D int[][]
array reference variable and
instantiate it with a size of a.length x b[0].length
. They complete the rest using the
series multiplication. This is an application of nested loop. The outermost loop will run from
0
to N
. The middle loop will run for index i
and the innermost one will run for index j
.
My code so far:
public static int[][] multiplyArrays(int[][] a, int[][] b) {
var aNumRows = a.length;
var aNumCols = a[0].length;
var bNumCols = b[0].length;
int[][] m = new int[aNumRows][bNumCols];
for (var r = 0; r < aNumRows; r) {
for (var c = 0; c < bNumCols; c) {
m[r][c] = 0;
for (var i = 0; i < aNumCols; i) {
m[r][c] = a[r][i] * b[i][c];
System.out.printf("=",r, c);
}
}
}
return m;
}
CodePudding user response:
Since the method you defined is static, you could simply use classname.multiplyArrays(a, b)
; where a and b are the names of the variables in your driver method.