【发布时间】: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