Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program var_15_task_3_working_2;
- {$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, value: Integer;
- begin
- Write('Enter the number of rows for the ', name, ' matrix: ');
- ReadLn(rows);
- Write('Enter the number of columns for the ', name, ' matrix: ');
- ReadLn(cols);
- 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
- Write('Element at position (', (i + 1), ', ', (j + 1), '): ');
- ReadLn(value);
- Result[i][j] := value;
- end;
- end;
- function ReadMatrixFromFile(const fileName: string): TArray<TArray<Integer>>;
- var
- fileText: TextFile;
- i, j, rows, cols, value: Integer;
- begin
- 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);
- 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