gpt4 book ai didi

c - 将两个矩阵相乘时获取地址作为输出

转载 作者:太空宇宙 更新时间:2023-11-04 04:49:13 25 4
gpt4 key购买 nike

你好,我有一个关于两个矩阵相乘的问题

这是我写的代码

#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;
}

输出不打印乘法后的值,而是打印出这些值的地址

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

代码有什么问题,我在将 matrix1、matrix2 和 matrix3 传递给 matrixMult 函数时做错了什么吗?

提前致谢。


我只重写了main中的代码,但是这次产品矩阵的最后一列仍然打印出垃圾

 #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

为什么最后一列打印出乱码?

最佳答案

您的代码中的问题在于:

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]);// you were entering the values in matrix1 and leaving the matri2 blank so make it matrix2 instead.
}

改变这个:

scanf("%d", &matrix1[i][j]);

为此:

scanf("%d", &matrix2[i][j]);

关于c - 将两个矩阵相乘时获取地址作为输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17541852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com