# 2D-Array

# Problem-1 2D-Array easy

Create a method named createMatrix() that will take row and col as parameter and create a matrix by taking user input and return the matrix.

Sample Method Call Sample Output
int[][] A = createMatrix(2,2)
7 8
2 5
No Output
int[][] A = createMatrix(3,2)
17 82
12 5
3 9
No Output

# Problem-2 2D-Array easy

Create a method named printMatrix() that will take a matrix as parameter and print the matrix.

Sample Method Call Sample Output
int[][] A = {{1,2}, {3,4}};
printMatrix(A);
1 2
3 4
int[][] A = {{14,13,5,7}, {12,3,4,9}, {3,0,17,13}};
printMatrix(A)
14 13 5 7
12 13 4 9
3 0 17 13

# Problem-3 2D-Array easy

Create method named addition() that will take two matrices of same size as a parameter and return the summation of those two matrices.

Sample Method Call Sample Output
int[][] A = {{1,2}, {3,4}};
int[][] B = {{5,6}, {7,8}};
int[][] C = addition(A, B);
printMatrix(C)
6 8
10 12
int[][] A = {{1,2,3,4}, {5,6,7,8}};
int[][] B = {{10,20,30,40}, {50,60,70,80}};
int[][] C = addition(A, B);
printMatrix(C)
11 22 33 44
55 66 77 88

# Problem-4 2D-Array easy

Create a java method named sumOfBoundary() that takes a matrix as parameter and find the sum of all the boundary elements in the matrix.

Sample Method Call Sample Output
int[][] A = {{11,22,33,44}, {55,66,77,88}, {1,2,3,4}};
sumOfBoundary(A);
Sum of Boundary: 263

Explanation: Elements that are in the boundary of this matrix are 11+22+33+44+55+88+1+2+3+4 = 263

# Problem-5 2D-Array medium

Create a java method named multiplication() that two matrices as parameter and perform matrix multiplication and return the new matrix.

Sample Method Call Sample Output
int[][] A = {{1,2},{3,4},{5,6}};
int[][] B = {{1,2,3},{4,5,6}};
int[][] C = multiplication(A,B)
printMatrix(C);
9 12 15
19 26 33
29 40 51
int[][] A = {{1,2},{3,4},{5,6}};
int[][] B = {{1,2,3},{4,5,6},{7,8,9}};
int[][] C = multiplication(A,B)
printMatrix(C);
Matrix multiplication is not possible

# Problem-6 2D-Array medium

Create a java method named transpose() that takes a matrix as parameter and transpose the matrix and return the new transposed matrix.

Sample Method Call Sample Output
int[][] A = {{1,2},{3,4},{5,6}};
int[][] B = transpose(A);
printMatrix(B)
1 3 5
2 4 6
int[][] A = {{-2,5,6},{5,2,7}};
int[][] B = transpose(A);
printMatrix(B)
-2 5
5 2
6 7

# Problem-7 2D-Array hard

Create a java method named spiralTraversal() that takes a matrix as parameter and prints the matrix in spiral order.

Sample Method Call Sample Output
int[][] A = {{1,2,3},{4,5,6},{7,8,9}};
spiralTraversal(A);
Spiral Order: 1 4 7 8 9 6 3 2 5