C++;矩阵乘法不能产生正确的输出 我尝试在C++中增加两个2D数组,但是预期输出不正确,下面是我的逻辑——我的尝试—— int A[l][m]; //creates A l*m matrix or a 2d array. for(int i=0; i<l; i++) //This loops on the rows. { for(int j=0; j<m; j++) //This loops on the columns { A[i][j] = i+j; // Allocating values to A matrix } } int B[m][n]; //creates B m*n matrix or a 2d array. for(int i=0; i<m; i++) //This loops on the rows. { for(int j=0; j<n; j++) //This loops on the columns { B[i][j] = i+j+1; // Allocating values to B matrix } } int C[l][n]; //creates C m*n matrix or a 2d array. for(int i=0; i<l; i++) //This loops on the rows. { for(int j=0; j<n; j++) //This loops on the columns { for(int k=0; k<m; k++) //This loops on the columns { C[i][j] += A[i][k] * B[k][j]; // Allocating values to C matrix //product[row][col] += aMatrix[row][inner] * bMatrix[inner][col]; } cout << C[i][j] << " "; } cout << "\n"; } inta[l][m]//创建l*m矩阵或二维数组。 对于(int i=0;i

C++;矩阵乘法不能产生正确的输出 我尝试在C++中增加两个2D数组,但是预期输出不正确,下面是我的逻辑——我的尝试—— int A[l][m]; //creates A l*m matrix or a 2d array. for(int i=0; i<l; i++) //This loops on the rows. { for(int j=0; j<m; j++) //This loops on the columns { A[i][j] = i+j; // Allocating values to A matrix } } int B[m][n]; //creates B m*n matrix or a 2d array. for(int i=0; i<m; i++) //This loops on the rows. { for(int j=0; j<n; j++) //This loops on the columns { B[i][j] = i+j+1; // Allocating values to B matrix } } int C[l][n]; //creates C m*n matrix or a 2d array. for(int i=0; i<l; i++) //This loops on the rows. { for(int j=0; j<n; j++) //This loops on the columns { for(int k=0; k<m; k++) //This loops on the columns { C[i][j] += A[i][k] * B[k][j]; // Allocating values to C matrix //product[row][col] += aMatrix[row][inner] * bMatrix[inner][col]; } cout << C[i][j] << " "; } cout << "\n"; } inta[l][m]//创建l*m矩阵或二维数组。 对于(int i=0;i,c++,matrix,C++,Matrix,您必须将C[i][j]初始化为0 或者您可以在0处开始求和,然后在以下时间之后分配给C[i][j]: //... for(int j=0; j<n; j++) //This loops on the columns { int sum = 0; for(int k=0; k<m; k++) //This loops on the columns { sum += A[i][k] * B[k][j]; // Allocating values to C m

您必须将
C[i][j]
初始化为0

或者您可以在
0
处开始求和,然后在以下时间之后分配给
C[i][j]

//...
for(int j=0; j<n; j++) //This loops on the columns
{
   int sum = 0;
   for(int k=0; k<m; k++) //This loops on the columns
   {
      sum += A[i][k] * B[k][j]; // Allocating values to C matrix
      //product[row][col] += aMatrix[row][inner] * bMatrix[inner][col];
   }
   C[i][j] = sum;
   cout << C[i][j] << "  ";
}
//...
/。。。

对于(int j=0;j您必须将
C[i][j]
初始化为0

或者您可以在
0
处开始求和,然后在以下时间之后分配给
C[i][j]

//...
for(int j=0; j<n; j++) //This loops on the columns
{
   int sum = 0;
   for(int k=0; k<m; k++) //This loops on the columns
   {
      sum += A[i][k] * B[k][j]; // Allocating values to C matrix
      //product[row][col] += aMatrix[row][inner] * bMatrix[inner][col];
   }
   C[i][j] = sum;
   cout << C[i][j] << "  ";
}
//...
/。。。

对于(int j=0;j您必须将
C[i][j]
初始化为0

或者您可以在
0
处开始求和,然后在以下时间之后分配给
C[i][j]

//...
for(int j=0; j<n; j++) //This loops on the columns
{
   int sum = 0;
   for(int k=0; k<m; k++) //This loops on the columns
   {
      sum += A[i][k] * B[k][j]; // Allocating values to C matrix
      //product[row][col] += aMatrix[row][inner] * bMatrix[inner][col];
   }
   C[i][j] = sum;
   cout << C[i][j] << "  ";
}
//...
/。。。

对于(int j=0;j您必须将
C[i][j]
初始化为0

或者您可以在
0
处开始求和,然后在以下时间之后分配给
C[i][j]

//...
for(int j=0; j<n; j++) //This loops on the columns
{
   int sum = 0;
   for(int k=0; k<m; k++) //This loops on the columns
   {
      sum += A[i][k] * B[k][j]; // Allocating values to C matrix
      //product[row][col] += aMatrix[row][inner] * bMatrix[inner][col];
   }
   C[i][j] = sum;
   cout << C[i][j] << "  ";
}
//...
/。。。
对于(int j=0;j您有未定义的行为:
int C[l][n];
已声明但从未初始化

通常,
memset
用于将内存归零,或者只是循环通过元素并将其归零(使用现代编译器,速度同样快)。您有未定义的行为:
int C[l][n];
已声明但从未初始化

通常,
memset
用于将内存归零,或者只是循环通过元素并将其归零(使用现代编译器,速度同样快)。您有未定义的行为:
int C[l][n];
已声明但从未初始化

通常,
memset
用于将内存归零,或者只是循环通过元素并将其归零(使用现代编译器,速度同样快)。您有未定义的行为:
int C[l][n];
已声明但从未初始化


通常,
memset
用于将内存归零,或者只是循环遍历元素并将其归零(使用现代编译器,速度同样快)。

您还没有将c[i][j]初始化为0。 使用


这将仅初始化数组的第一个元素。但是,如果初始化数组的某些元素,则未初始化的元素将设置为0。

您尚未将c[i][j]初始化为0。 使用


这将仅初始化数组的第一个元素。但是,如果初始化数组的某些元素,则未初始化的元素将设置为0。

您尚未将c[i][j]初始化为0。 使用


这将仅初始化数组的第一个元素。但是,如果初始化数组的某些元素,则未初始化的元素将设置为0。

您尚未将c[i][j]初始化为0。 使用

这将仅初始化数组的第一个元素。但是,如果初始化数组的某些元素,则未初始化的元素将设置为0