CPP中实现矩阵乘法

 

001、

root@ubuntu26:/home/test01/cpp# cat test01.cpp         ## 测试程序
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int ay1[2][3] = {{1,0,2},{3,2,4}};                // 分别定义两个二维数组
    int ay2[3][2] = {{0,4},{2,6},{2,1}};
    int ay3[2][2];
    int i,j,k;

    for(i = 0; i < 2; i++)
    {
        for(j = 0; j < 2; j++)
        {
            ay3[i][j] = 0;
            for(k = 0; k < 3; k++)
            {
                ay3[i][j] = ay3[i][j] + ay1[i][k] * ay2[k][j];     // 利用循环条件计算矩阵每一个元素
            }
        }
    }

    for(i = 0; i < 2; i++)
    {
        for(j = 0; j < 2; j++)
        {
            cout  << ay3[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}
root@ubuntu26:/home/test01/cpp# g++ test01.cpp -Wall -O2 -o kkk     ## 编译
root@ubuntu26:/home/test01/cpp# ls
kkk  test01.cpp
root@ubuntu26:/home/test01/cpp# ./kkk
4 6
12 28

 

image

 。

 

posted @ 2026-11-07 17:43  小鲨鱼2018  阅读(7)  评论(0)    收藏  举报