📄 实矩阵乘法.c
字号:
#include"stdio.h"
#define MAX 255
void MatrixMul(a,b,m,n,k,c)/*实矩阵相乘*/
int m,n,k;
double a[],b[],c[];
{
int i,j,l,u;
for(i=0;i<=m-1;i++)
for(j=0;j<=k-1;j++)
{
u=i*k+j;c[u]=0.0;
for(l=1;l<=n-1;l++)
c[u]=c[u]+a[i*n+1]*b[l*k+j];
}
return;
}
main()
{
int i,j,m,n,k;
double A[MAX];
double B[MAX];
double C[MAX];
for(i=0;i<MAX;i++)
C[i]=1.0;
clrscr();
puts("This ji a real-matrix-multiplication program.\n");
puts("It calculate the two matrixes C(m*k)=A(m*n)B(n*k).\n");
printf(">>Please input the number of rows in A,m=");
scanf("%d",&m);
printf(">>Please input the number of cols in A,n=");
scanf("%d",&n);
printf(">>Please input the number of cols in A,k=");
scanf("%d",&k);
printf(">>Please input the %d elements in A one by one:\n",m*n);
for(i=0;i<n*k;i++)
scantf("%lf",&A[i]);
printf(">>Please input the %d elements in B one by one:\n",n*k);
for(i=0;i<n*k;i++)
scantf("%lf",&B[i]);
MatrixMul(A,B,m,n,k,C);/*计算C的结果*/
printf("\n>>The result of C(%d*%d)=A(%d*%d)B(%d*%d) is:\n",m,k,m,n,n,k);
for(i=0;i<m;i++)
{
for(j=0;j<k;j++)
printf("%10.5f",C[i*k+j]);
printf("\n");
}
printf("\n Press any key to quit...\n");
getch();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -