0

i have been trying to figure out the problem for 7-8h,

the thing that i must build a matrix multiplication func, using a given structs and an array of pointers to linked lists(The Matrix)

everything must be allocated by malloc,

Here is the given structs:

typedef struct cellNode
    {
        int cell;
        struct cellNode* next;
    } Node;
typedef struct matrix
    {
        int numRows;
        int numColumns;
        Node** rows;
    } Matrix;

and here is the function to build the matrix and to allocate it:

Matrix* MatrixBuilder(Matrix* A, int rows, int cols)
{
    int i=0;
    A->numColumns=cols;
    A->numRows=rows;

    A->rows = (Node**)malloc(rows * sizeof(Node*));
    if (A->rows == NULL)
    {
        printf("memory error!");
        exit(1);
    }

    for (i=0; i<rows; i++)
    {
    A->rows[i] = (Node*)malloc(rows * sizeof(Node));
    if (A->rows[i] == NULL)
    {
        printf("memory error!");
        exit(1);
    }
    }
    for (i=0; i<rows; i++)
    {
        A->rows[i] = NULL;
    }
    return A;

}

and here is the function to build the node, with a given data and link it to the list:

Matrix* MatrixBuilder(Matrix* A, int rows, int cols)
{
    int i=0;
    A->numColumns=cols;
    A->numRows=rows;

    A->rows = (Node**)malloc(rows * sizeof(Node*));
    if (A->rows == NULL)
    {
        printf("memory error!");
        exit(1);
    }

    for (i=0; i<rows; i++)
    {
    A->rows[i] = (Node*)malloc(rows * sizeof(Node));
    if (A->rows[i] == NULL)
    {
        printf("memory error!");
        exit(1);
    }
    }
    for (i=0; i<rows; i++)
    {
        A->rows[i] = NULL;
    }
    return A;

}

and this is my main function:

int main()
{
    int n,m,k,i,x,j=0; /* x to help me insert integers into the list */
    Matrix* A = (Matrix*)malloc(sizeof(Matrix));
    Matrix* B = (Matrix*)malloc(sizeof(Matrix));
    Matrix* C = (Matrix*)malloc(sizeof(Matrix));

    printf("please enter n:");
    scanf("%d",&n);

    printf("please enter m:");
    scanf("%d",&m);

    printf("please enter k:");
    scanf("%d",&k);

    A = MatrixBuilder(A, n, m);
    B = MatrixBuilder(B, m, k);
    C = MatrixBuilder(C, n, k);




    for (i=0; i<n; i++)
    {
        printf("Enter row %d data\n",i);
        for (j=0; j<m; j++)
        {
            scanf("%d", &x);
            NodeBuilder_Insert(A->rows[j], x);
        }
    }

    return 0;
}

when i run it, it stops right when i enter the first element in first row, so i quess the problem is in the NodeBuilder function, but i cant find it, and i am not sure if i allocated everything properly...

Taimbo
  • 1
  • 1
  • Apart from those obvious problems, just a design note. The first argument to `MatrixBuilder` is near worthless *unless* the intent is the caller provides a base `Matrix` structure that is (a) partiallyt pre-existing in some form, be it dynamic or otherwise, and (b) *not* populated, since whatever is/was there prior will be obliterated by the function with prejudice. Unrelated, [stop casting memory allocation functions in C](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). It helps *nothing* and potentially masks problems. – WhozCraig May 30 '22 at 18:01
  • why it shall be cols? 'rows[i]' is the array that have pointers to LLists, and the len of it is the number of rows(the rows in the matrix) and about setting all row-pointers to NULL, a friend of mine told me its a must... @SupportUkraine – Taimbo May 30 '22 at 18:03
  • @WhozCraig, b is not populated because i still didint manage to insert integers to A properly, i cant find the way to do it, also this is kind of a homework to me, it is a must to allocate them, i cant do it otherwise, i cant use another arrays beside "rows"... i have some rules – Taimbo May 30 '22 at 18:07
  • @SupportUkraine wow, setting them to NULL was the problem i quess, ill try to complete it rn, thank you guys, its my first time asking a question here and you helped me:) thank you – Taimbo May 30 '22 at 18:10
  • printing a cell return error.. why would it be?@WhozCraig @SupportUkraine – Taimbo May 30 '22 at 18:52

0 Answers0