user1612261
user1612261

Reputation: 57

how to store matrices in heap memory

My code below works just fine. However, I want to know how I can store the matrices in heap memory. The code accepts 3 positive integers a, b and c from the user. User then input two matrices. First matrix is n(rows) by m(columns) and second matrix is m(rows) by p(columns).

the matrix product/output is n rows by p columns,e.g; Sample Input

4

3

2

14 9 3

2 11 15

0 12 17

5 2 3

12 25 9 10

8 5

Sample Output

273 455

243 235

244 205

102 160

int main(void) {
    int row1, row2, col1, col2, i, j, e;
    int temp, **matrix1, **matrix2, **mtxProduct;

    scanf("%d", &row1);
    scanf("%d", &col1);

    temp = col1;
    row2=temp;    
    scanf("%d", &col2);

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

    matrix1 = (int**) malloc(row1 * sizeof(int*));

    //read elements of 1st matrix
    for (i = 0; i < row1; i++) {
        matrix1[i] = (int*) malloc(col1 * sizeof (int));
        for (j = 0; j < col1; j++) {
            scanf("%d %d %d\n", &matrix1[i][j], &matrix1[i][j], &matrix1[i][j]);
        }
    }

    matrix2 = (int**) malloc(row2 * sizeof (int*));

    //read elements of 2nd matrix
    for (i = 0; i < row2; i++) {
        matrix2[i] = (int*) malloc(col2 * sizeof (int));
        for (j = 0; j < col2; j++) {
            scanf("%d %d %d", &matrix2[i][j], &matrix2[i][j], &matrix2[i][j]);
        }
    }

    mtxProduct = (int**) malloc(row1 * sizeof (int*));

    for (i = 0; i < col2; i++) {
        mtxProduct[i] = (int*) malloc(col2 * sizeof (int));
    }

    for (i = 0; i < row1; i++) {
        for (j = 0; j < col2; j++) {
            mtxProduct[i][j] = 0;
            for (e = 0; e < row2; e++) {
                mtxProduct[i][j] +=(matrix1[i][e] * matrix2[e][j]);
            }
        }
    }

    for (i = 0; i < row1; i++) {
        for (j = 0; j < col2; j++) {
            printf("%d ", mtxProduct[i][j]);
        }
    }
    return 0;
}

Upvotes: 1

Views: 1394

Answers (2)

Alex Chamberlain
Alex Chamberlain

Reputation: 4207

As other answerers have said, you need to use malloc. I am assuming the entries in your matrices can only be int

int * allocate_matrix(int m, int n) {
  /* Returns an allocated m x n matrix */
  return malloc(sizeof(int) * m * n);
}

To access the matrix, you need to decided whether you use column-major or row-major form. In column-major form (promoted by FORTRAN and most common in scientific programming), you store each column continuously and one after another. To access the entry at (2,3), you would take the pointer to the matrix, say A, and dereference it wrt 2*(number of columns) + 1. So, for an m x n matrix, it would be A[1+2*n] - you take one off the row and column numbers to account for 0-indexing.

If you want to get into this kind of stuff - it's really cool, honest - Google BLAS.

Upvotes: 0

Jens Gustedt
Jens Gustedt

Reputation: 78903

If you have a modern C compiler, starting from C99, it should allow the following ideom

double (*A)[m] = malloc(sizeof(double[n][m]));

such a thing is call a "variably modified type" and carries along the necessary size so the compiler can resolve A[i][j] and things like that by itself.

If you are a purist you could even do:

double (*A)[n][m] = malloc(sizeof *A);

And carry around the * by yourself, something like (*A)[i][j].

And don't forget to free the space at the end when you don't need it anymore, free(A) should do in both cases.

Upvotes: 1

Related Questions