Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

draw a flew char for this code import java.util.Scanner; public class Matrix { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(Enter

draw a flew char for this code

import java.util.Scanner;

public class Matrix {

public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter number of rows: first matrix "); int rows = scanner.nextInt(); System.out.print("Enter number of columns first matrix: "); int columns = scanner.nextInt(); System.out.print("Enter number of rows: seconed matrix "); int rowss = scanner.nextInt(); System.out.print("Enter number of columns seconed matrix: "); int columnss = scanner.nextInt(); System.out.println(); if(rowss!=rows || columnss!=columns) { System.out.println("Cannot subtract these! Dimension mismatch"); System.exit(0); } if(rows < 0 || columns < 0) { System.out.println("Invalid rows or columns"); System.exit(0); } System.out.println("Enter first matrix"); int[][] a = readMatrix(rows, columns); System.out.println(); if(rows < 0 || columns < 0) { System.out.println("Invalid rows or columns"); System.exit(0); } System.out.println("Enter second matrix"); int[][] b = readMatrix(rowss, columnss); int[][] difference = subtract(a, b); System.out.println("A - B ="); printMatrix(difference); System.out.println(); } public static int[][] readMatrix(int rows, int columns) { int[][] result = new int[rows][columns]; Scanner s = new Scanner(System.in); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { result[i][j] = s.nextInt(); } } return result; } public static int[][] subtract(int[][] a, int[][] b) { int rows = a.length; int columns = a[0].length; int[][] result = new int[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { result[i][j] = a[i][j] - b[i][j]; } } return result; }

public static void printMatrix(int[][] matrix) { int rows = matrix.length; int columns = matrix[0].length; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { System.out.print(matrix[i][j] + " "); } System.out.println();} } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions