简体   繁体   中英

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.

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:

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:

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

You forgot to initialize i and j inside the loop where you add the two matrices.

According to your code add.

i=0; j=0 inside the double for loop for addition.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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