Answers for "matrix addition and multiplication in c+"

C++
0

matrix addition and multiplication in c+

#include<bits/stdc++.h>
 
using namespace std;
 
int main() {
 
    int a[2][2] = {{1, 2},
                   {3, 4}};
    int b[2][2] = {{5, 6},
                   {7, 8}};
    int mul[2][2];
    int sum[2][2];
 
    //sum
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            sum[i][j] = a[i][j]+b[i][j];
        }
    }
    //print summ
    cout<<"matrix addition: \n";
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            cout << sum[i][j] << "    ";
        }
        cout << endl;
    }
 
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            mul[i][j] = 0;
            for (int k = 0; k < 2; k++) {
                mul[i][k] += a[i][j] * b[j][k];
            }
        }
    }
 
    //print mul
    cout<<"matrix multiplication: \n";
 
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            cout << mul[i][j] << "    ";
        }
        cout << endl;
    }
 
    return 0;
}
Posted by: Guest on October-24-2021

Code answers related to "matrix addition and multiplication in c+"

Browse Popular Code Answers by Language