【问题标题】:Matrix Multiplication in C - Problem with inputsC 中的矩阵乘法 - 输入问题
【发布时间】:2022-12-07 23:33:56
【问题描述】:

我编写了一个使用函数执行矩阵乘法的程序。代码如下:

#include <stdio.h>
void obtainMatrixElems(int mtrx[][10], int row_elems, int column_elems);
void multMatrixes(int matrix1[][10], int matrix2[][10], int multAns[][10], int rowmat1, int rowmat2, int colmat1, int colmat2);
void resDisp(int multAns[][10], int row_elems, int col_elems);

int main() {
    int rowmat1, rowmat2, colmat1, colmat2, matrix1[10][10], matrix2[10][10], multAns[10][10];

    printf("Kindly enter the number of row/s for the first matrix: \n");
    scanf("%d", &rowmat1);
    printf("Kindly enter the number of column/s for the first matrix: \n");
    scanf("%d", &colmat1);
    printf("Kindly enter the number of row/s for the first matrix: \n");
    scanf("%d", &rowmat2);
    printf("Kindly enter the number of column/s for the second matrix: \n");
    scanf("%d", &colmat2);

    while(colmat1 != rowmat2){
        printf("This matrix is not computable. Kindly re-enter the correct data.\n");
        printf("Number of row/s for the first matrix: \n");
        scanf("%d", &rowmat1);
        printf("Number of column/s for the first matrix: \n");
        scanf("%d", &colmat1);
        printf("Number of row/s for the second matrix: \n");
        scanf("%d", &rowmat2);
        printf("Number of column/s for the second matrix: \n");
        scanf("%d", &colmat2);
    }

    //calling defined function which will obtain elements of the matrix - function is called for both matrices independantly
    obtainMatrixElems(matrix1, rowmat1, colmat1); //matrix 1
    obtainMatrixElems(matrix2, colmat2, rowmat2); //matrix 2

    //calling defined function which will compute multiplication of the two matrices.
    multMatrixes(matrix1, matrix2, multAns, rowmat1, colmat1, rowmat2, colmat2);

    //calling defined function in order to display resultant matrix.
    resDisp(multAns, rowmat1, colmat2);
}
//defining function which will be used in order to obtain the elements of a matrix
void obtainMatrixElems(int mtrx[][10], int row_elems, int col_elems){
    printf("Kindly enter matrix elements: \n");

    for(int x = 0; x < row_elems; x++){
        for(int y = 0; y < col_elems; y++){
            printf("Enter element at position %d,%d: \n", x+1, y+1);
            scanf("&d", &mtrx[x][y]);
        }
    }
}

//defining function which will be used in order to multiply the matrixes together.
void multMatrixes(int matrix1[][10], int matrix2[][10], int multAns[][10], int rowmat1, int rowmat2, int colmat1, int colmat2){

    //making matrix multiplication result equal to 0 for initialization purposes
    for(int x = 0; x < rowmat1; x++){
        for(int y = 0; y < colmat2; y++){
            multAns[x][y] = 0;
        }
    }

    //mutliplying the matrices

    for(int x = 0; x < rowmat1; x++){
        for(int y = 0; y < colmat2; y++){
            for(int z = 0; z < colmat1; z++){
                multAns[x][y] = matrix1[x][y] * matrix2[x][y];
            }
        }
    }
}

//defining function which will be used in order to display the resultant matrix
void resDisp(int multAns[][10], int row_elems, int col_elems){
    printf("\nRESULTANT MATRIX:\n");
    for (int x = 0; x < row_elems; ++x) {
        for (int y = 0; y < col_elems; ++y) {
            printf("%d", multAns[x][y]);
            if (y == col_elems - 1)
                printf("\n");
        }
    }
}

但是,当我运行该程序时,会遇到以下输出:

Kindly enter the number of row/s for the first matrix: 
3
Kindly enter the number of column/s for the first matrix: 
3
Kindly enter the number of row/s for the first matrix: 
3
Kindly enter the number of column/s for the second matrix: 
3
Kindly enter matrix elements: 
Enter element at position 1,1: 
Enter element at position 1,2: 
Enter element at position 1,3: 
Enter element at position 2,1: 
Enter element at position 2,2: 
Enter element at position 2,3: 
Enter element at position 3,1: 
Enter element at position 3,2: 
Enter element at position 3,3: 
Kindly enter matrix elements: 
Enter element at position 1,1: 
Enter element at position 1,2: 
Enter element at position 1,3: 
Enter element at position 2,1: 
Enter element at position 2,2: 
Enter element at position 2,3: 
Enter element at position 3,1: 
Enter element at position 3,2: 
Enter element at position 3,3: 

RESULTANT MATRIX:
000
000
000

Process finished with exit code 0

函数 obtainMatrixElems 中的循环似乎有问题——尽管我可能是错的。我完全不知道它可能是什么,所以任何帮助将不胜感激。

尝试使用函数在 C 中执行矩阵乘法 - 获得矩阵元素的函数循环两次并且不排除任何输入。程序在代码 0 之后终止。

【问题讨论】:

  • 总是,总是,总是,没有失败,总是检查scanf返回的值。总是。如果您甚至没有验证 scanf 是否已按预期解析输入,那么您会遇到“输入问题”一点也不奇怪。

标签: c loops matrix multiplication


【解决方案1】:

您对scanf的调用有误 它应该读作

scanf("%d", &mtrx[x][y]);

还要注意在每次输入后按 Enter 键。为了安全起见,你应该抓住它。使用类似的东西

scanf(" %d", &mtrx[x][x]);

【讨论】:

  • 您好,感谢您的意见。我已经尝试了你的建议并且它有效但仅适用于输入矩阵的第一个元素。输入第一个元素后,它似乎跳过其余部分并终止。
  • 您是否也尝试了第二条 scanf 行?
【解决方案2】:

您似乎在multAns[x][y] = matrix1[x][y] * matrix2[x][y]; 缺少一个“+”。

它应该是:

multAns[x][y] += matrix1[x][y] * matrix2[x][y];

如果你最后的操作结果是0 那么这就解释了为什么你得到一个 0 矩阵..

编辑:

标志是问题之一。您从用户那里获得输入的方式也有问题。

    for(int x = 0; x < row_elems; x++){
        for(int y = 0; y < col_elems; y++){
            printf("Enter element at position %d,%d: 
", x+1, y+1);
            // Note the change of "&" to "%" and the extra sequence
            // "
" which expects the user to press ENTER (i.e.:
            // new line) between input cells
            scanf("%d
", &mtrx[x][y]);
        }
    }

【讨论】:

  • 不幸的是没有解决它:/
  • @aadinnvv 你试过什么矩阵?
猜你喜欢
  • 1970-01-01
  • 2014-12-07
  • 1970-01-01
  • 2015-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多