[英]Why aren't my matrices adding?
When I add the matrices, my added matrix output is just zeroes instead of the added numbers. 当我添加矩阵时,添加的矩阵输出只是零而不是添加的数字。 My instructor says the problem may be that I passed the sum as a double pointer instead of a triple pointer to update it, but I don't know how to fix it.
我的教练说,问题可能是我将总和作为双指针而不是三指针传递给了它,但我不知道如何解决。 What do I do so the matrix numbers sums come out instead of zeroes?
我该怎么做,矩阵数字总和而不是零?
#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;
}
Your make_matrix()
functions are not good. 您的
make_matrix()
函数不好。 The code to read numbers 读取数字的代码
for(row = 0; row < num_rows; ++row){
for(col = 0; col < num_cols; ++col){
scanf("%d", &userNum);
}
}
You should do 你应该做
for(row = 0; row < num_rows; ++row){
for(col = 0; col < num_cols; ++col){
scanf("%d", &matrixA[row][col]); //use matrix to store number
}
}
Change your functions appropriately to use correct variables. 适当更改函数以使用正确的变量。 without this your code does not store values in matrix and adds up random numbers that are in matrices.
没有此代码,您的代码将不会在矩阵中存储值,并且会添加矩阵中的随机数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.