📄 矩阵相乘小函数.txt
字号:
矩阵相乘小函数[原创]
随便写了个函数,用来计算机矩阵相乘。其实呢,这三个矩阵不用全都是方阵,如果first矩阵为m×n,second矩阵为n×k,则third矩阵为m×k即可。
#define N 5 /*to compute the product of two matrix*/
#i nclude <conio.h>
void init(int p[N][N])/*initialize the matrix*/
{
int row,col;
for(row=0;row<N;row++)
for(col=0;col<N;col++)
p[row][col]=1;
}
void compute(int p[N][N],int q[N][N],int result[N][N])
{ /*compute the product of two matrix p and q to result*/
int row,col,temp;
int sum;
for(row=0;row<N;row++)
for(col=0;col<N;col++)
{
sum=0;
for(temp=0;temp<N;temp++)
sum=sum+p[row][temp]*q[temp][col];
result[row][col]=sum;
}
}
void output(int p[N][N]) /*print the matrix p*/
{
int row,col;
for(row=0;row<N;row++)
{
printf("\n");
for(col=0;col<N;col++)
printf("%4d",p[row][col]);
}
}
void main()
{
int first[N][N],second[N][N],third[N][N];
init(first);
init(second);
init(third);
compute(first,second,third);
clrscr();
printf("\nThe first matrix is:......\n");
output(first);
printf("\nThe second matrix is:......\n");
output(second);
printf("\nThe third matrix is:........\n");
output(third);
printf("\nBye..........Press any key to return");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -