⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 矩阵相乘小函数.txt

📁 c语言的一些常见的算法以及思考和改进的文章,写的很不错,花费了很大的精力从网络了搜罗的,希望大家喜欢.
💻 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 + -