zoukankan      html  css  js  c++  java
  • c语言中计算矩阵的乘积

    c语言中计算矩阵的乘积。

    矩阵相乘的条件:左侧矩阵的列数等于右侧矩阵的行数。

    矩阵相乘的结果:行数为左侧矩阵的行数,列数为右侧矩阵的列数。

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

  • 相关阅读:
    2018.4.26 lvm
    2018.4.25 github创建新项目
    2018.4.24 快排查找第K大
    2018.4.24 flask_mail使用
    SpringBoot中使用ES和MongoDB常用API
    设计模式-Template_Method模式
    设计模式-Chain of Responsibility模式
    设计模式-Observer模式
    设计模式-Adapter模式
    设计模式-Strategy模式
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14727072.html
Copyright © 2011-2022 走看看