c - 为什么我的矩阵没有相加?

标签 c pointers matrix

当我添加矩阵时,添加的矩阵输出只是零,而不是添加的数字。我的老师说问题可能是我将总和作为双指针而不是三重指针传递来更新它,但我不知道如何修复它。我该怎么做才能得到矩阵数字的和而不是零?

#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_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
    }
}

适本地更改您的函数以使用正确的变量。 如果没有这个,您的代码不会将值存储在矩阵中,并将矩阵中的随机数相加。

关于c - 为什么我的矩阵没有相加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37622172/

相关文章:

c++ - 将 C 程序更改为 C++ 程序 - 编译错误

c++ - 在 2D、z 顺序渲染系统中组合点(粒子)和三角形( Sprite )

c - 通过C程序编写XML文件

matlab - 如何在 Matlab 中选择子矩阵(不以任何特定模式)

c - *(int*)变量是什么意思?

pointers - 清除截断指针的更快或更慢的方法?

pointers - 戈朗 : Passing in Slice as Reference issue

arrays - 在 C 中,什么时候我们随便移出指针区域是安全的?

c++ - 如何手动输入数据矩阵?

javascript - 在 JavaScript 中转置二维数组