Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program MatrixMultiplication;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- function MultiplyMatrices(const matrixA, matrixB: TArray<TArray<Integer>>): TArray<TArray<Integer>>;
- var
- rowsA, colsA, colsB, i, j, k: Integer;
- begin
- rowsA := Length(matrixA);
- colsA := Length(matrixA[0]);
- colsB := Length(matrixB[0]);
- SetLength(Result, rowsA, colsB);
- for i := 0 to rowsA - 1 do
- for j := 0 to colsB - 1 do
- begin
- Result[i][j] := 0;
- for k := 0 to colsA - 1 do
- Result[i][j] := Result[i][j] + matrixA[i][k] * matrixB[k][j];
- end;
- end;
- procedure DisplayMatrix(const matrix: TArray<TArray<Integer>>);
- var
- i, j: Integer;
- begin
- for i := 0 to High(matrix) do
- begin
- for j := 0 to High(matrix[i]) do
- Write(matrix[i][j], ' ');
- WriteLn;
- end;
- end;
- procedure SaveMatrixToFile(const matrix: TArray<TArray<Integer>>; const fileName: string);
- var
- fileText: TextFile;
- i, j: Integer;
- begin
- AssignFile(fileText, fileName);
- Rewrite(fileText);
- for i := 0 to High(matrix) do
- begin
- for j := 0 to High(matrix[i]) do
- Write(fileText, matrix[i][j], ' ');
- Writeln(fileText);
- end;
- CloseFile(fileText);
- end;
- function InputMatrix(const name: string): TArray<TArray<Integer>>;
- var
- rows, cols, i, j: Integer;
- inputValueStr: string;
- begin
- repeat
- Write('Enter the number of rows for the ', name, ' matrix: ');
- ReadLn(rows);
- Write('Enter the number of columns for the ', name, ' matrix: ');
- ReadLn(cols);
- if (rows <= 0) or (cols <= 0) then
- WriteLn('Error: Matrix dimensions must be positive.');
- until (rows > 0) and (cols > 0);
- SetLength(Result, rows, cols);
- WriteLn('Enter the elements for the ', name, ' matrix:');
- for i := 0 to rows - 1 do
- for j := 0 to cols - 1 do
- begin
- repeat
- Write('Element at position (', (i + 1), ', ', (j + 1), '): ');
- ReadLn(inputValueStr);
- if not TryStrToInt(inputValueStr, Result[i][j]) then
- WriteLn('Error: Invalid input. Please enter a valid integer.');
- until TryStrToInt(inputValueStr, Result[i][j]);
- end;
- WriteLn;
- end;
- function ReadMatrixFromFile(const fileName: string): TArray<TArray<Integer>>;
- var
- fileText: TextFile;
- i, j, rows, cols, value: Integer;
- begin
- if not FileExists(fileName) then
- begin
- WriteLn('Error: File not found or cannot be read.');
- Exit(nil);
- end;
- AssignFile(fileText, fileName);
- Reset(fileText);
- // Determine the size of the matrix
- rows := 0;
- cols := 0;
- while not Eof(fileText) do
- begin
- Read(fileText, value);
- Inc(cols);
- if Eoln(fileText) then
- begin
- Inc(rows);
- ReadLn(fileText); // Move to the next line
- end;
- end;
- // Reset the file position to the beginning
- Reset(fileText);
- if (rows <= 0) or (cols mod rows <> 0) then
- begin
- WriteLn('Error: Invalid matrix dimensions in the file.');
- Exit(nil);
- end;
- SetLength(Result, rows, cols div rows);
- // Read the matrix from the file
- for i := 0 to rows - 1 do
- for j := 0 to cols div rows - 1 do
- Read(fileText, Result[i][j]);
- CloseFile(fileText);
- end;
- var
- matrixA, matrixB, resultMatrix: TArray<TArray<Integer>>;
- option: Integer;
- fileNameA, fileNameB: string;
- begin
- // Ask the user whether to input matrices or read from files
- WriteLn('Choose an option:');
- WriteLn('1. Input matrices manually');
- WriteLn('2. Read matrices from text files');
- ReadLn(option);
- case option of
- 1:
- begin
- matrixA := InputMatrix('first');
- matrixB := InputMatrix('second');
- end;
- 2:
- begin
- Write('Enter the file name for matrix A: ');
- ReadLn(fileNameA);
- matrixA := ReadMatrixFromFile(fileNameA);
- Write('Enter the file name for matrix B: ');
- ReadLn(fileNameB);
- matrixB := ReadMatrixFromFile(fileNameB);
- end;
- else
- begin
- WriteLn('Invalid option. Exiting program.');
- Exit;
- end;
- end;
- if (Length(matrixA) = 0) or (Length(matrixB) = 0) then
- begin
- WriteLn('Error in reading matrices. Exiting program.');
- Exit;
- end;
- // Perform matrix multiplication
- resultMatrix := MultiplyMatrices(matrixA, matrixB);
- // Display the matrices and result
- WriteLn('Matrix A:');
- DisplayMatrix(matrixA);
- WriteLn('Matrix B:');
- DisplayMatrix(matrixB);
- WriteLn('Result of matrix multiplication:');
- DisplayMatrix(resultMatrix);
- // Save the result to a text file
- SaveMatrixToFile(resultMatrix, 'resultMatrix.txt');
- WriteLn('Result matrix saved to resultMatrix.txt');
- ReadLn; // Keep console open
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement