您好我有一個關於相乘兩個矩陣相乘兩個矩陣
這個問題是我寫的
#include <stdio.h>
void getData(int matrix1[20][20], int matrix2[20][20], int row1, int row2, int col1, int col2);
void matrixMult(int matrix1[20][20], int matrix2[20][20], int matrix3[20][20], int row1, int row2, int col1, int col2);
int main(void)
{
// global declarations
int matrix1[20][20];
int matrix2[20][20];
int matrix3[20][20];
int row1;
int row2;
int col1;
int col2;
printf("Enter first matrix dimension: ");
scanf("%d %d", &row1, &col1);
printf("%d x %d\n", row1, col1);
printf("Enter first matrix dimension: ");
scanf("%d %d", &row2, &col2);
printf("%d x %d\n", row2, col2);
printf("\n");
if(col1 == row2)
{
getData(matrix1, matrix2, row1, row2, col1, col2);
matrixMult(matrix1, matrix2, matrix3, row1, row2, col1, col2);
}
return 0;
}
void getData(int matrix1[20][20], int matrix2[20][20], int row1, int row2, int col1, int col2)
{
// local declarations
int i = 0;
int j = 0;
printf("Enter number by row and column: ");
printf("\n");
// input first matrix
for(i = 0; i < row1; i++)
for(j = 0; j < col1; j++)
{
scanf("%d", &matrix1[i][j]);
}
// reset i and j
i = 0;
j = 0;
printf("Enter number by row and column: ");
printf("\n");
// intput second matrix
for(i = 0; i < row2; i++)
for(j = 0; j < col2; j++)
{
scanf("%d", &matrix1[i][j]);
}
return;
}
void matrixMult(int matrix1[20][20], int matrix2[20][20], int matrix3[20][20], int row1, int row2, int col1, int col2)
{
int i = 0;
int j = 0;
int k = 0;
int sum = 0;
for(i = 0; i < row1; i++)
{
for(j = 0; j < col2; j++)
{
for (k= 0; k < row2; k++)
{
sum += matrix1[i][k] * matrix2[k][j];
}
matrix3[i][j] = sum;
sum = 0; // set sum to 0
}
}
//reset i and j
i = 0;
j = 0;
printf("product of matrix 1 and 2: ");
printf("\n");
for(i = 0; i < row1; i++)
{
for(j = 0; j < col2; j++)
{
printf("%d\t", matrix3[i][j]);
}
printf("\n");
}
return;
}
乘法,而不是它的打印輸出後不打印值的代碼出那些逝去矩陣1,矩陣和matrix3時有什麼不好的代碼,我是不是做錯事的matrixMult函數值
Enter first matrix dimension: 2 2
2 x 2
Enter first matrix dimension: 2 3
2 x 3
Enter number by row and column:
1 2
2 3
Enter number by row and column:
1 2 3
1 2 4
product of matrix 1 and 2:
40829548 41222770 41615992
40829548 41222770 41615992
的地址?
提前致謝。
我重新寫了只有主要的代碼,但這次的產品矩陣的最後一列還打印出垃圾
#include <stdio.h>
int main(void)
{
// global declarations
int matrix1[20][20];
int matrix2[20][20];
int matrix3[20][20];
int row1;
int row2;
int col1;
int col2;
int i = 0;
int j = 0;
int k = 0;
int sum = 0;
printf("Enter first matrix dimension: ");
scanf("%d %d", &row1, &col1);
printf("%d x %d\n", row1, col1);
printf("Enter first matrix dimension: ");
scanf("%d %d", &row2, &col2);
printf("%d x %d\n", row2, col2);
printf("\n");
if(col1 != row2)
{
printf("Matrices cannot be multiply!");
}
else
{
printf("Enter first matrix: ");
printf("\n");
for(i = 0; i < row1; i++)
for(j = 0; j < col1; j++)
scanf("%d", &matrix1[i][j]);
printf("Enter second matrix: ");
printf("\n");
for(i = 0; i < row1; i++)
for(j = 0; j < col1; j++)
scanf("%d", &matrix2[i][j]);
for(i = 0; i < row1; i++)
{
for(j = 0; j < col2; j++)
{
for (k= 0; k < row2; k++)
{
sum += matrix1[i][k] * matrix2[k][j];
}
matrix3[i][j] = sum;
sum = 0; // set sum to 0
}
}
printf("product of matrix 1 and 2: ");
printf("\n");
for(i = 0; i < row1; i++)
{
for(j = 0; j < col2; j++)
{
printf("%d\t", matrix3[i][j]);
}
printf("\n");
}
}
return 0;
}
這是我的輸出
Enter first matrix dimension: 2 2
2 x 2
Enter first matrix dimension: 2 3
2 x 3
Enter first matrix:
1 2
3 4
Enter second matrix:
2 1 4
2 5 6
product of matrix 1 and 2:
10 5 1717986916
22 11 -1717986924
爲什麼最後一欄是否打印出垃圾?
我通常把括號中這樣的說法&(matrix1 [i] [j]),比其他更清晰,因爲運算符的優先順序會正確地處理它... http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm – Antonio
你的代碼工作正常後,我做了更改。 – 0decimal0