Write code to Add two matrix and subtract matrix - Java Data Structure

Java examples for Data Structure:Matrix

Description

Write code to Add two matrix and subtract matrix

You can use the following code structure and fill in your logics.

import java.util.Random;

public class Main {

   public static void main(String[] args) {
       int rows = 5;
       int columns = 5;
       int[][] a = readMatrix(rows, columns);
       int[][] b = readMatrix(rows, columns);
       int[][] sum = add(a, b);
       int[][] difference1 = subtract(a, b);
       int[][] difference2 = subtract(b, a);
       System.out.println();
       System.out.println("A + B =");
       printMatrix(sum);
       System.out.println();
       System.out.println("A - B =");
       printMatrix(difference1);
       System.out.println();
       System.out.println("B - A =");
       printMatrix(difference2);
   }

   public static int[][] readMatrix(int rows, int columns) {
       int[][] result = new int[rows][columns];
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < columns; j++) {
               result[i][j] = new Random().nextInt();
           }
       }
       return result;
   }

   public static int[][] add(int[][] a, int[][] b) {
       //your code here
   }

   public static int[][] subtract(int[][] a, int[][] b) {
       //your code here
   }

   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();
       }
   }
}

Solution

Demo Code



import java.util.Random;

public class Main {

   public static void main(String[] args) {
       int rows = 5;
       int columns = 5;
       int[][] a = readMatrix(rows, columns);
       int[][] b = readMatrix(rows, columns);
       int[][] sum = add(a, b);
       int[][] difference1 = subtract(a, b);
       int[][] difference2 = subtract(b, a);
       System.out.println();//  w  ww . j a v  a2 s  . c o m
       System.out.println("A + B =");
       printMatrix(sum);
       System.out.println();
       System.out.println("A - B =");
       printMatrix(difference1);
       System.out.println();
       System.out.println("B - A =");
       printMatrix(difference2);
   }

   public static int[][] readMatrix(int rows, int columns) {
       int[][] result = new int[rows][columns];
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < columns; j++) {
               result[i][j] = new Random().nextInt();
           }
       }
       return result;
   }

   public static int[][] add(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 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();
       }
   }
}

Related Tutorials