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 matrixSum() here
public static double[][] matrixAdd(double[][] a, double[][] b)
{
double[][] c = new double[a.length][a[0].length];
for ( int i = 0; i < a.length; i++ )
for ( int j = 0; j < a[0].length; j++ )
c[i][j] = a[i][j] + b[i][j];
return c;
}
}
|