Represent Matrix in tabular form C++

334 views Asked by At

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.

While my printout() method only print it out when temp != NULL, and this is the result

Element position(3,3) = 9

I want to represent it like

1   2   3
4   5   6
7   8   9

Below is my codes with the linked node in the matrix

#include <iostream>
#include <conio.h>
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>

using namespace std;


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


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

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

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

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


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

    for (i = 0; i < 11; i++) {
        temp = x[i];
        while (temp != NULL) {
            cout << "Element position" << "(" << i << "," << temp->column << ") = " << temp->value << endl;
            test = 1;
            temp = temp->next;
        }

    }

    if (test == 0) {
        cout << "This matrix is empty" << endl;
    }
}



int main(int argc, const char * argv[]) {

    int choice, column, row, value, number;
    element *a[10], *b[10], *sum[10];
    Init(a);    Init(b);    Init(sum);
    do
    {
        cout << "Add Sparse Matrix" << endl;
        cout << "1. Insert in A" << endl;
        cout << "2. Insert in B" << endl;
        cout << "3. Print 2 matrix" << endl;
        cout << "0. Exit" << endl;
        cout << "Please enter your option" << endl;

        cin >> choice;
        switch (choice)
        {
        case 1:
            do
            {
                cout << "Enter row -> ";
                cin >> row;
            } while (row < 0 || row > 11);

            do
            {
                cout << "Enter column -> ";
                cin >> column;
            } while (column < 0);

            cout << "Enter value -> ";
            cin >> value;

            Insert(a, row, column, value);

            break;
        case 2:
            do
            {
                cout << "Enter row -> ";
                cin >> row;
            } while (row < 0 || row > 11);

            do
            {
                cout << "Enter column -> ";
                cin >> column;
            } while (column < 0);

            cout << "Enter value -> ";
            cin >> value;

            Insert(b, row, column, value);

            break;
        case 3:
            cout << "\n::::::: MATRIX A :> \n\n";
            Printout(a);
            cout << "\n::::::: MATRIX B :> \n\n";
            Printout(b);
            break;

        default:
            cout << "WRONG CHOICE\n\n";
        }
    } while (choice != 0);




    return 0;


}

Sorry for asking this question, I am just started to learn C++ programming, need someone to enlighten me. Thank you for your concern.

1

There are 1 answers

1
Anthony On BEST ANSWER

Since your linked list is used to saves the memory usage, you can write some codes to print the zero until the width is reached.

void SM::Printout(element *x[])
{
    int width = -1;
    for (int row = 0; row < 11; row++)
    {
        for (element *node = x[row]; node != NULL; node = node->next)
            if (node->column > width)
                width = node->column;
    }
    width++;

    for (int row = 0; row < 11; row++)
    {
        int col = 0;
        for (element *node = x[row]; node != NULL; node = node->next)
        {
            for (; col < node->column; col++)
                cout << "0  ";
            if (node->value != NULL)
            cout << node->value << "  ";
            col = node->column + 1;
        }
        for (; col < width; col++)
            cout << "0  ";
        cout << "\n";
    }
}