| 1 | import java.util.Scanner; |
| 2 | |
| 3 | public class Matrix { |
| 4 | static void printMatrix(int[][] matrix){ |
| 5 | for(int i = 0; i < matrix.length; i++){ |
| 6 | for(int j = 0; j < matrix[i].length; j++){ |
| 7 | System.out.print(matrix[i][j] + " "); |
| 8 | } |
| 9 | System.out.println(); |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | static void multiply(int[][] a, int r1, int c1, int[][] b, int r2, int c2){ |
| 14 | if(c1 != r2){ |
| 15 | System.out.println("Multiplication not possible - wrong dimension"); |
| 16 | return; |
| 17 | } |
| 18 | |
| 19 | int[][] mul = new int[r1][c2]; |
| 20 | |
| 21 | for(int i = 0; i < r1; i++){ // row number |
| 22 | for(int j = 0; j < c2; j++){ //column number |
| 23 | for(int k = 0; k < c1; k++){ |
| 24 | /* |
| 25 | i = 1, j = 0 |
| 26 | mul[i][j] = ith row of a * jth col of b |
| 27 | */ |
| 28 | mul[i][j] += (a[i][k] * b[k][j]); |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | System.out.println("Multiplication of 2 matrices"); |
| 34 | printMatrix(mul); |
| 35 | } |
| 36 | |
| 37 | static void add(int[][] a, int r1, int c1, int[][] b, int r2, int c2){ |
| 38 | if(r1 != r2 || c1 != c2){ |
| 39 | System.out.println("Wrong Input - Addition not possible"); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | int[][] sum = new int[r1][c1]; |
| 44 | |
| 45 | for(int i = 0; i < r1; i++){ //row number |
| 46 | for(int j = 0; j < c1; j++){ //column number |
| 47 | sum[i][j] = a[i][j] + b[i][j]; |
| 48 | } |
| 49 | } |
| 50 | System.out.println("Sum of matrix 1 and matrix 2"); |
| 51 | printMatrix(sum); |
| 52 | } |
| 53 | |
| 54 | public static void main(String[] args) { |
| 55 | Scanner sc = new Scanner(System.in); |
| 56 | System.out.println("Enter number of rows and columns of matrix 1"); |
| 57 | int r1 = sc.nextInt(); |
| 58 | int c1 = sc.nextInt(); |
| 59 | int[][] a = new int[r1][c1]; |
| 60 | System.out.println("Enter matrix values"); |
nothing calls this directly
no outgoing calls
no test coverage detected