Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Matrix {
  3.   public static void main (String[] args){
  4.     Scanner sc = new Scanner(System.in);
  5.     System.out.println("Enter the number of rows for the 1st matrix (Matrix A): ");
  6.     int s = sc.nextInt();
  7.     System.out.println("Enter the number of columns for the 1st matrix (Matrix A): ");
  8.     int t = sc.nextInt();
  9.     System.out.println("Enter the number of rows for the 1st matrix (Matrix B): ");
  10.     int o = sc.nextInt();
  11.     System.out.println("Enter the number of columns for the 1st matrix (Matrix B): ");
  12.     int p = sc.nextInt();
  13.     int[][] a = new int[s][t];
  14.     int[][] b = new int[o][p];
  15.     for (x=0; x< a.length; x++){
  16.       for (y=0; y<a[0].length; y++){
  17.       System.out.println("Enter a["+x+"]["+y+"]: ");
  18.       a[x][y] = sc.nextInt();
  19.       }
  20.     }
  21.     for (c=0; c< b.length; c++){
  22.       for (d=0; d<b[0].length; d++){
  23.       System.out.println("Enter b["+c+"]["+d+"]: ");
  24.       b[x][y] = sc.nextInt();
  25.       }
  26.     }
  27.   public static int[][] matrixAdd(int[][] a, int[][] b) {
  28.     if (a.length == 0 || b.length == 0) {
  29.         return new int[0][0];
  30.     }
  31.     else {
  32.         int[][] sum = new int[a.length][a[0].length];
  33.        
  34.         for (int i = 0; i < result.length; i++) {
  35.        
  36.             for (int j = 0; j < result[0].length; j++) {
  37.                 result[i][j] = a[i][j] + b[i][j];
  38.             }
  39.            
  40.         }
  41.        
  42.         return result;
  43.     }
  44. }
  45.   public static int[][] matrixSub(int[][] a, int[][] b) {
  46.     if (a.length == 0 || b.length == 0) {
  47.         return new int[0][0];
  48.     }
  49.     else {
  50.         int[][] sub = new int[a.length][a[0].length];
  51.        
  52.         for (int i = 0; i < result.length; i++) {
  53.        
  54.             for (int j = 0; j < result[0].length; j++) {
  55.                 result[i][j] = a[i][j] - b[i][j];
  56.             }
  57.            
  58.         }
  59.        
  60.         return result;
  61.     }
  62. }
  63.   public static int[][] matrixSub(int[][] a, int[][] b) {
  64.     if (a.length == 0 || b.length == 0) {
  65.         return new int[0][0];
  66.     }
  67.     else {
  68.         int[][] sub = new int[a.length][a[0].length];
  69.        
  70.         for (int i = 0; i < result.length; i++) {
  71.        
  72.             for (int j = 0; j < result[0].length; j++) {
  73.                 result[i][j] = a[i][j] - b[i][j];
  74.             }
  75.            
  76.         }
  77.        
  78.         return result;
  79.     }
  80. }
  81. }
  82.  
  83.     Scanner sc = new Scanner(System.in);
  84.     System.out.println()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement