在乘以两个矩阵时将地址作为输出

时间:2013-07-09 06:54:17

标签: c multidimensional-array matrix-multiplication

嗨我有一个关于乘以两个矩阵的问题

这是我写的代码

#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

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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]);