The matrixAdd(a, b) method will add the elements a[i][j] + b[i][j] together and creates a result matrix of the sum.
For example, if the input matrices are:
double[][] a = { {1, 2},
{3, 4} };
double[][] b = { {1, 1},
{1, 1} };
|
the matrixAdd(a,b) call will return an matrix with values:
{ {2, 3},
{4, 5} }
|
And if the input matrices are:
double[][] c = { {2, 3, 4},
{3, 7, 5} };
double[][] d = {-1, -1, -1}
{-1, -1, -1} };
|
the matrixAdd(c,d) call will return an matrix with values
{ {1, 2, 3},
{2, 6, 4} };
|
Use the program below to complete the assignment:
import java.util.Arrays;
public class myProg
{
public static void main(String[] args)
{
double[][] a = { {1, 2},
{3, 4} };
double[][] b = { {1, 1},
{1, 1} };
double[][] x;
x = matrixAdd(a, b);
System.out.println( Arrays.toString(a[0]) );
System.out.println( Arrays.toString(a[1]) );
System.out.println( " +" );
System.out.println( Arrays.toString(b[0]) );
System.out.println( Arrays.toString(b[1]) );
System.out.println( " =" );
System.out.println( Arrays.toString(x[0]) );
System.out.println( Arrays.toString(x[1]) );
System.out.println("\n");
double[][] c = { {2, 3, 4},
{3, 7, 5} };
double[][] d = { {-1, -1, -1},
{-1, -1, -1} };
x = matrixAdd(c, d);
System.out.println( Arrays.toString(c[0]) );
System.out.println( Arrays.toString(c[1]) );
System.out.println( " +" );
System.out.println( Arrays.toString(c[0]) );
System.out.println( Arrays.toString(c[1]) );
System.out.println( " =" );
System.out.println( Arrays.toString(x[0]) );
System.out.println( Arrays.toString(x[1]) );
}
// Write the method matrixAdd() here
}
|