zoukankan      html  css  js  c++  java
  • C语言 矩阵的转置及矩阵的乘法

     C语言 矩阵的转置及矩阵的乘法

    //凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/

    1.矩阵的转置

     1 #include<stdio.h>
     2 #define N 2
     3 #define M 3
     4 void main(){
     5     int i,j,a[N][M],b[M][N];
     6     //从键盘输入矩阵a
     7     for(i=0;i<N;i++){
     8         for(j=0;j<M;j++){
     9             printf("a[%d][%d]= ",i,j);
    10             scanf("%d",&a[i][j]);
    11         }
    12     }
    13     //输出矩阵a
    14     printf("Array a:
    ");
    15     for(i=0;i<N;i++){
    16         for(j=0;j<M;j++){
    17             printf("%5d",a[i][j]);
    18             b[j][i]=a[i][j];
    19         }
    20         printf("
    ");
    21     }
    22     //输出矩阵b
    23     printf("Array b:
    ");
    24     for(i=0;i<M;i++){
    25         for(j=0;j<N;j++){
    26             printf("%5d",b[i][j]);
    27         }
    28         printf("
    ");
    29     }
    30 
    31 }

    结果为:

    2.矩阵的乘法运算

     1 #include<stdio.h>
     2 #define M 10
     3 void main(){
     4     long int a[M][M], b[M][M], c[M][M]={0};
     5     int m, n, p, i, j, k;
     6 
     7     printf("Please input 3 numbers:
    ");
     8     scanf("%d %d %d",&m, &n, &p);
     9 
    10     printf("Please input array A (%d * %d)
    ", m, n);
    11     for(i=0;i<m;i++){
    12         for(j=0;j<n;j++){
    13             scanf("%ld", &a[i][j]);
    14         }
    15     }
    16 
    17     printf("Plaese input array B (%d * %d)
    ", n, p);
    18     for(i=0;i<n;i++){
    19         for(j=0;j<p;j++){
    20             scanf("%ld", &b[i][j]);
    21         }
    22     }
    23     //A*B=C
    24     for(i=0;i<m;i++){
    25         for(j=0;j<p;j++){
    26             for(k=0;k<n;k++){
    27                 c[i][j]=c[i][j]+a[i][k]*b[k][j];
    28             }
    29         }
    30     }
    31 
    32     printf("A*B= 
    ");
    33     for(i=0;i<m;i++){
    34         for(j=0;j<p;j++){
    35             printf("%3ld",c[i][j]);
    36         }
    37         printf("
    ");
    38     }
    39 
    40 }

    结果为:

  • 相关阅读:
    求数组中的最小子数组,时间复杂度o(n),java
    第四周进度条
    四则混合运算3
    软件工程作业3
    《构建之法》第三周阅读笔记
    第三周学习进度
    学习进度01
    构建之法阅读笔记01
    构建之法问题
    随机生成题目运算
  • 原文地址:https://www.cnblogs.com/kailugaji/p/8594667.html
Copyright © 2011-2022 走看看