簡體   English   中英

為什么我的矩陣不加?

[英]Why aren't my matrices adding?

當我添加矩陣時,添加的矩陣輸出只是零而不是添加的數字。 我的教練說,問題可能是我將總和作為雙指針而不是三指針傳遞給了它,但我不知道如何解決。 我該怎么做,矩陣數字總和而不是零?

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

適當更改函數以使用正確的變量。 沒有此代碼,您的代碼將不會在矩陣中存儲值,並且會添加矩陣中的隨機數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM