2

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.

Stargateur
  • 24,473
  • 8
  • 65
  • 91
RogerSK
  • 393
  • 1
  • 18
  • 2
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [ask] page for help clarifying this question. – Stargateur May 20 '18 at 01:54
  • I don't understand what you want, something like `x[a][b]` ? Please note that like you are a beginner, you code don't make a lot of sense, for example, you seem to use linked list, but you use an array `element *a[3]` to stock them. This is really unclear, please don't hesitate to describe a lot more what you want. By the way, this question has a low chance to be useful, you should read some book about C. There is a lot of ressource to learn. Here, we are just going to give you some good code, but you will probably not learn a lot from it. – Stargateur May 20 '18 at 02:00
  • It is not a good idea to use `new` as a variable name as it is a keyword in C++/Java/.. It may lead to confusion – Ed Heal May 20 '18 at 03:50

1 Answers1

0

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");
    }
}
user3386109
  • 34,287
  • 7
  • 49
  • 68
  • It worked perfectly. I will accept it as the answer. Just what is this for loop sections for? – RogerSK May 20 '18 at 05:30
  • for (element *node = x[row]; node != NULL; node = node->next) if (node->column > width) width = node->column; – RogerSK May 20 '18 at 05:32
  • @RogerT That's finding the element with the largest column number, which determines the width of the matrix. For example, if the first row only has one element at column 2, then you know to print two zeros before printing the element. But you don't know how many zeros to print after the element, unless you find the width of the matrix first. – user3386109 May 20 '18 at 05:36