Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 矩阵乘法_C_Arrays_Matrix - Fatal编程技术网

C 矩阵乘法

C 矩阵乘法,c,arrays,matrix,C,Arrays,Matrix,我不明白哪里出了错。请帮我改正一下。结果矩阵的所有元素都为零时输出 #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");

我不明白哪里出了错。请帮我改正一下。结果矩阵的所有元素都为零时输出

 #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();
}
#包括
#包括
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(“输入第一个矩阵的行数和列数:\n”);
scanf(“%d%d”,&row1,&col1);
printf(“输入第二个矩阵的行数和列数:\n”);
scanf(“%d%d”、&row2、&col2);
if(col1==row2)
{
第3行=第1行;
col3=col2;
}
其他的
{
printf(“不可能!”);
出口(1);
}
printf(“输入第一个矩阵的元素:\n”);

对于(i=0;i您在列循环之外设置结果,因此每行只设置一个结果。通过在大括号内移动这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;
     }
}

您忘记在添加两个矩阵的循环中初始化i和j

根据您的代码添加

i=0;j=0在双for循环内进行加法


希望这有帮助

但是shweta,在我开始乘法过程之前,我已经写了I=0,j=0。Sahil:随着循环的进行,I和j不断增加,并且超过矩阵的界限,条件变得不满意。请在开始之前使用它。试着让我知道。很抱歉,它给出了垃圾值。加法公式a也需要更改。你能解释一下是什么导致了公式的更改吗?在内部循环中,你必须将a[][]的k行与b[][]的l列相乘。因此a[][]的行索引必须是k(不是i),b[][]的列索引必须是l(不是j)。
s=s+a[k][j++]*b[i++][l];