how to take input and add ,subtract , multiply and divide the two matrices? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

how to take input and add ,subtract , multiply and divide the two matrices?

20th Jan 2017, 2:44 PM
Ajay Choudhury
Ajay Choudhury - avatar
3 Answers
+ 2
include <iostream> using namespace std; int main() { int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k; cout << "Enter rows and columns for first matrix: "; cin >> r1 >> c1; cout << "Enter rows and columns for second matrix: "; cin >> r2 >> c2; while (c1!=r2) { cout << "Error! column of first matrix not equal to row of second."; cout << "Enter rows and columns for first matrix: "; cin >> r1 >> c1; cout << "Enter rows and columns for second matrix: "; cin >> r2 >> c2; } cout << endl << "Enter elements of matrix 1:" << endl; for(i = 0; i < r1; ++i) for(j = 0; j < c1; ++j) { cout << "Enter element a" << i + 1 << j + 1 << " : "; cin >> a[i][j]; } cout << endl << "Enter elements of matrix 2:" << endl; for(i = 0; i < r2; ++i) for(j = 0; j < c2; ++j) { cout << "Enter element b" << i + 1 << j + 1 << " : "; cin >> b[i][j]; } for(i = 0; i < r1; ++i) for(j = 0; j < c2; ++j) mult[i][j]=0; //This is what you need for(i = 0; i < r1; ++i) for(j = 0; j < c2; ++j) for(k = 0; k < c1; ++k) mult[i][j] += a[i][k] * b[k][j]; cout << endl << "Output Matrix: " << endl; for(i = 0; i < r1; ++i) for(j = 0; j < c2; ++j) { cout << " " << mult[i][j]; if(j == c2-1) cout << endl; } return 0; }
1st Mar 2017, 6:26 AM
Megatron
Megatron - avatar
+ 1
divide two matrices it is not possible mathematically (from my knowledge) then how can anyone help you. you can multiply by inverse but cannot divide two matrices.
20th Jan 2017, 2:49 PM
Megatron
Megatron - avatar
0
i require the multiplicatiion method plzz
28th Feb 2017, 7:23 PM
Ajay Choudhury
Ajay Choudhury - avatar