[go: up one dir, main page]

0% found this document useful (0 votes)
23 views4 pages

Assignment 02

Uploaded by

riddhichaskar750
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

Assignment 02

Uploaded by

riddhichaskar750
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

Scanner;

class MatrixInputOutput {
private Scanner scanner = new Scanner(System.in);

public void printMatrix(int[][] matrix) {


for (int[] row : matrix) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}

public void print3DMatrix(int[][][] matrix) {


for (int i = 0; i < matrix.length; i++) {
System.out.println("Matrix slice " + (i + 1) + ":");
for (int[] row : matrix[i]) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
System.out.println();
}
}

public int[][] getMatrixFromUser(int rows, int cols, String matrixName) {


int[][] matrix = new int[rows][cols];
System.out.println("Enter elements of " + matrixName + " (Matrix " + rows +
"x" + cols + "):");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element [" + i + "][" + j + "]: ");
matrix[i][j] = scanner.nextInt();
}
}
return matrix;
}

public int[][][] get3DMatrixFromUser(int depth, int rows, int cols, String


matrixName) {
int[][][] matrix = new int[depth][rows][cols];
System.out.println("Enter elements of " + matrixName + " (Matrix " + depth
+ "x" + rows + "x" + cols + "):");
for (int d = 0; d < depth; d++) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element [" + d + "][" + i + "][" + j + "]:
");
matrix[d][i][j] = scanner.nextInt();
}
}
}
return matrix;
}
}

class MatrixMultiplication {
public int[][] multiply(int[][] matrixA, int[][] matrixB, int rowsA, int colsA,
int colsB) {
int[][] result = new int[rowsA][colsB];

for (int i = 0; i < rowsA; i++) {


for (int j = 0; j < colsB; j++) {
result[i][j] = 0;
for (int k = 0; k < colsA; k++) {
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
return result;
}

public int[][][] multiply3D(int[][][] matrixA, int[][][] matrixB, int depthA,


int rowsA, int colsA, int colsB) {
int[][][] result = new int[depthA][rowsA][colsB];

for (int d = 0; d < depthA; d++) {


for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
result[d][i][j] = 0;
for (int k = 0; k < colsA; k++) {
result[d][i][j] += matrixA[d][i][k] * matrixB[d][k][j];
}
}
}
}
return result;
}
}

public class Assignment_02 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Choose Matrix Multiplication Type:");


System.out.println("1. 2D Matrix Multiplication");
System.out.println("2. 3D Matrix Multiplication");
int choice = scanner.nextInt();

MatrixInputOutput matrixIO = new MatrixInputOutput();


MatrixMultiplication matrixMultiplication = new MatrixMultiplication();

switch (choice) {
case 1:

System.out.print("Enter number of rows for Matrix A: ");


int rowsA = scanner.nextInt();
System.out.print("Enter number of columns for Matrix A: ");
int colsA = scanner.nextInt();

System.out.print("Enter number of rows for Matrix B: ");


int rowsB = scanner.nextInt();
System.out.print("Enter number of columns for Matrix B: ");
int colsB = scanner.nextInt();
if (colsA != rowsB) {
System.out.println("Matrix multiplication not possible. Columns
of Matrix A must be equal to rows of Matrix B.");
return;
}

int[][] matrixA = matrixIO.getMatrixFromUser(rowsA, colsA, "Matrix


A");
int[][] matrixB = matrixIO.getMatrixFromUser(rowsB, colsB, "Matrix
B");

System.out.println("Matrix A:");
matrixIO.printMatrix(matrixA);

System.out.println("Matrix B:");
matrixIO.printMatrix(matrixB);

int[][] resultMatrix = matrixMultiplication.multiply(matrixA,


matrixB, rowsA, colsA, colsB);

System.out.println("Resultant Matrix (A * B):");


matrixIO.printMatrix(resultMatrix);
break;

case 2:

System.out.print("Enter depth for Matrix A and Matrix B: ");


int depth = scanner.nextInt();
System.out.print("Enter number of rows for Matrix A and Matrix B:
");
int rows3D = scanner.nextInt();
System.out.print("Enter number of columns for Matrix A and Matrix
B: ");
int cols3D = scanner.nextInt();

int[][][] matrixA3D = matrixIO.get3DMatrixFromUser(depth, rows3D,


cols3D, "Matrix A");
int[][][] matrixB3D = matrixIO.get3DMatrixFromUser(depth, rows3D,
cols3D, "Matrix B");

System.out.println("Matrix A:");
matrixIO.print3DMatrix(matrixA3D);

System.out.println("Matrix B:");
matrixIO.print3DMatrix(matrixB3D);

int[][][] result3DMatrix =
matrixMultiplication.multiply3D(matrixA3D, matrixB3D, depth, rows3D, cols3D,
cols3D);

System.out.println("Resultant 3D Matrix (A * B):");


matrixIO.print3DMatrix(result3DMatrix);
break;

default:
System.out.println("Invalid choice. Please select 1 or 2.");
break;
}
}
}

You might also like