{"id":7017,"date":"2021-08-16T12:21:51","date_gmt":"2021-08-16T06:51:51","guid":{"rendered":"https:\/\/python-programs.com\/?p=7017"},"modified":"2021-11-22T18:39:25","modified_gmt":"2021-11-22T13:09:25","slug":"program-for-addition-of-two-matrices-in-python-cpp","status":"publish","type":"post","link":"https:\/\/python-programs.com\/program-for-addition-of-two-matrices-in-python-cpp\/","title":{"rendered":"Program for addition of two matrices in Python & C++ Programming"},"content":{"rendered":"
In the previous article, we have discussed about\u00a0C++11 Multithreading \u2013 Part 8: std::future , std::promise and Returning values from Thread<\/a>. Let us learn Program for addition of two matrices in C++ Program and Python.<\/p>\n What is a matrix:<\/strong><\/p>\n A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.<\/p>\n Example:<\/strong><\/p>\n <\/p>\n Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.<\/p>\n In this order, the dimensions of a matrix indicate the number of rows and columns.<\/p>\n Here as there are 5 rows and 4 columns it is called as 5*4 matrix.<\/p>\n What is matrix addition:<\/strong><\/p>\n Given two matrices of same order(dimensions) then we can easily add the two matrices by doing the sum of corresponding elements in both the matrices.<\/p>\n Example:<\/strong><\/p>\n <\/p>\n Here Matrix C is matrix addition of the matrices A and B.<\/p>\n Examples for matrix addition:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n A matrix can be implemented as a nested list in Python (list inside a list). Each variable can be thought of as a row in the matrix.<\/p>\n We used nesting loops in this program\u00a0to iterate through and row and column. In the two matrices we will add the appropriate elements and store them in the result at each level.<\/p>\n Here we give the matrix input as static.<\/p>\n Below is the\u00a0 implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The program’s output is the same as previously. For iterating through each element in the array, we used the nested list comprehension.<\/p>\n Understanding the list helps us to write concise codes and we need to always try to use them in Python. They’re really beneficial.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We used nesting loops in this program\u00a0to iterate through and row and column. In the two matrices we will add the appropriate elements and store them in the result at each level.<\/p>\n Let us take dynamic input in this case.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n In the previous article, we have discussed about\u00a0C++11 Multithreading \u2013 Part 8: std::future , std::promise and Returning values from Thread. Let us learn Program for addition of two matrices in C++ Program and Python. What is a matrix: A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or …<\/p>\nMatrix 1 = [ 11 -2 0 ]\r\n [ 4 8 6 ]\r\nMatrix 2 = [ 5 1 -5 ]\r\n [ 2 3 0 ]<\/pre>\n
Printing the sum of matrices : \r\n16 -1 -5\r\n6 11 6<\/pre>\n
Program for matrix Addition<\/h2>\n
\n
Method #1:Using nested Loops in Python<\/h3>\n
# given matrix A\r\nA = [[11, -2, 0],\r\n [4, 8, 6]]\r\n# given matrix B\r\nB = [[5, 1, -5],\r\n [2, 3, 0]]\r\n# Initialize the sum of matrices elements to 0\r\nmatrixSum = [[0, 0, 0],\r\n [0, 0, 0]]\r\n\r\n# Traverse the rows\r\nfor rows in range(len(A)):\r\n # Traverse the columns\r\n for columns in range(len(A[0])):\r\n matrixSum[rows][columns] = A[rows][columns] + B[rows][columns]\r\n# printing the sum of matrices\r\nprint(\"Printing the sum of matrices : \")\r\nfor rows in matrixSum:\r\n print(*rows)\r\n<\/pre>\n
Printing the sum of matrices : \r\n16 -1 -5\r\n6 11 6<\/pre>\n
Method #2:Using List Comprehension in Python<\/h3>\n
# given matrix A\r\nA = [[11, -2, 0],\r\n [4, 8, 6]]\r\n# given matrix B\r\nB = [[5, 1, -5],\r\n [2, 3, 0]]\r\n# using list comprehension\r\nmatrixSum = [[A[i][j] + B[i][j]\r\n for j in range(len(A[0]))] for i in range(len(A))]\r\n# printing the sum of matrices\r\nprint(\"Printing the sum of matrices : \")\r\nfor rows in matrixSum:\r\n print(*rows)\r\n<\/pre>\n
Printing the sum of matrices : \r\n16 -1 -5\r\n6 11 6<\/pre>\n
Method #3:Using nested loops in C++<\/h3>\n
#include <iostream>\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n int rows, columns, A[100][100], B[100][100], sumMatrix[100][100], i, j;\r\n\r\n cout << \"Enter the number of rows of the matrix \"<<endl;\r\n cin >> rows;\r\n\r\n cout << \"Enter the number of columns of the matrix\"<<endl;\r\n cin >> columns;\r\n\r\n cout << \"Enter the elements of first matrix \" << endl;\r\n\r\n \/\/ Initializing matrix A with the user defined values\r\n for(i = 0; i < rows; ++i)\r\n for(j = 0; j < columns; ++j)\r\n {\r\n cout << \"Enter element A\" << i + 1 << j + 1 << \" = \";\r\n cin >> A[i][j];\r\n }\r\n\r\n \/\/ Initializing matrix B with the user defined values\r\n cout << endl << \"Enter elements of 2nd matrix: \" << endl;\r\n for(i = 0; i < rows; ++i)\r\n for(j = 0; j < columns; ++j)\r\n {\r\n cout << \"Enter element B\" << i + 1 << j + 1 << \" = \";\r\n cin >> B[i][j];\r\n }\r\n\r\n \/\/ Performing matrix addition by doing sum of given two matrices A and B\r\n for(i = 0; i < rows; ++i)\r\n for(j = 0; j < columns; ++j)\r\n sumMatrix[i][j] = A[i][j] + B[i][j];\r\n \/\/printing matrix A\r\n cout << endl << \" printing the matrix A\" << endl;\r\n for (i = 0; i < rows; ++i) {\r\n for (j = 0; j < columns; ++j) {\r\n cout << A[i][j] << \" \";\r\n }\r\n cout << endl;\r\n}\r\n \/\/printing matrix B\r\n cout << endl << \" printing the matrix B\" << endl;\r\n for (i = 0; i < rows; ++i) {\r\n for (j = 0; j < columns; ++j) {\r\n cout << B[i][j] << \" \";\r\n }\r\n cout << endl;\r\n}\r\n\r\n \/\/printing the sum of matrices\r\n cout << endl << \" printing the sum of matrices A and B\" << endl;\r\n \r\n for (i = 0; i < rows; ++i) {\r\n for (j = 0; j < columns; ++j) {\r\n cout << sumMatrix[i][j] << \" \";\r\n }\r\n cout << endl;\r\n}\r\n\r\n return 0;\r\n}<\/pre>\n
Enter the number of rows of the matrix \r\n3\r\nEnter the number of columns of the matrix\r\n3\r\nEnter the elements of first matrix \r\nEnter element A11 = 1\r\nEnter element A12 = 2\r\nEnter element A13 = 3\r\nEnter element A21 = 4\r\nEnter element A22 = 5\r\nEnter element A23 = 6\r\nEnter element A31 = 7\r\nEnter element A32 = 8\r\nEnter element A33 = 9\r\n\r\nEnter elements of 2nd matrix: \r\nEnter element B11 = -3\r\nEnter element B12 = 2\r\nEnter element B13 = 5\r\nEnter element B21 = 7\r\nEnter element B22 = 1\r\nEnter element B23 = 0\r\nEnter element B31 = 3\r\nEnter element B32 = 4\r\nEnter element B33 = 6\r\n\r\nprinting the matrix A\r\n1 2 3 \r\n4 5 6 \r\n7 8 9\r\n\r\nprinting the matrix B\r\n-3 2 5 \r\n7 1 0 \r\n3 4 6\r\n\r\nprinting the sum of matrices A and B\r\n-2 4 8 \r\n11 6 6 \r\n10 12 15<\/pre>\n
\n