第一行在使用 C 的矩阵乘积中不起作用
First row doesnt work in matrix product using C
我是 C 的初学者,在过去的几个小时里我一直被这个特定问题所困扰。我需要乘以 2 个矩阵,但据我所知,代码尽管是正确的,但不适用于结果矩阵的第一行
请不要在没有至少检查整个问题一次的情况下将其标记为重复;我真的需要帮助。
例如
但我的代码输出的是这个
代码:
// Multiply two matrices
# include <stdio.h>
int main()
{
// declare itertives and matrices
int i, j, k, rows0, cols0, rows1, cols1, matrix0[10][10], matrix1[10][10], matrix2[10][10];
printf("Number of Rows and columns shouldn't exceed 10\n");
printf("Enter number of rows and columns for first Matrix\n");
printf("Enter number of rows: ");
scanf("%d", &rows0);
printf("Enter number of columns: ");
scanf("%d", &cols0);
printf("Enter number of rows and columns for second Matrix\n");
printf("Enter number of rows: ");
scanf("%d", &rows1);
printf("Enter number of columns: ");
scanf("%d", &cols1);
if (cols0!=rows1)
{
printf("Number of columns in first matrix should be equal to number of rows in second matrix\n");
return 0;
}
printf("Input values into first Matrix\n");
for(i = 0; i < rows0; i++)
{
printf("Input values in row %d with each value separated with space\n", i);
for(j=0; j< cols0; j++)
{
scanf("%d", &matrix0[i][j]);
}
}
printf("Input values into second Matrix\n");
for(i = 0; i < rows1; i++)
{
printf("Input values in row %d with each value separated with space\n", i);
for(j=0; j< cols1; j++)
{
scanf("%d", &matrix1[i][j]);
}
}
// Matrix multiplication
for (i=0; i<rows0; i++)
{
matrix2[i][j]=0;
for (j=0; j< cols1; j++)
{
for(k=0;k<cols1;k++)
{
matrix2[i][j]+=(matrix0[i][k]*matrix1[k][j]);
}
}
}
printf("Resultant matrix after matrix multiplication is\n");
for(i=0; i < rows0; i++)
{
for(j=0; j<cols1; j++)
{
printf("%d ", matrix2[i][j]);
}
printf("\n");
}
}
输出
matrix2[i][j]=0;
不合适。你欠我 60 秒的调试时间:-)
我是 C 的初学者,在过去的几个小时里我一直被这个特定问题所困扰。我需要乘以 2 个矩阵,但据我所知,代码尽管是正确的,但不适用于结果矩阵的第一行
请不要在没有至少检查整个问题一次的情况下将其标记为重复;我真的需要帮助。
例如
但我的代码输出的是这个
代码:
// Multiply two matrices
# include <stdio.h>
int main()
{
// declare itertives and matrices
int i, j, k, rows0, cols0, rows1, cols1, matrix0[10][10], matrix1[10][10], matrix2[10][10];
printf("Number of Rows and columns shouldn't exceed 10\n");
printf("Enter number of rows and columns for first Matrix\n");
printf("Enter number of rows: ");
scanf("%d", &rows0);
printf("Enter number of columns: ");
scanf("%d", &cols0);
printf("Enter number of rows and columns for second Matrix\n");
printf("Enter number of rows: ");
scanf("%d", &rows1);
printf("Enter number of columns: ");
scanf("%d", &cols1);
if (cols0!=rows1)
{
printf("Number of columns in first matrix should be equal to number of rows in second matrix\n");
return 0;
}
printf("Input values into first Matrix\n");
for(i = 0; i < rows0; i++)
{
printf("Input values in row %d with each value separated with space\n", i);
for(j=0; j< cols0; j++)
{
scanf("%d", &matrix0[i][j]);
}
}
printf("Input values into second Matrix\n");
for(i = 0; i < rows1; i++)
{
printf("Input values in row %d with each value separated with space\n", i);
for(j=0; j< cols1; j++)
{
scanf("%d", &matrix1[i][j]);
}
}
// Matrix multiplication
for (i=0; i<rows0; i++)
{
matrix2[i][j]=0;
for (j=0; j< cols1; j++)
{
for(k=0;k<cols1;k++)
{
matrix2[i][j]+=(matrix0[i][k]*matrix1[k][j]);
}
}
}
printf("Resultant matrix after matrix multiplication is\n");
for(i=0; i < rows0; i++)
{
for(j=0; j<cols1; j++)
{
printf("%d ", matrix2[i][j]);
}
printf("\n");
}
}
输出
matrix2[i][j]=0;
不合适。你欠我 60 秒的调试时间:-)