30/09/2018, 21:25

Hỏi về cách dùng mảng hai chiều như tham trị trong C

Dưới đây là bài code của em em muốn nhập một ma trận có kích thước row x column từ bàn phím, rồi tính tổng các thành phần trong ma trận, tổng trên một cột , tổng trên một hàng, em dùng một mảng hai chiều để chứa các giá trị nhưng khi đưa vào hàm tính tổng thì hàm không nhận được giá trị của mảng. Em không hiểu lỗi này ạ, ai giải thích giúp em.

#include <stdio.h>
#include <stdlib.h>

void getArraySize(int* row, int* column);

int getArrayRow();
int getArrayColumn();

void getArrayElements(int** array, int row, int column);

void getArraySum(int** array, int row, int column);
void displayMenu();
int getMenu();
void sumarizeAllElements(int** array, int row, int column);
void sumarizeInRow(int** array, int row, int column);
void sumarizeInColumn(int** array, int row, int column);
char rerun();

void displayGreetings();

void run();

int main(int argc, char** argv) {

    run();
    return (EXIT_SUCCESS);
}

void getArraySize(int* row, int* column) {
    printf("What size of matrix do you want ?
");
    *row = getArrayRow();
    *column = getArrayColumn();
}

int getArrayRow() {
    int row;

    printf("Row: ");
    scanf("%d", &row);
    while (row < 0) {
        printf("Wrong ! The row 's length must be positive.
");
        printf("Row: ");
        scanf("%d", &row);
    }

    return row;
}

int getArrayColumn() {
    int column;

    printf("Column: ");
    scanf("%d", &column);
    while (column < 0) {
        printf("Wrong ! The column 's length must be positive.
");
        printf("Column: ");
        scanf("%d", &column);
    }

    return column;

}

void getArrayElements(int** array, int row, int column) {

    array = (int*) malloc(row * sizeof(int));
    for (int i = 0; i < row; i++) {
        array[i] = (int*) malloc(column * sizeof(int));
        for (int j = 0; j < column; j++) {
            printf("Element at position (%d, %d): ", i, j);
            scanf("%d", array[i][j]);
        }
    }
    free(array);
}

void getArraySum(int** array, int row, int column) {
    char key;

    displayMenu();
    do {
        switch (getMenu()) {
            case 1:
                sumarizeAllElements(array, row, column);
                break;
            case 2:
                sumarizeInRow(array, row, column);
                break;
            case 3:
                sumarizeInColumn(array, row, column);
                break;
        }
        key = rerun();
    }
    while (key == 10);

}

void displayMenu() {
    printf("What do you want to compute ?
");
    printf("Press 1 to compute the sum of all elements in your matrix.
");
    printf("Press 2 to compute the sum of all elements in one row.
");
    printf("Press 3 to compute the sum of all elements in one column.
");
}

int getMenu() {
    int selection;

    printf("Selection: ");
    scanf("%d", &selection);
    while ((selection < 1) || (selection > 3)) {
        printf("Invalid selection. Selection is only between 1 and 3.
");
        printf("Selection: ");
        scanf("%d", &selection);
    }
}

void sumarizeAllElements(int** array, int row, int column) {
    int sum = 0;

    for (int i = 0; i < row; i++)
        for (int j = 0; j < column; j++)
            sum += array[i][j];

    printf("The sum of all elements in your matrix is %d.", sum);
}

void sumarizeInRow(int** array, int row, int column) {
    int sum = 0, rowIndex;

    printf("Which row you want to compute the sum ?
");
    rowIndex = getArrayRow();
    if (rowIndex > row)
        sum = 0;
    else
        for (int j = 0; j < column; j++)
            sum += array[row][j];
    printf("The sum of all elements in your row %d is %d.", row + 1, sum);
}

void sumarizeInColumn(int** array, int row, int column) {
    int sum = 0, columnIndex;

    columnIndex = getArrayColumn();
    if (columnIndex > column)
        sum = 0;
    else
        for (int i = 0; i < row; i++)
            sum += array[i][column];
    printf("The sum of all elements in your column %d is %d.", column + 1, sum);
}

char rerun() {
    char key;

    printf("Press Enter to run again. Press Esc to exit.
");
    fpurge(stdin);
    scanf("%c", &key);
    while ((key != 10) || (key != 27)) {
        printf("Invalid key. Press again.
");
        printf("Press Enter to run again. Press Esc to exit.
");
        fpurge(stdin);
        scanf("%c", &key);
    }

    return key;
}

void displayGreetings() {
    printf("Welcome to Matrix Summarizing Program.
");
}

void run() {
    int row, column;
    int array[50][50];
    displayGreetings();
    getArraySize(&row, &column);
    getArrayElements(array, row, column);
    getArraySum(array, row, column);
}
Gió viết 23:41 ngày 30/09/2018

Bỏ dòng đầu và dòng cuối của getarrayelemnt

Nguyễn Danh Đắc Sang viết 23:37 ngày 30/09/2018

bỏ r mà nó ra như vầy nè anh

Tao Không Ngu. viết 23:29 ngày 30/09/2018

This post was flagged by the community and is temporarily hidden.

Nguyễn Danh Đắc Sang viết 23:35 ngày 30/09/2018

tks ạ, bữa em mò ra rồi, ms đầu cũng bối rối mấy này lắm

Bài liên quan
0