the multiplication of two matrix in a simple way

The results: 

The matrix a:
   2   5
   7   7
The matrix b:
   7   1
   6   9
The result matrix:
  44  47
  91  70

The codes:

//get the multiplication of two matrix
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
const int n = 2;
void initialization_matrix(int a[][n]);
void multiplication_matrix(int a[][n], int b[][n], int c[][n]);
void disp(int a[][n]);
int main()
{
    srand((unsigned)time(NULL));
    int a[n][n], b[n][n], c[n][n];
    //give random numbers range from 1 to 10 to matrix a and b
    initialization_matrix(a);
    cout << "The matrix a:"<< endl;
    disp(a);
    initialization_matrix(b);
    cout << "The matrix b:" << endl;
    disp(b);
    //get the result of multiplication between a[][n] and b[][n]
    for(int i = 0; i<n; ++i){
        for(int j = 0; j < n; ++j){
            c[i][j] = 0;
        }
    }
    multiplication_matrix(a, b, c);
    //display the elements in matrix c
    cout << "The result matrix:" << endl;
    disp(c);
}
void initialization_matrix(int a[][n])
{
    for(int i = 0; i<n; ++i){
        for(int j = 0; j < n; ++j){
            a[i][j] = rand()%10 + 1;
        }
    }
}
void disp(int a[][n])
{
    for(int i = 0; i<n; ++i){
        for(int j = 0; j < n; ++j){
           cout<< setw(4)<< a[i][j];
        }
        cout << endl;
    }
}
void multiplication_matrix(int a[][n], int b[][n], int c[][n])
{
    for(int i =0; i<n;++i){
        for(int j =0; j <n; ++j){
              for(int k=0; k<n; ++k){
                    c[i][j] += a[i][k]*b[k][j];
            }
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值