-1
행렬을 추가 할 때, 추가 된 행렬 출력은 추가 된 숫자가 아니라 단지 0입니다. 강사는 문제를 해결하기 위해 삼중 포인터 대신 이중 포인터로 합계를 전달했지만 문제를 해결할 방법을 모르겠다 고 말합니다. 매트릭스 번호의 합이 0 대신 나오도록하려면 어떻게해야합니까?왜 매트릭스가 추가되지 않습니까?
#include <stdio.h>
#include <stdlib.h>
int** make_matrixA(int num_rows, int num_cols){
int** matrixA;
int row, col;
int userNum;
matrixA = (int**)malloc(num_rows * sizeof(int*));
for(row = 0; row < num_rows; ++row){
matrixA[row] = (int*)malloc(num_cols * sizeof(int));
}
for(row = 0; row < num_rows; ++row){
for(col = 0; col < num_cols; ++col){
scanf("%d", &userNum);
}
}
return matrixA;
}
int** make_matrixB(int num_rows, int num_cols){
int** matrixB;
int row, col;
int userNum;
matrixB = (int**)malloc(num_rows * sizeof(int*));
for(row = 0; row < num_rows; ++row){
matrixB[row] = (int*)malloc(num_cols * sizeof(int));
}
for(row = 0; row < num_rows; ++row){
for(col = 0; col < num_cols; ++col){
scanf("%d", &userNum);
}
}
return matrixB;
}
int** make_matrixC(int num_rows, int num_cols){
int** matrixC;
int row;
matrixC = (int**)malloc(num_rows * sizeof(int*));
for(row = 0; row < num_rows; ++row){
matrixC[row] = (int*)malloc(num_cols * sizeof(int));
}
return matrixC;
}
int** add_matrix(int num_rows, int num_cols, int** matrixA, int** matrixB, int** matrixC){
int row, col;
for(row = 0; row < num_rows; ++row){
for(col = 0; col < num_cols; ++col){
matrixC[row][col] = matrixA[row][col] + matrixB[row][col];
}
}
return matrixC;
}
void print_matrix(int num_rows, int num_cols,int** matrixA, int** matrixB, int** matrixC){
int row, col;
printf("A + B = \n");
for(row = 0; row < num_rows; ++row){
for(col = 0; col < num_cols; ++col){
matrixC[row][col] = matrixA[row][col] + matrixB[row][col];
printf("%d ", matrixC[row][col]);
}
printf("\n");
}
}
void delete_matrixA(int num_rows, int** matrixA){
int row;
for(row = 0; row < num_rows; ++row){
free(matrixA[row]);
}
free(matrixA);
}
void delete_matrixB(int num_rows, int** matrixB){
int row;
for(row = 0; row < num_rows; ++row){
free(matrixB[row]);
}
free(matrixB);
}
void delete_matrixC(int num_rows, int** matrixC){
int row;
for(row = 0; row < num_rows; ++row){
free(matrixC[row]);
}
free(matrixC);
}
int main(){
int num_rows, num_cols;
int** matrixA;
int** matrixB;
int** matrixC;
//get input
printf("Please enter the number of rows: ");
scanf("%d", &num_rows);
printf("Please enter the number of columns: ");
scanf("%d", &num_cols);
//make matrix A
printf("Enter Matrix A\n");
matrixA = make_matrixA(num_rows, num_cols);
//make matrix B
printf("Enter Matrix B\n");
matrixB = make_matrixB(num_rows, num_cols);
//add the matrices
matrixC = make_matrixC(num_rows, num_cols);
matrixC = add_matrix(num_rows, num_cols, matrixA, matrixB, matrixC);
//print the matrix
print_matrix(num_rows, num_cols, matrixA, matrixB, matrixC);
//delete the matrices
delete_matrixA(num_rows, matrixA);
delete_matrixB(num_rows, matrixB);
delete_matrixC(num_rows, matrixC);
return 0;
}
의견 : 코드가 동일한 경우 왜 make_matrixA와 make_matrixB를 사용합니까? 'matrixC'를 이미 주었을 때'add_matrix'에서 무엇을 돌려 주어야합니까? 왜'print_matrix'도 단지 print가 아닌 sum을 계산합니까? 문제에 관해서, 당신은'add_matrix' 함수를 디버깅함으로써 무엇을 배웠습니까? –
강사가 2 차원 배열을 일컬어 행렬을 사용해야한다고 말했고 3 성급 C 프로그래머라는 것은 칭찬이 아닙니다. 그녀에게 나쁜 빛을 비췄다. 2D 배열/행렬과 같은 것도 없으며 코드에서 하나를 보유 할 수도 없습니다. – Olaf
함수를 좀 더 일반화하려고해야합니다 ... 예를 들어 행렬을 할당하는 함수 하나, 값을 입력하는 함수 등이 있습니다. 또한 모든 함수에 행렬을 모두 전달할 필요는 없습니다. , 그 기능이 사용할 것입니다. 그리고, 왜 당신은 당신의 인쇄 기능을 이미 추가 기능에 추가했을 때 매트릭스를 추가하고 있습니까? – Dmitri