C++ Online Compiler
Example: Matrix Operations (Addition, Subtraction, Trace) in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Matrix Operations (Addition, Subtraction, Trace) #include <iostream> #include <vector> // Using std::vector for dynamic matrices using namespace std; // Function to get matrix dimensions from user void getMatrixDimensions(int& rows, int& cols) { cout << "Enter number of rows: "; cin >> rows; cout << "Enter number of columns: "; cin >> cols; } // Function to get matrix elements from user void getMatrixInput(vector<vector<int>>& matrix, int rows, int cols, const string& name) { cout << "Enter elements for " << name << " (" << rows << "x" << cols << "):\n"; matrix.resize(rows, vector<int>(cols)); // Resize vector to match dimensions for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { cout << name << "[" << i << "][" << j << "]: "; cin >> matrix[i][j]; } } } // Function to print a matrix void printMatrix(const vector<vector<int>>& matrix, const string& name) { cout << "\n" << name << ":\n"; for (int i = 0; i < matrix.size(); ++i) { for (int j = 0; j < matrix[0].size(); ++j) { cout << matrix[i][j] << "\t"; } cout << "\n"; } } // Function to add two matrices vector<vector<int>> addMatrices(const vector<vector<int>>& mat1, const vector<vector<int>>& mat2, int rows, int cols) { vector<vector<int>> result(rows, vector<int>(cols)); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { result[i][j] = mat1[i][j] + mat2[i][j]; } } return result; } // Function to subtract two matrices vector<vector<int>> subtractMatrices(const vector<vector<int>>& mat1, const vector<vector<int>>& mat2, int rows, int cols) { vector<vector<int>> result(rows, vector<int>(cols)); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { result[i][j] = mat1[i][j] - mat2[i][j]; } } return result; } // Function to calculate the trace of a matrix int calculateTrace(const vector<vector<int>>& matrix, int rows) { int trace = 0; for (int i = 0; i < rows; ++i) { trace += matrix[i][i]; // Sum main diagonal elements } return trace; } int main() { // Step 1: Declare variables for matrix dimensions and matrices int rowsA, colsA; int rowsB, colsB; vector<vector<int>> matrixA; vector<vector<int>> matrixB; int choice; // Step 2: Get dimensions and elements for Matrix A cout << "--- Enter details for Matrix A ---\n"; getMatrixDimensions(rowsA, colsA); getMatrixInput(matrixA, rowsA, colsA, "Matrix A"); printMatrix(matrixA, "Matrix A"); // Step 3: Get dimensions and elements for Matrix B cout << "\n--- Enter details for Matrix B ---\n"; getMatrixDimensions(rowsB, colsB); getMatrixInput(matrixB, rowsB, colsB, "Matrix B"); printMatrix(matrixB, "Matrix B"); // Step 4: Display operation menu and get user choice cout << "\n--- Choose an operation ---\n"; cout << "1. Add Matrices (A + B)\n"; cout << "2. Subtract Matrices (A - B)\n"; cout << "3. Calculate Trace of Matrix A\n"; cout << "Enter your choice (1-3): "; cin >> choice; // Step 5: Perform the chosen operation based on user input switch (choice) { case 1: // Addition if (rowsA == rowsB && colsA == colsB) { vector<vector<int>> sumMatrix = addMatrices(matrixA, matrixB, rowsA, colsA); printMatrix(sumMatrix, "Sum (A + B)"); } else { cout << "Error: Matrices must have the same dimensions for addition.\n"; } break; case 2: // Subtraction if (rowsA == rowsB && colsA == colsB) { vector<vector<int>> diffMatrix = subtractMatrices(matrixA, matrixB, rowsA, colsA); printMatrix(diffMatrix, "Difference (A - B)"); } else { cout << "Error: Matrices must have the same dimensions for subtraction.\n"; } break; case 3: // Trace if (rowsA == colsA) { int traceA = calculateTrace(matrixA, rowsA); cout << "\nTrace of Matrix A: " << traceA << "\n"; } else { cout << "Error: Matrix A must be a square matrix to calculate its trace.\n"; } break; default: cout << "Invalid choice. Please enter a number between 1 and 3.\n"; break; } return 0; }
Output
Clear
ADVERTISEMENTS