当我添加矩阵时,我添加的矩阵输出只是零而不是添加的数字。我的导师说问题可能是我将总和作为双指针传递而不是三指针来更新它,但我不知道如何解决它。我该怎么做才能得到矩阵数和而不是零?
#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;
}
答案 0 :(得分:2)
您的make_matrix()
功能不佳。读取数字的代码
for(row = 0; row < num_rows; ++row){
for(col = 0; col < num_cols; ++col){
scanf("%d", &userNum);
}
}
你应该做
for(row = 0; row < num_rows; ++row){
for(col = 0; col < num_cols; ++col){
scanf("%d", &matrixA[row][col]); //use matrix to store number
}
}
适当更改您的功能以使用正确的变量。 如果没有这个,你的代码就不会在矩阵中存储值,而是将矩阵中的随机数加起来。