Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <vector>
- using namespace std;
- // Function prototypes
- vector<vector<int>> inputMatrix(const string& name);
- vector<vector<int>> readMatrixFromFile(const string& fileName);
- vector<vector<int>> multiplyMatrices(const vector<vector<int>>& matrixA, const vector<vector<int>>& matrixB);
- void displayMatrix(const vector<vector<int>>& matrix);
- void saveMatrixToFile(const vector<vector<int>>& matrix, const string& filename);
- int main() {
- cout << "Choose an option:" << endl;
- cout << "1. Input matrices manually" << endl;
- cout << "2. Read matrices from a text file" << endl;
- int option;
- cin >> option;
- vector<vector<int>> matrixA, matrixB;
- if (option == 1) {
- matrixA = inputMatrix("first");
- matrixB = inputMatrix("second");
- } else if (option == 2) {
- cout << "Enter the file name for matrix A: ";
- string fileNameA;
- cin >> fileNameA;
- matrixA = readMatrixFromFile(fileNameA);
- cout << "Enter the file name for matrix B: ";
- string fileNameB;
- cin >> fileNameB;
- matrixB = readMatrixFromFile(fileNameB);
- } else {
- cout << "Invalid option. Exiting program." << endl;
- return 1;
- }
- if (matrixA.empty() || matrixB.empty()) {
- cout << "Error in reading matrices. Exiting program." << endl;
- return 1;
- }
- // Perform matrix multiplication
- vector<vector<int>> resultMatrix = multiplyMatrices(matrixA, matrixB);
- // Display the result
- cout << "Result of matrix multiplication:" << endl;
- displayMatrix(resultMatrix);
- // Save the result to a text file
- saveMatrixToFile(resultMatrix, "resultMatrix.txt");
- cout << "Result matrix saved to resultMatrix.txt" << endl;
- return 0;
- }
- // Function to input matrix from the user
- vector<vector<int>> inputMatrix(const string& name) {
- cout << "Enter the number of rows for the " << name << " matrix: ";
- int rows, cols;
- cin >> rows;
- cout << "Enter the number of columns for the " << name << " matrix: ";
- cin >> cols;
- if (rows <= 0 || cols <= 0) {
- cout << "Error: Matrix dimensions must be positive." << endl;
- return {};
- }
- vector<vector<int>> matrix(rows, vector<int>(cols, 0));
- cout << "Enter the elements for the " << name << " matrix:" << endl;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- cout << "Element at position (" << (i + 1) << ", " << (j + 1) << "): ";
- cin >> matrix[i][j];
- }
- }
- return matrix;
- }
- // Function to read matrix from a text file
- vector<vector<int>> readMatrixFromFile(const string& fileName) {
- ifstream file(fileName);
- if (!file.is_open()) {
- cout << "Error: File not found or cannot be read." << endl;
- return {};
- }
- // Initialize a stringstream to parse each line
- stringstream ss;
- string line;
- int rows = 0;
- int cols = 0;
- // Count the number of rows and columns
- while (getline(file, line)) {
- ss.clear();
- ss.str(line);
- int value;
- while (ss >> value) {
- cols++;
- }
- rows++;
- }
- // Reset file to the beginning
- file.clear();
- file.seekg(0);
- // Initialize the matrix
- vector<vector<int>> matrix(rows, vector<int>(cols / rows, 0));
- // Read matrix elements
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols / rows; j++) {
- if (!(file >> matrix[i][j])) {
- cout << "Error: Insufficient data in the file." << endl;
- return {};
- }
- }
- }
- file.close();
- return matrix;
- }
- // Function to multiply two matrices
- vector<vector<int>> multiplyMatrices(const vector<vector<int>>& matrixA, const vector<vector<int>>& matrixB) {
- int rowsA = matrixA.size();
- int colsA = matrixA[0].size();
- int colsB = matrixB[0].size();
- vector<vector<int>> resultMatrix(rowsA, vector<int>(colsB, 0));
- 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
- void displayMatrix(const vector<vector<int>>& matrix) {
- for (const auto& row : matrix) {
- for (int value : row) {
- cout << value << " ";
- }
- cout << endl;
- }
- }
- // Function to save a matrix to a text file
- void saveMatrixToFile(const vector<vector<int>>& matrix, const string& filename) {
- ofstream writer(filename);
- if (!writer.is_open()) {
- cout << "Error: Unable to save result matrix to file." << endl;
- return;
- }
- //// writer << matrix.size() << " " << matrix[0].size() << endl;
- for (const auto& row : matrix) {
- for (int value : row) {
- writer << value << " ";
- }
- writer << endl;
- }
- writer.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement