简体   繁体   English

矩阵乘法

[英]Matrix multiplication

I can't understand where is the mistake. 我不明白哪里出了错。 Help me correct it please. 请帮我改正。 The output is coming as all the elements of resultant matrix being zero. 由于结果矩阵的所有元素均为零,因此输出即将到来。

 #include<stdio.h>
#include<conio.h>
int main()
{
    int a[5][5],b[5][5],c[5][5],i=0,j=0,row1,col1,row2,col2,row3,col3,s=0,k=0,l=0;
    printf("Enter no. of rows and no. of columns of first matrix:\n");
    scanf("%d %d",&row1,&col1);
    printf("Enter no. of rows and no. of columns of second matrix:\n");
    scanf("%d %d",&row2,&col2);
    if(col1==row2)
    {
                  row3=row1;
                  col3=col2;
    }
    else
    {
        printf("Not possible!");
        exit(1);
    }

    printf("Enter elements of first matrix:\n");
    for(i=0;i<row1;i++)
    {
                       for(j=0;j<col1;j++)
                       {
                                          scanf("%d",&a[i][j]);
                       }
    }


        printf("Enter elements of second matrix:\n");
    for(i=0;i<row2;i++)
    {
                       for(j=0;j<col2;j++)
                       {
                                          scanf("%d",&b[i][j]);
                       }
    }

    i=0;
    j=0;

    for(k=0;k<row3;k++)
    {
                       for(l=0;l<col3;l++)
                       {

                                          while(i<row3 || j<col3)
                                          {
                                                       //printf("Hi");
                                                       s=s+a[i][j++]*b[i++][j];
                                                       //printf("%d\n",s);

                                          }

                       }
                       printf("%d\n",s);
                       c[k][l]=s;
                        s=0;
    }


    printf("Sum matrix is:\n");
        for(k=0;k<row3;k++)
    {
                       for(l=0;l<col3;l++)
                       {
                                          printf("%d ",c[k][l]);
                       }
                       printf("\n");
    }
    getch();
}

I have included comments of printing in the while loop so as to debug but it's not helping. 我在while循环中包含了打印注释,以便进行调试,但无济于事。

You are setting the result outside of your column loop, so you only set one result per row. 您正在列循环之外设置结果,因此每行只设置一个结果。 Change the code to this by moving those 3 lines inside the brace: 通过在大括号内移动这3行,将代码更改为此:

for(k=0;k<row3;k++)
{
      for(l=0;l<col3;l++)
      {
          i = 0; j = 0;
          while(i<row3 || j<col3)
          {
              //printf("Hi");
              s=s+a[k][j++]*b[i++][l];
              //printf("%d\n",s);
           }

           // THIS CODE HAS MOVED:
           printf("%d\n",s);
           c[k][l]=s;
           s=0;
     }
}

Also, your addition needs to use the k and l indices, so that you move along a row of a[][] given by k and a column of b[][] given by l: 另外,您的加法需要使用k和l索引,以便沿着k给定的a [] []行和l给定的b [] []列移动:

s=s+a[k][j++]*b[i++][l];

You forgot to initialize i and j inside the loop where you add the two matrices. 您忘记在将两个矩阵相加的循环内初始化i和j。

According to your code add. 根据您的代码添加。

i=0; i = 0; j=0 inside the double for loop for addition. 在用于加法的double for循环内j = 0。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM