C语言动态数组在矩阵相乘中的应用

本文运用动态数组技术编制了C语言矩阵相乘的程序.

  1: #include <stdio.h>
  2: #include <stdlib.h>
  3: 
  4: main()
  5: {
  6:    int i,j,k,sss,**p1,**p2,**p3;
  7:    
  8:    /* Request the first block of heap memory for the first matrix.*/
  9:    p1=(int**)malloc(2*sizeof(int*));
 10:    for(i=0;i<=1;i++)
 11:    {
 12:       p1[i]=(int*)malloc(3*sizeof(int));
 13:    }
 14:    
 15:    /* Request the second block of heap memory for the second matrix.*/
 16:    p2=(int**)malloc(3*sizeof(int*));
 17:    for(i=0;i<=2;i++)
 18:    {
 19:       p2[i]=(int*)malloc(2*sizeof(int));
 20:    }
 21: 
 22:    /* Request the third block of heap memory for the third matrix */
 23:    p3=(int**)malloc(3*sizeof(int*));
 24:    for(i=0;i<=2;i++)
 25:    {
 26:       p3[i]=(int*)malloc(3*sizeof(int));
 27:    }
 28: 
 29:    /* Assign the first matrix. */
 30:    printf("The first matirx:\n");
 31:    for(i=0;i<=1;i++)
 32:    {
 33:       for(j=0;j<=2;j++)
 34:       {
 35:          p1[i][j]=(i+1)*10+(j+1);
 36:          printf("%d\t",p1[i][j]);
 37:       }
 38:       printf("\n");
 39:    }
 40: 
 41:    printf("The second matirx:\n");
 42:    /* Assign the second matrix. */
 43:    for(i=0;i<=2;i++)
 44:    {
 45:       for(j=0;j<=1;j++)
 46:       {
 47:          p2[i][j]=(i+1)*10+(j+1)*1;
 48:          printf("%d\t",p2[i][j]);
 49:       }
 50:       printf("\n");
 51:    }
 52:    
 53:    printf("The product:\n");
 54:    /* Calculate the product of the two matrice. */
 55:    for(i=0;i<=1;i++)
 56:    {
 57:       for(j=0;j<=1;j++)
 58:       {
 59:          sss=0;
 60:          for(k=0;k<=2;k++)
 61:          {
 62:             sss=sss+(p1[i][k])*(p2[k][j]);
 63:          }
 64:          p3[i][j]=sss;
 65:          printf("%d\t",p3[i][j]);
 66:       }
 67:       printf("\n");
 68:    } 
 69: }
 70: 

运行结果:

The first matirx:
11    12    13   
21    22    23   
The second matirx:
11    12   
21    22   
31    32   
The product:
776    812   
1406    1472   

转载于:https://my.oschina.net/u/588967/blog/76879

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值