1711014393

C++ Matrix Sum Challenge


You are challenged to create a C++ program that performs the sum of two matrices. Your task is to implement a function or method that takes two matrices as input, adds them element by element and returns the resulting matrix. 1. Your program should ask the user to enter the number of rows and columns for each matrix. 2. The dimensions of the two matrices must be compatible for the sum, i.e. the number of rows and columns must be the same for both matrices. 3. Once the dimensions have been entered, the program asks the user to enter the elements of each matrix. 4. Once the elements have been entered, the program must calculate the sum of the two matrices. 5. Finally, the program should display the resulting matrix. **Example**: ```cpp Enter the number of rows and columns for the first matrix: 2 3 Enter the elements of the first matrix: 1 2 3 4 5 6 Enter the number of rows and columns for the second matrix: 2 3 Enter the elements of the second matrix: 7 8 9 10 11 12 The sum of the two matrices is 8 10 12 14 16 18 ``` **Notes**: * You can choose to implement the solution as a function or a complete program. * Make sure you handle cases where the dimensions of the matrices are not compatible for the sum. * Test your program with different sets of matrices to ensure that it is working correctly. To publish your answer all you have to do is register and use the tool [<span style="color: blue">here</span>](https://chat-to.dev/createpost). Just one more thing, don't forget to use markdown to style your code.

(3) Comments
jair_messias_bol
jair_messias_bol
0

```cpp #include <iostream> #include <vector> using namespace std; // Function to perform the sum of two matrices vector<vector<int>> addMatrices(const vector<vector<int>>& A, const vector<vector<int>>& B) { int rows = A.size(); int cols = A[0].size(); vector<vector<int>> result(rows, vector<int>(cols, 0)); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { result[i][j] = A[i][j] + B[i][j]; } } return result; } // Function to display the matrix void displayMatrix(const vector<vector<int>>& matrix) { for (const auto& row : matrix) { for (int elem : row) { cout << elem << " "; } cout << endl; } } int main() { // Example matrices for testing vector<vector<int>> matrix1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; vector<vector<int>> matrix2 = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}}; // Adding the matrices vector<vector<int>> result = addMatrices(matrix1, matrix2); // Displaying the resulting matrix cout << "Result of matrix addition:" << endl; displayMatrix(result); return 0; } ``` This code defines a function addMatrices() to add two matrices and a function displayMatrix() to display a matrix. The main program (main()) creates two example matrices, adds them using the addMatrices() function, and displays the resulting matrix.


JavaJuggler
JavaJuggler
0

`Mr. HackerX9` that seems confusing 😕


hackerX9
hackerX9
0

```cpp #include <iostream> #include <vector> using namespace std; vector<vector<int>> addMatrices(vector<vector<int>>& m1, vector<vector<int>>& m2) { if (m1.size() != m2.size() || m1[0].size() != m2[0].size()) return {}; vector<vector<int>> r(m1.size(), vector<int>(m1[0].size())); for (int i = 0; i < m1.size(); i++) for (int j = 0; j < m1[0].size(); j++) r[i][j] = m1[i][j] + m2[i][j]; return r; } int main() { vector<vector<int>> m1 = {{1, 2, 3}, {4, 5, 6}}; vector<vector<int>> m2 = {{7, 8, 9}, {10, 11, 12}}; auto r = addMatrices(m1, m2); for (const auto& row : r) { for (int e : row) cout << e << " "; cout << endl; } return 0; } ``` I've never been very good at this language, but here's my attempt. Please correct me if I have any BUGs.