Search code examples
cmatrixtabular-form

Present Matrix in tabular form C Programming


Currently, I am trying to use the linked node to represent the matrix. My codes are working fine, while I not sure it is possible to represent my matrix in tabular form instead of (x,y) = value I want to represent it like(consists of zero element and non-zero elements.)

1   2   3
0   5   0
7   8   9

Below is my codes with the linked node in the matrix, my program will read the row,column and value from user and print it out.

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int column;
    int value;
    int row;
    struct node *next;
} element;

void Init(element *x[])
{
    int i;
    for (i = 0; i < 3; i++) x[i] = NULL;
}

void Insert(element *x[], int row, int column, int value)
{
    int r = row;
    element *p;

    element *new = malloc(sizeof(element));
    new->row = row;
    new->column = column;
    new->value = value;

    if (x[r] == NULL)
    {
        x[r] = new;
        new->next = NULL;
    }
    else
    {
        p = x[r];
        if (new->column < p->column)
        {
            new->next = p;
            x[r] = new;
        }
        else if (new->column > p->column)
        {
            while (p->next != NULL && p->next->column < new->column)
            {
                p = p->next;
            }
            new->next = p->next;
            p->next = new;
        }
        else printf("An element already exists there!!\n");
    }
}

void Printout(element *x[])
{
    int i, test = 0;
    element *temp;

    for (i = 0; i < 3; i++)
    {
        temp = x[i];
        while (temp != NULL)
        {
            printf("Element position (%d,%d) = %d\n", i, temp->column, temp->value);
            test = 1;
            temp = temp->next;
        }
    }
    if (test == 0) printf("This matrix is empty!!\n");
}

int main(int argc, const char * argv[])
{
    int choice, column, row, value, number;
    element *a[3], *b[3], *sum[3];
    Init(a);    Init(b);    Init(sum);
    do
    {
        printf("\n***\tADDING SPARSE MATRICES\t***\n");
        printf("\n 1.) Insert in A");
        printf("\n 2.) Insert in B");
        printf("\n 3.) Printout both");
        printf("\n 0.) EXIT");
        printf("\nChoose ---------> ");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:     /*Insert in A */
            do
            {
                printf("Enter row -> ");
                scanf("%d", &row);
            } while (row < 0 || row > 3);

            do
            {
                printf("Enter column -> ");
                scanf("%d", &column);
            } while (column < 0);

            printf("Enter value -> ");
            scanf("%d", &value);

            Insert(a, row, column, value);

            break;
        case 2:     /*Insert in B */
            do
            {
                printf("Enter row -> ");
                scanf("%d", &row);
            } while (row < 0 || row > 2);

            do
            {
                printf("Enter column -> ");
                scanf("%d", &column);
            } while (column < 0);

            printf("Enter value -> ");
            scanf("%d", &value);

            Insert(b, row, column, value);

            break;
        case 3:     /* Printout A & B */
            printf("\n::::::: MATRIX A :> \n\n");
            Printout(a);
            printf("\n::::::: MATRIX B :> \n\n");
            Printout(b);
            break;

        default:
            printf("\nWRONG CHOICE");
        }
    } while (choice != 0);

    return 0;
}

I need someone to enlighten me.


Solution

  • First, the code needs to figure out the width of the matrix. This can be done by scanning all of the elements to find the element with the largest column. The matrix width is the largest column plus one.

    Then, to print each row in the matrix, print zeros until the correct column is reached. Then print the value in the element, and move to the next element. When all of the elements have been printed, print additional zeros until the width is reached.

    void Printout(element *x[])
    {
        // find the width of the matrix
        int width = -1;
        for (int row = 0; row < 3; row++)
        {
            for (element *node = x[row]; node != NULL; node = node->next)
                if (node->column > width)
                    width = node->column;
        }
        width++;  // width is max column plus one
    
        // print each row of the matrix
        for (int row = 0; row < 3; row++)
        {
            int col = 0;
            for (element *node = x[row]; node != NULL; node = node->next)
            {
                for (; col < node->column; col++) // print zeros until the column is reached
                    printf("0  ");
                printf("%d  ", node->value);      // print the value for the current element
                col = node->column + 1;
            }
            for (; col < width; col++)            // print zeros until the width is reached
                printf("0  ");
            printf("\n");
        }
    }