Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- public class MatrixMultiplication {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("Choose an option:");
- System.out.println("1. Input matrices manually");
- System.out.println("2. Read matrices from a text file");
- int option = scanner.nextInt();
- int[][] matrixA, matrixB;
- if (option == 1) {
- matrixA = inputMatrix("first");
- matrixB = inputMatrix("second");
- } else if (option == 2) {
- matrixA = readMatrixFromFile("matrixA.txt");
- matrixB = readMatrixFromFile("matrixB.txt");
- } else {
- System.out.println("Invalid option. Exiting program.");
- return;
- }
- if (matrixA == null || matrixB == null) {
- System.out.println("Error in reading matrices. Exiting program.");
- return;
- }
- int rowsA = matrixA.length;
- int colsA = matrixA[0].length;
- int rowsB = matrixB.length;
- int colsB = matrixB[0].length;
- if (colsA != rowsB) {
- System.out.println("Error: The number of columns in the first matrix must be equal to the number of rows in the second matrix.");
- return;
- }
- // Perform matrix multiplication
- int[][] resultMatrix = multiplyMatrices(matrixA, matrixB);
- // Display the result
- System.out.println("Result of matrix multiplication:");
- displayMatrix(resultMatrix);
- // Save the result to a text file
- saveMatrixToFile(resultMatrix, "resultMatrix.txt");
- }
- // Function to input matrix from the user
- private static int[][] inputMatrix(String name) {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter the number of rows for the " + name + " matrix: ");
- int rows = scanner.nextInt();
- System.out.print("Enter the number of columns for the " + name + " matrix: ");
- int cols = scanner.nextInt();
- if (rows <= 0 || cols <= 0) {
- System.out.println("Error: Matrix dimensions must be positive.");
- return null;
- }
- int[][] matrix = new int[rows][cols];
- System.out.println("Enter the elements for the " + name + " matrix:");
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- System.out.print("Element at position (" + (i + 1) + ", " + (j + 1) + "): ");
- matrix[i][j] = scanner.nextInt();
- }
- }
- return matrix;
- }
- // Function to read matrix from a text file
- private static int[][] readMatrixFromFile(String filename) {
- try {
- Scanner fileScanner = new Scanner(new File(filename));
- int rows = fileScanner.nextInt();
- int cols = fileScanner.nextInt();
- if (rows <= 0 || cols <= 0) {
- System.out.println("Error: Matrix dimensions must be positive.");
- return null;
- }
- int[][] matrix = new int[rows][cols];
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- if (fileScanner.hasNextInt()) {
- matrix[i][j] = fileScanner.nextInt();
- } else {
- System.out.println("Error: Insufficient data in the file.");
- return null;
- }
- }
- }
- fileScanner.close();
- return matrix;
- } catch (IOException e) {
- System.out.println("Error: File not found or cannot be read.");
- return null;
- }
- }
- // Function to multiply two matrices
- private static int[][] multiplyMatrices(int[][] matrixA, int[][] matrixB) {
- int rowsA = matrixA.length;
- int colsA = matrixA[0].length;
- int colsB = matrixB[0].length;
- int[][] resultMatrix = new int[rowsA][colsB];
- for (int i = 0; i < rowsA; i++) {
- for (int j = 0; j < colsB; j++) {
- for (int k = 0; k < colsA; k++) {
- resultMatrix[i][j] += matrixA[i][k] * matrixB[k][j];
- }
- }
- }
- return resultMatrix;
- }
- // Function to display a matrix
- private static void displayMatrix(int[][] matrix) {
- for (int[] row : matrix) {
- for (int value : row) {
- System.out.print(value + " ");
- }
- System.out.println();
- }
- }
- // Function to save a matrix to a text file
- private static void saveMatrixToFile(int[][] matrix, String filename) {
- try {
- FileWriter writer = new FileWriter(filename);
- writer.write(matrix.length + " " + matrix[0].length + "\n");
- for (int[] row : matrix) {
- for (int value : row) {
- writer.write(value + " ");
- }
- writer.write("\n");
- }
- writer.close();
- System.out.println("Result matrix saved to " + filename);
- } catch (IOException e) {
- System.out.println("Error: Unable to save result matrix to file.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement