ปัญหาในการเพิ่มเมทริกซ์สองตัวโดยใช้อาร์เรย์หลายมิติ

ดูเหมือนว่าฉันจะได้รับอินพุตสำหรับเมทริกซ์ตัวแรก แต่เมื่อฉันขอให้ผู้ใช้ป้อนข้อมูลสำหรับเมทริกซ์ตัวที่สอง โปรแกรมก็หยุดทำงาน..เหตุใดจึงเป็นเช่นนี้ คิดไม่ออก ฉันพยายามจัดสรรหน่วยความจำแล้ว ผลลัพธ์ก็เหมือนเดิม...

#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 แหล่งที่มา
comment
แม้กระทั่งลองจัดสรรหน่วยความจำ - และหยุดทำเช่นนั้นเพราะเหตุใด คุณคิดว่าข้อผิดพลาดใดก็ตามที่คุณพบและข้อผิดพลาดในการไม่จัดสรรหน่วยความจำในที่ที่จำเป็นต้องใช้หน่วยความจำจะถูกยกเลิกหรือไม่?   -  person n. 1.8e9-where's-my-share m.    schedule 02.01.2014
comment
ฉันไม่ได้รันโค้ดของคุณ แต่อย่างน้อยคุณควรเริ่มต้นอาร์เรย์ของคุณ เช่น int (*two_d_array)[MAXCOLUMNS] = NULL;   -  person Bruce Dean    schedule 02.01.2014
comment
@ n.m ฉันพยายามจัดสรรหน่วยความจำแบบนี้   -  person user3151918    schedule 02.01.2014
comment
two_d_array[i] = (int*) malloc (คอลัมน์ * ขนาดของ(int)); two_d_array2[i] = (int*) malloc (คอลัมน์ * ขนาดของ(int)); เอาต์พุต [i] = (int*) malloc (คอลัมน์ * ขนาดของ (int)); @n.m   -  person user3151918    schedule 02.01.2014
comment
แต่มันบอกว่างานประเภทที่เข้ากันไม่ได้ ฉันจะผิดตรงไหน? @BLUEPIXY   -  person user3151918    schedule 02.01.2014
comment
แก้ไขคำถามที่คุณต้องการเพิ่มข้อมูลใหม่ รหัสในความคิดเห็นไม่สามารถอ่านได้ (BTW การลบบรรทัดโค้ดที่คอมไพเลอร์บ่นนั้นไม่ใช่กลยุทธ์การแก้ไขข้อผิดพลาดที่ดีนัก)   -  person n. 1.8e9-where's-my-share m.    schedule 02.01.2014
comment
@BLUEPIXY ตั้งแต่ฉันลองแล้วและทำไม่ถูกต้อง   -  person user3151918    schedule 02.01.2014
comment
ฉันผิดตรงไหน? ไม่สามารถรักษาความปลอดภัยหน่วยความจำได้อย่างเหมาะสม   -  person BLUEPIXY    schedule 02.01.2014
comment
@BLUEPIXY คุณช่วยพิมพ์ตัวอย่างวิธีจัดสรรหน่วยความจำสำหรับอาร์เรย์ 2 มิติได้ไหม   -  person user3151918    schedule 02.01.2014
comment
ฉันคิดว่ามันเป็นการเริ่มต้นที่ดี int ** ถ้าคุณไม่ต้องยึดติดกับ MAXCOLUMNS   -  person BLUEPIXY    schedule 02.01.2014


คำตอบ (1)


สำหรับ 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]);
        }
    }
}

สำหรับ 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");
    }
}

สำหรับ int (**)[SIZE]

#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");
    }
}

สำหรับ int (*)[SIZE]

#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
**array[MAX] จะใช้เมื่อใด เป็นวิธีการเขียนอีกวิธีหนึ่ง (*array)[MAXSIZE] หรือเป็นอาร์เรย์ของพอยน์เตอร์เช่น *array[MAXSIZE] ? @BLUEPIXY - person user3151918; 02.01.2014
comment
และฉันจะรู้ได้อย่างไรว่าเมื่อใดควรเขียน malloc เช่นนี้ array[i] =int*malloc(columns * sizeof(int)); โดยที่ i เพิ่มขึ้นใน for loop เป็นจำนวนแถวสูงสุด และคอลัมน์แทนจำนวนสูงสุด ของคอลัมน์ ทำไมคุณไม่คูณคอลัมน์ด้วย sizeof int ในตัวอย่างอาร์เรย์ ** @BLUEPIXY - person user3151918; 02.01.2014
comment
array = malloc (แถว ขนาดของ(int()[MAXSIZE])); สำหรับ ( i = 0; i ‹rows ;i ++) array[i] = malloc(sizeof(int[MAXSIZE])); //ฉันพยายามจัดสรรหน่วยความจำแบบนี้ตามที่คุณบอก แต่มันบอกว่าประเภทที่เข้ากันไม่ได้ในงานมอบหมาย.. - person user3151918; 02.01.2014
comment
@ user3151918 ทำไมคุณไม่คูณคอลัมน์ด้วย sizeof int ในตัวอย่าง **array array[r] = malloc(cols * sizeof(int)); ฉันคูณคอลัมน์แล้ว - person BLUEPIXY; 02.01.2014
comment
@ user3151918 array = malloc (ขนาดแถวของ(int()[MAXSIZE])); for ( i = 0; i ‹rows ;i ++) array[i] = malloc(sizeof(int[MAXSIZE])); ดูเหมือนว่าจะผิดในรูปแบบต่างๆ - person BLUEPIXY; 02.01.2014
comment
@ user3151918 **array[MAX] , (*array)[MAXSIZE] , *array[MAXSIZE] มันแตกต่าง - person BLUEPIXY; 02.01.2014