Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

we were given this program in class yesterday. The original question is here Write a method to add/subtract two matrices. The header of the method

we were given this program in class yesterday. The original question is here

Write a method to add/subtract two matrices. The header of the method is as follows:

public static double[][] addMatrix(double[][] a, double[][] b

or

public static double[][] subtractMatrix(double[][] a, double[][] b

In order to be add/subtract, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each element cij is aij + bij. For example, for two 3 * 3 matrices a and b, c is

Write a test program that prompts the user to enter two 3 * 3 matrices and displays their sum or differece. Here is a sample run

the code given is this below. could you please explain what is going on throughout it? I still do not understand it.

Thank you!

package Week4;

import java.util.Scanner;

public class Lab4_3 {

//Calculations public static double[][] subtractMatrix(double[][] a, double[][] b) { double[][] result = new double[a.length][a[0].length]; for (int i = 0; i < a.length; ++i) { for (int j = 0; j < a[i].length; ++j) { result[i][j] = a[i][j] - b[i][j]; } } return result; }

public static void main(String[] args) { Scanner in = new Scanner(System.in); double[][] matrix1 = new double[3][3]; double[][] matrix2 = new double[3][3];

//Prompt user to input maxtrix 1 System.out.print("Enter matrix1: "); for (int i = 0; i < matrix1.length; ++i) { for (int j = 0; j < matrix1[i].length; ++j) { matrix1[i][j] = in.nextDouble(); } }

//Prompt user to input maxtrix 2 System.out.print("Enter matrix2: "); for (int i = 0; i < matrix2.length; ++i) { for (int j = 0; j < matrix2[i].length; ++j) { matrix2[i][j] = in.nextDouble(); } } double[][] matrix3 = subtractMatrix(matrix1, matrix2);

//Output System.out.println(" The Subtraction of the matrices is"); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { System.out.printf("%5.1f\t", matrix1[i][j]); } if (i == 1) { System.out.printf(" - "); } else { System.out.printf(" "); } for (int j = 0; j < 3; ++j) { System.out.printf("%5.1f\t", matrix2[i][j]); } if (i == 1) { System.out.printf(" = "); } else { System.out.printf(" "); } for (int j = 0; j < 3; ++j) { System.out.printf("%5.1f\t", matrix3[i][j]); } System.out.printf(" "); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Carlos Coronel, Steven Morris

14th Edition

978-0357673034

More Books

Students also viewed these Databases questions

Question

Need help with this homework, any help is greatly appreciated!

Answered: 1 week ago

Question

a. How will the leader be selected?

Answered: 1 week ago