【问题标题】:matrix multipication segmentation error?矩阵乘法分割错误?
【发布时间】:2012-08-20 17:02:41
【问题描述】:

需要帮助来确定程序不产生矩阵乘积的原因。这个想法是:

输入 n,m,p 后跟 2 个矩阵;

第一个是 'n' 行由 'm' 列矩阵。第二个是“p”列矩阵的“m”行。

4

3

2

14 9 3

2 11 15

0 12 17

5 2 3

12 25

9 10

8 5

两个矩阵的输出乘积

273 455

243 235

244 205

102 160

#include<stdio.h>;
#include<malloc.h>;

int main(void) {
    int r1, r2, c1, c2, i, j, e;
    int temp, **mat1, **mat2, **ansMat;

    /*Accepting row and column values of 1st matrix*/
    //Enter no. of rows for 1st matrix
    scanf("%d", &r1);
    //Enter no. of cols for 1st matrix
    scanf("%d", &c1);

    temp = c1;
    r2=temp;
    /*Accepting row and column values of 2nd matrix*/
    //Enter no. of cols for 2nd matrix
    scanf("%d", &c2);

    if (c1 != r2) {
        printf("\nIncorrect combination!\n");
        return 1;
    }

    //Allocate memory for matrix1(# of rows)
    mat1 = (int**) malloc(r1 * sizeof(int*));

    printf("Enter element of 1st matrix:\n");
    for (i = 0; i < r1; i++) {
        mat1[i] = (int*) malloc(c1 * sizeof (int));
        for (j = 0; j < c1; j++) {
            printf("\nEnter element matrix 1[%d][%d]:", i, j);
            scanf("%d", &mat1[i][j]);
        }
    }

    //Allocate memory for matrix2(# of rows)
    mat2 = (int**) malloc(r2 * sizeof (int*));

    printf("\nEnter elements of 2nd matrix:\n");
    for (i = 0; i < r2; i++) {
        mat2[i] = (int*) malloc(c2 * sizeof (int));
        for (j = 0; j < c2; j++) {
            printf("\n\tEnter element matrix 2[%d][%d]:", i, j);
            scanf("%d", &mat2[i][j]);
        }
    }

    //Print the first matrix
    printf("\nFirst Matrix:\n");
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c1; j++) {
            printf("%d ", mat1[i][j]);
        }
        printf("\n");
    }

    //Print the second matrix
    printf("\nSecond Matrix:\n");
    for (i = 0; i < r2; i++) {
        for (j = 0; j < c2; j++) {
            printf("%d ", mat2[i][j]);
        }
        printf("\n");
    }

    //Allocate memory for solution matrix(# of rows)
    ansMat = (int**) malloc(r1 * sizeof (int*));

    //Allocate memory for solution matrix(# of columns)
    for (i = 0; i < c2; i++) {
        ansMat[i] = (int*) malloc(c2 * sizeof (int));
    }

    //Matrix multiplication
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            ansMat[i][j] = 0;
            for (e = 0; e < r2; e++) {
                ansMat[i][j] += mat1[i][e] * mat2[e][j]);
            }
        }
    }
    //Print matrix solution
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            printf("%d ", ansMat[i][j]);
        }
    }
    return 0;
}

【问题讨论】:

  • 请贴出ansMat、mat1、mat2的定义。
  • 这是一个数组边界问题。我们可以在不看到这个函数的开头以及调用它的数据的情况下提供进一步的帮助吗?没有。
  • 如果您遇到分段错误,则该问题与 ansMat 有关。它是作为参数接收的局部变量还是动态分配的指针?您需要公开更多代码。
  • @user1202136 得到一个与 OP 混淆的名称
  • r2的值是多少?

标签: c


【解决方案1】:
int temp, *mat1, *mat2, *ansMat;

...

//Allocate memory for matrix1(# of rows)
mat1 = (int*) malloc(r1 * c1 * sizeof(int));

printf("Enter element of 1st matrix:\n");
for (i = 0; i < r1; i++) {
    for (j = 0; j < c1; j++) {
        printf("\nEnter element matrix 1[%d][%d]:", i, j);
        scanf("%d", &mat1[i*j]);
    }
}

...

//Allocate memory for matrix2(# of rows)
mat2 = (int*) malloc(r2 * c2 * sizeof (int));

printf("\nEnter elements of 2nd matrix:\n");
for (i = 0; i < r2; i++) {
    for (j = 0; j < c2; j++) {
        printf("\n\tEnter element matrix 2[%d][%d]:", i, j);
        scanf("%d", &mat2[i*j]);
    }
}

...

//Allocate memory for solution matrix(# of rows)
ansMat = (int*) malloc(r1 * c2 * sizeof (int));

//Matrix multiplication
for (i = 0; i < r1; i++) {
    for (j = 0; j < c2; j++) {
        ansMat[i*j] = 0;
        for (e = 0; e < r2; e++) {
            ansMat[i*j] += mat1[i*e] * mat2[e*j]);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    相关资源
    最近更新 更多