要求:编写程序, 输入两个3行3列矩阵,对输入矩阵做相乘操作,用行指针实现。
效果:
代码:
#include "stdio.h"
void multiplication(int (*p)[3],int (*q)[3]);//矩阵相乘
void main()
{
int a[3][3],b[3][3],i,j;
int (*p)[3]=a,(*q)[3]=b;
printf("请输入a矩阵:\n");
for( i=0;i<3;i++)
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
printf("请输入b矩阵:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
printf("矩阵a*b:\n");
multiplication(p,q);
}
void multiplication(int (*p)[3],int (*q)[3])
{
int i,j;
int k;
int sum;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum=sum+(*(p[i]+k))*(*(q[j]+k));
}
printf("\t%d ",sum);
}
printf("\t\n");
}
}