• c语言中求两个矩阵的乘积


    1、

    #include <stdio.h>
    
    int main(void)
    {
        int a[4][3] = {{2,5,4},{1,2,4},{5,3,2},{4,2,6}};
        int b[3][4] = {{2,1,6,2},{3,2,4,1},{5,2,1,2}};
        int i, j, k;
        int c[4][4] = {0};
        
        for (i = 0; i < 4; i++)
        {
            for (k = 0; k < 4; k++)
            {
                for (j = 0; j < 3; j++)
                {
                    c[i][k] += a[i][j] * b[j][k];
                }
            }
        }
        
        for (i = 0; i < 4; i++)
        {
            for (j = 0; j < 4; j++)
            {
                printf("%d ", c[i][j]);
            }
            putchar('\n');
        }
        return 0;
    } 

     

    #include <stdio.h>
    
    int main(void)
    {
        int a[4][3], b[3][4];
        int i, j;
        puts("please input the elements of 4 * 3 matrix.");
        for (i = 0; i < 4; i++)
        {
            for (j = 0; j < 3; j++)
            {
                printf("a[%d][%d] =  ", i, j); scanf("%d", &a[i][j]);
             } 
        }
        
        puts("please input the elements of 3 * 4 matrix.");
        for (i = 0; i < 3; i++)
        {
            for (j = 0; j < 4; j++)
            {
                printf("b[%d][%d] = ", i, j); scanf("%d", &b[i][j]);
            }
        }
        
        
        int k;
        int c[4][4] = {0};
        for (i = 0; i < 4; i++)
        {
            for (k = 0; k < 4; k++)
            {
                for (j = 0; j < 3; j++)
                {
                    c[i][k] += a[i][j] * b[j][k];
                }
            }
        }
        puts("output the multiply of the two matrix.");
        for (i = 0; i < 4; i++)
        {
            for (j = 0; j < 4; j++)
            {
                printf("%d ", c[i][j]);
            }
            putchar('\n');
        }
        return 0;
    }

    ↓ 

    #include <stdio.h>
    
    int main(void)
    {
        int a[2][4], b[4][5];
        int i, j;
        puts("please input the elements of the 2 * 4 matrix");
        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 4; j++)
            {
                printf("a[%d][%d] = ", i, j); scanf("%d", &a[i][j]);
            }
        }
        puts("please input the elements of the 4 * 5 matrix");
        for (i = 0; i < 4; i++)
        {
            for (j = 0; j < 5; j++)
            {
                printf("b[%d][%d] = ", i, j); scanf("%d", &b[i][j]);
            }
        }
        int c[2][5] = {0};
        int k;
        for (i = 0; i < 2; i++)
        {
            for (k = 0; k < 5; k++)
            {
                for (j = 0; j < 4; j++)
                {
                    c[i][k] += a[i][j] * b[j][k];
                }
            }
        }
        puts("output the multiply of the two matrix.");
        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 5; j++)
            {
                printf("%d ", c[i][j]);
            }
            putchar('\n');
        }
        return 0;
    }

  • 相关阅读:
    JavaScript
    HTML
    DockerDesktop安装mysql
    c语言ld returned 1 exit status😂
    eclipse项目导入idea jdk版本不一致😵
    markdown语法
    【Kali】Kali Linux更新5.2.9后 Vmware Workstation无法正常启动
    【NetDevOps】网络自动化运维--1获取用户基本信息
    【NetDevops】网络自动化运维--1获取用户基本信息
    Kali Linux中安装Python虚拟环境
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14380289.html
Copyright © 2020-2023  润新知