0

我正在寻找一种将 2x1 和 2x2 矩阵相乘的方法,但我的代码几乎每次都不起作用,即使它确实起作用,我也必须将数字放入代码中。如果我在运行代码时尝试询问数字,那就不行了

到目前为止,这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int main(void)  
{  
  unsigned int a[1][2],b[2][2],c[2][1], i=0, j=0, k=0;  
  printf("-=Program to Multiply two Matrices=-");  


//--------------------------------------------------------------------  

 printf("\nEnter the values of Matrix A: \n");  

  for(i=0;i<1;i++)  
    {  
      for(j=0;j<2;j++)  
        {  
         // printf("\n"); 
          scanf("%d",&a[i][j]);  
        }  
     }  
   printf("\nMatrix A: ");  
   for(i=0;i<1;i++)  
    {  
      printf("\n");  
      for(j=0;j<2;j++)  
        {  
          printf(" ");  
          printf("%d",a[i][j]);  
        }  
     }  
//----------------------------------------------------  

printf("\nEnter the values of Matrix B: \n");  
  for(i=0;i<2;i++)  
    {  
      for(j=0;j<2;j++)  
        {  
         // printf("\n");  
          scanf("%d",&b[i][j]);  
        }  
     }  
   printf("\nMatrix B: ");  
   for(i=0;i<2;i++)  
    {  
      printf("\n");  
      for(j=0;j<2;j++)  
        {  
          printf(" ");  
          printf("%d",b[i][j]);  
        }  
     }  

//--------------------------------------------------------  

  printf("\n\nMultiplication of matrices A and B is : \n\n");  
  for(i=0;i<2;i++)  
   {  
     for(j=0;j<2;j++)  
       {  
          c[i][j]=0;  
          for(k=0;k<1;k++)  
            {  
        c[i][j]=c[i][j]+(a[i][k]*b[k][j]);  
            }  
       }  
   }  
  printf("\nMatrix C (Resultant Matrix): ");  
    for(i=0;i<2;i++)  
    {  
      printf("\n");  
      for(j=0;j<1;j++)  
        {  
          printf(" ");  
          printf("%d",c[i][j]);  
        }  
     }  
printf("\n");  
  return 0;  
}  
4

2 回答 2

1

Basic Concept of multiplication of matrices is 1st Matrix Column must be equal to Row of 2nd matrix

Example:

//valid since column of A is equal to row of B i.e. 2

Matrix A = 1 X 2
Matrix B = 2 X 2

//Invalid (Your Case)
Matrix A = 2 X 1
Matrix B = 2 X 2

Matrix Multiplication image

于 2011-03-05T16:06:15.083 回答
0

就矩阵乘法而言,只有第一个矩阵的列数与第二个矩阵的行数匹配才有可能,所以这样做没有用,整个概念都是错误的

于 2014-01-28T16:25:40.953 回答