Search code examples
cabstract-data-type

Function to print Matrix in abstract data type


In the matrix. c I have

struct matrix{
    int row;
    int col;
    int *a;
};

Matrix* allocateMemory(int m, int n) {
    Matrix* mat = (Matrix*) malloc(sizeof(Matrix));

    if (mat == NULL) {
    return NULL;
}
mat->row = m;
mat->col = n;
mat->a = (int*)calloc(m*n, sizeof(int));
return m;
}

While in the matrix.h I have

#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED

typedef struct matrix Matrix;

Matrix* allocateMemory(int m, int n);  
//...
int printMatrix(Matrix* mat); 

int transpose(Matrix* mat); 

#endif // MATRIX_H_INCLUDED

I am working in an ADT for matrices but I stucked in the print function. I would like to create a function

int printMatrix(Matrix* mat)

So in the main.c I would have something like

int i, j;
for (i = 0; i < row; i++){
    for(j = 0; j < col; j++){           
        printf("%d    ", printMatrix(mat));
    }
    printf("\n");
}

It means I want the printf in the main, not in the function, but I just could do this

void printMatrix(Matrix* mat){
int i, j, k;
    for (i = 0; i < mat->row; i++){
        for(j = 0; j < mat->col; j++){
            k = i*mat->col + j;
            printf("%d   ", mat->a[k]);
        }
        printf("\n");
    }   
}

It does print the matrix, but it doesn't seem to be right. It is the same for the transpose matrix function, it does print the transpose matrix correctly, but I would like an

int transpose(Matrix* mat)

So I would use the function printMatrix in the main to print the transpose, but I just could do

void transpose(Matrix* mat){
int i, j, k;
    for (i = 0; i < mat->col; i++){
        for (j = 0; j < mat->row; j++){
            k = j*mat->row + i;
            printf("%f     ", mat->a[k]);
        }
        printf("\n");
    }    
}

How can I create the int function to print the matrix?

I am still studying ADT, but what would be my lack of understanding so I couldn't do the function?


Solution

  • This is prefaced by my top comments.

    A few more style tips ...

    Do not cast the return of malloc [et. al.]

    A bit more idiomatic is (e.g.):

    Matrix *mat = malloc(sizeof(*mat));
    

    I realize that in school, they teach the use of (e.g.) i, j, k, but try to use more descriptive names (e.g) row, col, off.

    And, make the arguments descriptive as well:

    Matrix *allocateMemory(int row,int col)
    

    Here's a refactored version [with some style cleanups]:

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct matrix {
        int row;
        int col;
        int *a;
    } Matrix;
    
    Matrix *
    allocateMemory(int row, int col)
    {
        Matrix *mat = malloc(sizeof(*mat));
    
        if (mat == NULL) {
            perror("malloc");
            exit(1);
        }
    
        mat->row = row;
        mat->col = col;
    
        mat->a = calloc(row * col, sizeof(*mat->a));
        if (mat->a == NULL) {
            perror("calloc");
            exit(1);
        }
    
        return mat;
    }
    
    void
    printMatrix(Matrix *mat)
    {
        int row, col, off;
    
        for (row = 0; row < mat->row; row++) {
            for (col = 0; col < mat->col; col++) {
                off = (row * mat->col) + col;
                printf("%d   ", mat->a[off]);
            }
            printf("\n");
        }
    }
    
    void
    matrix_fill(Matrix * mat)
    {
        int row, col, off;
        int val = 1;
    
        for (row = 0; row < mat->row; row++) {
            for (col = 0; col < mat->col; col++) {
                off = (row * mat->col) + col;
                mat->a[off] = val++;
            }
        }
    }
    
    void
    transpose_copy(Matrix *matout,Matrix *matin)
    {
        int row, col;
        int inoff, outoff;
    
        for (row = 0; row < matin->row; row++) {
            for (col = 0; col < matin->col; col++) {
                inoff = (row * matin->col) + col;
                outoff = (col * matout->col) + row;
                matout->a[outoff] = matin->a[inoff];
            }
            printf("\n");
        }
    }
    
    Matrix *
    transpose_new(Matrix *matin)
    {
        Matrix *matout = allocateMemory(matin->col,matin->row);
    
        transpose_copy(matout,matin);
    
        return matout;
    }
    
    int
    main(void)
    {
    
        Matrix *matin = allocateMemory(2,3);
        matrix_fill(matin);
    
        printf("Original:\n");
        printMatrix(matin);
    
        Matrix *matout = transpose_new(matin);
    
        printf("Transposed:\n");
        printMatrix(matout);
    
        return 0;
    }
    

    Here's the program output:

    Original:
    1   2   3
    4   5   6
    
    Transposed:
    1   4
    2   5
    3   6