简体   繁体   English

为什么我的矩阵不加?

[英]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.

相关问题 为什么我的进程并不同时运行? - Why aren't my processes running concurrently? 为什么我的 for 循环在 C 中的冒泡排序 function 中不起作用 - Why aren't my for loops working in this bubble sort function in C 为什么这些continue语句不能在我的循环中工作? - Why aren't these continue statements working in my loops? 为什么我的整数数组指针不起作用? - Why aren't my pointers to integer arrays working? 为什么我的琴弦没有打印出正确的值? - Why aren't my strings printing the proper value? 为什么我的编译守卫不阻止多个定义包含? - Why aren't my compile guards preventing multiple definition inclusions? 为什么我的双向链表的元素没有正确显示? - Why aren't the elements of my Doubly Linked List displayed properly? 为什么在WaitForSingleObject之后我的变量不保持状态? - Why aren't my variables holding state after WaitForSingleObject? 为什么没有新元素被添加到我的链表中? - Why aren't new elements being added to my linked list? 为什么节点添加不正确?为什么反向打印? (单链表) - Why aren't nodes adding properly and why is it printing in reverse? (singly linked list)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM