Advertisement
Sauka1337

Untitled

Oct 11th, 2023
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.41 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5.  
  6. public class MatrixMultiplication {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         System.out.println("Choose an option:");
  11.         System.out.println("1. Input matrices manually");
  12.         System.out.println("2. Read matrices from a text file");
  13.         int option = scanner.nextInt();
  14.  
  15.         int[][] matrixA, matrixB;
  16.  
  17.         if (option == 1) {
  18.             matrixA = inputMatrix("first");
  19.             matrixB = inputMatrix("second");
  20.         } else if (option == 2) {
  21.             matrixA = readMatrixFromFile("matrixA.txt");
  22.             matrixB = readMatrixFromFile("matrixB.txt");
  23.         } else {
  24.             System.out.println("Invalid option. Exiting program.");
  25.             return;
  26.         }
  27.  
  28.         if (matrixA == null || matrixB == null) {
  29.             System.out.println("Error in reading matrices. Exiting program.");
  30.             return;
  31.         }
  32.  
  33.         int rowsA = matrixA.length;
  34.         int colsA = matrixA[0].length;
  35.         int rowsB = matrixB.length;
  36.         int colsB = matrixB[0].length;
  37.  
  38.         if (colsA != rowsB) {
  39.             System.out.println("Error: The number of columns in the first matrix must be equal to the number of rows in the second matrix.");
  40.             return;
  41.         }
  42.  
  43.         // Perform matrix multiplication
  44.         int[][] resultMatrix = multiplyMatrices(matrixA, matrixB);
  45.  
  46.         // Display the result
  47.         System.out.println("Result of matrix multiplication:");
  48.         displayMatrix(resultMatrix);
  49.  
  50.         // Save the result to a text file
  51.         saveMatrixToFile(resultMatrix, "resultMatrix.txt");
  52.     }
  53.  
  54.     // Function to input matrix from the user
  55.     private static int[][] inputMatrix(String name) {
  56.         Scanner scanner = new Scanner(System.in);
  57.  
  58.         System.out.print("Enter the number of rows for the " + name + " matrix: ");
  59.         int rows = scanner.nextInt();
  60.         System.out.print("Enter the number of columns for the " + name + " matrix: ");
  61.         int cols = scanner.nextInt();
  62.  
  63.         if (rows <= 0 || cols <= 0) {
  64.             System.out.println("Error: Matrix dimensions must be positive.");
  65.             return null;
  66.         }
  67.  
  68.         int[][] matrix = new int[rows][cols];
  69.  
  70.         System.out.println("Enter the elements for the " + name + " matrix:");
  71.         for (int i = 0; i < rows; i++) {
  72.             for (int j = 0; j < cols; j++) {
  73.                 System.out.print("Element at position (" + (i + 1) + ", " + (j + 1) + "): ");
  74.                 matrix[i][j] = scanner.nextInt();
  75.             }
  76.         }
  77.  
  78.         return matrix;
  79.     }
  80.  
  81.     // Function to read matrix from a text file
  82.     private static int[][] readMatrixFromFile(String filename) {
  83.         try {
  84.             Scanner fileScanner = new Scanner(new File(filename));
  85.             int rows = fileScanner.nextInt();
  86.             int cols = fileScanner.nextInt();
  87.  
  88.             if (rows <= 0 || cols <= 0) {
  89.                 System.out.println("Error: Matrix dimensions must be positive.");
  90.                 return null;
  91.             }
  92.  
  93.             int[][] matrix = new int[rows][cols];
  94.  
  95.             for (int i = 0; i < rows; i++) {
  96.                 for (int j = 0; j < cols; j++) {
  97.                     if (fileScanner.hasNextInt()) {
  98.                         matrix[i][j] = fileScanner.nextInt();
  99.                     } else {
  100.                         System.out.println("Error: Insufficient data in the file.");
  101.                         return null;
  102.                     }
  103.                 }
  104.             }
  105.  
  106.             fileScanner.close();
  107.             return matrix;
  108.         } catch (IOException e) {
  109.             System.out.println("Error: File not found or cannot be read.");
  110.             return null;
  111.         }
  112.     }
  113.  
  114.     // Function to multiply two matrices
  115.     private static int[][] multiplyMatrices(int[][] matrixA, int[][] matrixB) {
  116.         int rowsA = matrixA.length;
  117.         int colsA = matrixA[0].length;
  118.         int colsB = matrixB[0].length;
  119.  
  120.         int[][] resultMatrix = new int[rowsA][colsB];
  121.  
  122.         for (int i = 0; i < rowsA; i++) {
  123.             for (int j = 0; j < colsB; j++) {
  124.                 for (int k = 0; k < colsA; k++) {
  125.                     resultMatrix[i][j] += matrixA[i][k] * matrixB[k][j];
  126.                 }
  127.             }
  128.         }
  129.  
  130.         return resultMatrix;
  131.     }
  132.  
  133.     // Function to display a matrix
  134.     private static void displayMatrix(int[][] matrix) {
  135.         for (int[] row : matrix) {
  136.             for (int value : row) {
  137.                 System.out.print(value + " ");
  138.             }
  139.             System.out.println();
  140.         }
  141.     }
  142.  
  143.     // Function to save a matrix to a text file
  144.     private static void saveMatrixToFile(int[][] matrix, String filename) {
  145.         try {
  146.             FileWriter writer = new FileWriter(filename);
  147.  
  148.             writer.write(matrix.length + " " + matrix[0].length + "\n");
  149.  
  150.             for (int[] row : matrix) {
  151.                 for (int value : row) {
  152.                     writer.write(value + " ");
  153.                 }
  154.                 writer.write("\n");
  155.             }
  156.  
  157.             writer.close();
  158.             System.out.println("Result matrix saved to " + filename);
  159.         } catch (IOException e) {
  160.             System.out.println("Error: Unable to save result matrix to file.");
  161.         }
  162.     }
  163. }
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement