Kesulitan menjumlahkan dua matriks menggunakan array multidimensi

Sepertinya saya mendapatkan masukan untuk matriks pertama, tetapi ketika saya meminta pengguna memasukkan masukan untuk matriks kedua, program macet..mengapa demikian? tidak dapat mengetahuinya, saya bahkan mencoba mengalokasikan memori, hasilnya sama...

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

#define MAXCOLUMNS 10 

// dealing with 2D arrays, passing to function etc
void read_input(int (*a)[MAXCOLUMNS], int n_rows, int n_columns);
void print_sum (int (*a)[MAXCOLUMNS], int (*b)[MAXCOLUMNS], int (*c)[MAXCOLUMNS], int    n_rows, int n_columns);

int main() {
    int i;
    int rows;
    int columns;

    int (*two_d_array)[MAXCOLUMNS];
    int (*two_d_array2)[MAXCOLUMNS];    
    int (*output)[MAXCOLUMNS];

    printf("enter the number of rows\n");
    scanf("%d", &rows);

    printf("enter the number of columns\n");
    scanf("%d", &columns);

    printf("enter data into array number 1\n");
    read_input(two_d_array, rows, columns);

    printf("enter data for 2d array number 2\n");
    read_input(two_d_array2, rows, columns);

    print_sum(two_d_array, two_d_array2, output, rows, columns);

    return 0;
}

void read_input(int (*a)[MAXCOLUMNS], int n_rows, int n_columns) {
    int i;
    int j;

    for (i = 0; i < n_rows; ++i) {
        for (j = 0; j < n_columns; ++j) {
            printf("enter details for rows number %d and column number %d\n", i + 1, j + 1);
            scanf("%d", (*(a+i)+j));
            getchar();
        }
    }   
}

void print_sum (int (*a)[MAXCOLUMNS], int (*b)[MAXCOLUMNS], int (*c)[MAXCOLUMNS], int n_rows, int n_columns) {
    int i;
    int j;

    // computing sum
    for (i = 0; i < n_rows; i++) {
        for (j = 0; j < n_columns; j++) {
            *(*(c+i)+j) = *(*(a+i)+j) + *(*(b+i)+j);
        }
    }

    // printing sum
    for (i = 0; i < n_rows; i++) {
        printf("\n");
        for (j = 0; j < n_columns; j++) {
            printf("%d\t", *(*(c+i)+j));
        }
    }
}

person user3151918    schedule 01.01.2014    source sumber
comment
bahkan mencoba mengalokasikan memori - dan berhenti melakukannya, mengapa? Apakah menurut Anda kesalahan apa pun yang Anda alami, dan kesalahan karena tidak mengalokasikan memori di tempat yang membutuhkan memori, akan hilang?   -  person n. 1.8e9-where's-my-share m.    schedule 02.01.2014
comment
Saya tidak menjalankan kode Anda tetapi Anda setidaknya harus menginisialisasi array Anda, misalnya, int (*two_d_array)[MAXCOLUMNS] = NULL;   -  person Bruce Dean    schedule 02.01.2014
comment
@n.m saya mencoba mengalokasikan memori seperti ini   -  person user3151918    schedule 02.01.2014
comment
two_d_array[i] = (int*) malloc (kolom * sizeof(int)); two_d_array2[i] = (int*) malloc (kolom * sizeof(int)); keluaran[i] = (int*) malloc (kolom * sizeof(int)); @n.m   -  person user3151918    schedule 02.01.2014
comment
tetapi dikatakan jenis tugas yang tidak kompatibel, di mana kesalahan saya? @BLUEPIXY   -  person user3151918    schedule 02.01.2014
comment
Edit pertanyaan Anda jika Anda ingin menambahkan informasi baru. Kode di komentar tidak dapat dibaca. (BTW menghapus baris kode yang dikeluhkan oleh kompiler bukanlah strategi koreksi kesalahan yang baik).   -  person n. 1.8e9-where's-my-share m.    schedule 02.01.2014
comment
@BLUEPIXY sejak saya mencobanya dan tidak melakukannya dengan benar.   -  person user3151918    schedule 02.01.2014
comment
di mana kesalahan saya? Belum mengamankan memori dengan benar.   -  person BLUEPIXY    schedule 02.01.2014
comment
@BLUEPIXY bisakah Anda mengetikkan contoh cara mengalokasikan memori untuk array 2d   -  person user3151918    schedule 02.01.2014
comment
Saya pikir ini adalah awal yang baik int ** Jika Anda tidak harus terpaku pada MAXCOLUMNS.   -  person BLUEPIXY    schedule 02.01.2014


Jawaban (1)


untuk C99

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

#define MAXCOLUMNS 10 

void read_input(int rows, int cols, int a[rows][cols]);
void print_sum (int rows, int cols, int in1[rows][cols], int in2[rows][cols], int out[rows][cols]);

int main(void) {
    int i, rows, columns;

    printf("enter the number of rows\n");
    scanf("%d", &rows);

    printf("enter the number of columns\n");
    scanf("%d", &columns);
    //if(columns > MAXCOLUMNS){ fprintf(stderr, "too large!"); return 1); }
    int array1[rows][columns];
    int array2[rows][columns];
    int array3[rows][columns];

    printf("enter data into array number 1\n");
    read_input(rows, columns, array1);

    printf("enter data for 2d array number 2\n");
    read_input(rows, columns, array2);

    print_sum(rows, columns, array1, array2, array3);

    return 0;
}

void read_input(int rows, int cols, int a[rows][cols]){
    int i, j;

    for (i = 0; i < rows; ++i) {
        for (j = 0; j < cols; ++j) {
            printf("enter details for rows number %d and column number %d\n", i + 1, j + 1);
            scanf("%d", &a[i][j]);
        }
    }   
}

void print_sum (int rows, int cols, int a[rows][cols], int b[rows][cols], int c[rows][cols]){
    int i, j;

    for (i = 0; i < rows; i++){
        printf("\n");,,
        for (j = 0; j < cols; j++){
            c[i][j] = a[i][j]  + b[i][j];
            printf("%d\t", c[i][j]);
        }
    }
}

untuk int**(2D_ARRAY)

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

int main(void){
    int rows = 3;
    int cols = 5;
    int **array;
    int r, c;
    //allocate
    array = malloc(rows * sizeof(int*));
    for(r = 0; r < rows ; ++r){
        array[r] = malloc(cols * sizeof(int));
    }
    //set
    for(r = 0; r < rows ; ++r){
        for(c = 0; c < cols ; ++c){
            array[r][c] = r * 10 + c;
        }
    }
    //print
    for(r = 0; r < rows ; ++r){
        for(c = 0; c < cols ; ++c){
            printf("%02d ", array[r][c]);
        }
        printf("\n");
    }
}

untuk int (**)[UKURAN]

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

#define MAX 10

int main(void){
    int rows = 3;
    int cols = 5;
    int (**array)[MAX];
    int r, c;
    //allocate
    array = malloc(rows * sizeof(int (*)[MAX]));
    for(r = 0; r < rows ; ++r){
        array[r] = malloc(sizeof(int[MAX]));
    }
    //set
    for(r = 0; r < rows ; ++r){
        for(c = 0; c < cols ; ++c){
            (*array[r])[c] = r * 10 + c;
        }
    }
    //print
    for(r = 0; r < rows ; ++r){
        for(c = 0; c < cols ; ++c){
            printf("%02d ", (*array[r])[c]);
        }
        printf("\n");
    }
}

untuk int (*)[UKURAN]

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

#define MAX 10

int main(void){
    int rows = 3;
    int cols = 5;
    int (*array)[MAX];
    int r, c;
    //allocate
    array = malloc(rows * sizeof(int[MAX]));
    //set
    for(r = 0; r < rows ; ++r){
        for(c = 0; c < cols ; ++c){
            array[r][c] = r * 10 + c;
        }
    }
    //print
    for(r = 0; r < rows ; ++r){
        for(c = 0; c < cols ; ++c){
            printf("%02d ", array[r][c]);
        }
        printf("\n");
    }
}
person BLUEPIXY    schedule 01.01.2014
comment
kapan **array[MAX] digunakan? apakah ini cara penulisan yang lain (*array)[MAXSIZE] atau array pointer seperti *array[MAXSIZE] ? @BLUEPIXY - person user3151918; 02.01.2014
comment
dan bagaimana saya tahu kapan harus menulis malloc seperti ini array[i] =int*malloc(columns * sizeof(int)); di mana i bertambah dalam loop for ke jumlah baris maksimal, dan kolom mewakili jumlah maksimal. kolom, mengapa Anda tidak mengalikan kolom dengan sizeof int pada contoh **array? @BLUEPIXY - person user3151918; 02.01.2014
comment
array = malloc (baris sizeof(int()[MAXSIZE])); untuk ( i = 0; i ‹baris ;i ++) array[i] = malloc(sizeof(int[MAXSIZE])); //Saya mencoba mengalokasikan memori seperti ini seperti yang Anda katakan tetapi dikatakan tipe tugas tidak kompatibel.. - person user3151918; 02.01.2014
comment
@ user3151918 kenapa kamu tidak mengalikan kolom dengan sizeof int pada contoh **array? array[r] = malloc(cols * sizeof(int)); saya mengalikan kolom. - person BLUEPIXY; 02.01.2014
comment
@user3151918 array = malloc (baris sizeof(int()[MAXSIZE])); for ( i = 0; i ‹rows ;i ++) array[i] = malloc(sizeof(int[MAXSIZE])); Tampaknya salah dalam berbagai hal. - person BLUEPIXY; 02.01.2014
comment
@ user3151918 **array[MAX] , (*array)[MAXSIZE] , *array[MAXSIZE] , Ini berbeda - person BLUEPIXY; 02.01.2014