1

嗨,我有一个关于将两个矩阵相乘的问题

这是我写的代码

#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

为什么最后一列打印出垃圾?

4

2 回答 2

0

我认为您看到的不是地址:您的 printf 语句似乎是正确的。您必须检查填充矩阵的函数,错误可能就在那里。如果您在程序开始时重置目标矩阵,它会有所帮助。

我还建议您在阅读后打印输入矩阵。

于 2013-07-09T06:57:57.240 回答
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]);// 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]);

于 2013-07-09T07:14:50.430 回答