0

04279169@uofn.edu.om

Do Muluplication in this program ?


#include<iostream>
using namespace std;

class Node {
    private:
        int row;// different data type for other apps
        int column;
        int value;
        Node *next;  // the link pointer to next item
    public: 
        Node(int x=0,int y=0,int z=0,Node * ptr=NULL); // constructor
        int getRow();
        int getColumn();
        int getValue();
        Node *getNext();
        void setData(int x,int y,int z);
        void setNext(Node *ptr);
    };  
Node::Node(int x,int y,int z, Node *p){ row=x;column=y;value=z; next=p;};
int Node::getRow( ){return row;};
int Node::getColumn( ){return column;};
int Node::getValue( ){return value;};
Node * Node::getNext( ){return next;};
void Node::setData(int x,int y,int z) {row=x;column=y;value=z;};
void Node::setNext(Node *ptr){next=ptr;};

class List {
    private:
        Node *head_ptr; Node *tail_ptr; int numOfItems;
    public: 
        List( ); // constructor
        int size( );  
        Node *getHead( );  
        Node *getTail( );
        bool isEmpty( );
        Node *itemAt(int position);
        void removeHead(); 
        void removeTail(); 
        void insertHead(int x,int y,int z);     
        void printList();
};
List::List( ){head_ptr= NULL; tail_ptr=NULL; numOfItems=0;};
int List::size( ){return numOfItems;};
Node * List::getHead( ) {return head_ptr;};
Node * List::getTail( ) {return tail_ptr;};
bool List::isEmpty() {return (numOfItems==0);};

Node *List::itemAt(int position){
    if (position<0 || position>=numOfItems) 
        return NULL;
    Node * currentPtr = getHead( );
    for(int k=0;k != position; k++)
            currentPtr = currentPtr -> getNext( );
    return currentPtr;
};
void List::removeHead( ){
        if (numOfItems == 0)
            return;
        Node * currentPtr = getHead( );
        head_ptr=head_ptr->getNext( );
        delete currentPtr;
        numOfItems--;
    };
void List::removeTail( ){
        if (numOfItems == 0)
            return;
        if (head_ptr == tail_ptr){
            head_ptr=NULL; tail_ptr= NULL; 
            numOfItems=0; return; }
        Node * beforeLast = itemAt(numOfItems-2);
        beforeLast->setNext(NULL); // beforeLast becomes last
        delete tail_ptr;   // deletes the last object 
        tail_ptr=beforeLast;
         numOfItems--;
    };


void List::insertHead(int x,int y,int z){
        Node * newHead = new Node(x,y,z,head_ptr);
        head_ptr= newHead;
        if (tail_ptr == NULL) // only one item in list
           tail_ptr = head_ptr;
        numOfItems++;
};

void List::printList()
        {
           cout<<"Head_Ptr--";
           for (int i=0; i<numOfItems;i++)
               {Node *p;
                 p=itemAt(i);
                 cout<<"( "<<p->getRow()<<","<<p->getColumn()<<","<<p->getValue();
                 if (i==numOfItems-1)
                     cout<<")--";
                 else
                     cout<<")-->";

               };
           cout<<"Tait_Ptr"<<endl;
           cout<<"============================="<<endl;
        };

void createList(List &l,int n,int dimension)
{

    int row,e=0;
    int column;
    int value;
for (int i=0;i<n;i++)
  {
      cout<<"Enter the Data "<<i<<" of the list: "<<endl;

     cout<<"ROW= ";

     while (e==0)
     {
         cin>>row;

         if ((row>=0) &&(row<dimension))
             e=1;
         else
             cout<<"Out of range \nRow= ";
     };
        e=0;

     cout<<"COLUMN=";
     while (e==0)
     {
         cin>>column;
         if ((column>=0 )&&(column<dimension)) e=1;  else cout<<"Out of range \ncolumn= ";
     };
     e=0;

     cout<<"VALUE= ";
     cin>>value;

    l.insertHead(row,column,value);

  };
cout<<"========================================"<<endl;

};

void mlprint(List l, int n)
{
    Node *current;
    current=l.getHead();

    for (int i=0; i<n; i++)
   {
       for(int j=0; j<n;j++)
       {
           if ((current!=NULL)&&(current->getRow()==i && current->getColumn()==j))
           {
               cout<<current->getValue()<<" ";
               current=current->getNext();
           }
           else
               cout<<"0 ";
       }
       cout<<endl;

 }

};
void mlsorting(List &l)
{
int d1,d2,d3;
Node *current;
Node *current2;
for (int i=0; i<l.size()-1;i++)
{current=l.itemAt(i);
 current2=current->getNext();
for (int j=i+1;j<l.size();j++)
  {

      if ((((current->getRow())>(current2->getRow())))||(((current->getRow())==(current2->getRow()))&&((current->getColumn())>(current2->getColumn()))))
{
    d1=current->getRow();
    d2=current->getColumn();
    d3=current->getValue();
    current->setData(current2->getRow(),current2->getColumn(),current2->getValue());
current2->setData(d1,d2,d3);
};

current2=current2->getNext();
  };
};

};

void merge(List l1,List l2,List &answer)
{
    Node *p1=l1.getHead();
    Node *p2=l2.getHead();

    while ((p1!=NULL) || (p2!=NULL))
    {
     if ((p1!=NULL) && (p2!=NULL))
     {
        if ((p1->getRow()==p2->getRow())&&(p1->getColumn()==p2->getColumn() ))
        {
            answer.insertHead(p1->getRow(),p1->getColumn(),p1->getValue()+p2->getValue());
                p1=p1->getNext();
                p2=p2->getNext();
        }
        else if ((p1->getRow()<p2->getRow())||(( p1->getRow()==p2->getRow())&&(  p1->getColumn()<p2->getColumn()   )     ))
        {answer.insertHead(p1->getRow(),p1->getColumn(),p1->getValue());
            p1=p1->getNext();}
        else
        {answer.insertHead(p2->getRow(),p2->getColumn(),p2->getValue());
            p2=p2->getNext();}
     }
     else if ( (p1!=NULL) && (p2==NULL) )
     {
            answer.insertHead(p1->getRow(),p1->getColumn(),p1->getValue());
            p1=p1->getNext();
     }
     else
     {
         answer.insertHead(p2->getRow(),p2->getColumn(),p2->getValue());
            p2=p2->getNext();
     }
    };  

};

int main()
{
int n1,n2,m;
cout<<"Enter the dimension of the Matrix: ";
cin>>m;
cout<<"Enter the number of Items in the list1: ";
cin>>n1;
List l1,l2,sum;
createList(l1,n1,m);
l1.printList();mlsorting(l1);
l1.printList();
mlprint(l1, m);
cout<<"Enter the number of Items in the list2: ";
cin>>n2;
createList(l2,n2,m);
l2.printList();mlsorting(l2);
l2.printList();
mlprint(l2, m);
merge(l1,l2,sum);

sum.printList();mlsorting(sum);sum.printList();
cout<<"================================="<<endl;
cout<<endl;
cout<<endl;
l1.printList();
l2.printList();
sum.printList();cout<<"=======The first Matrix==========="<<endl;
mlprint(l1, m);cout<<"========The second Matrix=========="<<endl;
mlprint(l2, m);cout<<"===The sum of first and second Matrix==="<<endl;
mlprint(sum,m);cout<<"=============================="<<endl;

    return 0;
}
4
Contributors
4
Replies
32
Views
1 Year
Discussion Span
Last Post by startomana
Featured Replies
  • 1

    For the sake of readability, let's start by separating the class declarations and implementations from the main program, and get the whole into some semblance of a consistent indentation: **matrix-list.h** #ifndef MATRIX_LIST_H #define MATRIX_LIST_H 1 class Node { private: int row; // different data type for other apps int column; … Read More

0

So, what is your problem. Don't expect us to analyze 260+ lines of code without some indication of the errors you are getting, and where you think they may be.

1

For the sake of readability, let's start by separating the class declarations and implementations from the main program, and get the whole into some semblance of a consistent indentation:

matrix-list.h

#ifndef MATRIX_LIST_H
#define MATRIX_LIST_H 1

class Node
{
private:
    int row;      // different data type for other apps
    int column;
    int value;
    Node *next;   // the link pointer to next item

public: 
    Node(int x=0,int y=0,int z=0,Node * ptr=NULL); // constructor
    int getRow();
    int getColumn();
    int getValue();
    Node *getNext();
    void setValue(int z);
    void setNext(Node *ptr);
};

class List
{
private:
    Node *head_ptr;
    Node *tail_ptr;
    int numOfItems;

public: 
    List();               // constructor
    int size( );  
    Node *getHead( );  
    Node *getTail( );
    bool isEmpty( );
    Node *itemAt(int position);
    void removeHead(); 
    void removeTail(); 
    void insertHead(int x, int y, int z);     
    void printList();
};

#endif

matrix-list.cpp

#include <iostream>
#include "matrix-list.h"

Node::Node(int x, int y, int z, Node *p):
    row(x), column(y), value(z), next(p)
{
    return;
}

int Node::getRow()
{
    return row;
}

int Node::getColumn()
{
    return column;
}

int Node::getValue()
{
    return value;
}

Node * Node::getNext()
{
    return next;
}
void Node::setValue(int z)
{
    value = z;
}

void Node::setNext(Node *ptr) {next=ptr;};


List::List()
{
    head_ptr= NULL;
    tail_ptr=NULL;
    numOfItems=0;
}

int List::size()
{
    return numOfItems;
}

Node * List::getHead()
{
    return head_ptr;
}

Node * List::getTail()
{
    return tail_ptr;
}

bool List::isEmpty()
{
    return (numOfItems==0);
}

Node *List::itemAt(int position)
{
    if (position < 0 || position >= numOfItems) 
        return NULL;
    Node * currentPtr = getHead( );
    for(int k=0; k != position; k++)
        currentPtr = currentPtr->getNext();
    return currentPtr;
}

void List::removeHead( )
{
    if (numOfItems == 0)
        return;
    Node * currentPtr = getHead();
    head_ptr=head_ptr->getNext();
    delete currentPtr;
    numOfItems--;
}

void List::removeTail( )
{
    if (numOfItems == 0)
        return;
    if (head_ptr == tail_ptr)
    {
        head_ptr=NULL; tail_ptr= NULL; 
        numOfItems=0; return;
    }

    Node * beforeLast = itemAt(numOfItems-2);
    beforeLast->setNext(NULL); // beforeLast becomes last
    delete tail_ptr;   // deletes the last object 
    tail_ptr=beforeLast;
    numOfItems--;
}


void List::insertHead(int x,int y,int z)
{
    Node * newHead = new Node(x,y,z,head_ptr);
    head_ptr= newHead;
    if (tail_ptr == NULL) // only one item in list
        tail_ptr = head_ptr;
    numOfItems++;
}

void List::printList()
{
    cout<<"Head_Ptr--";
    for (int i=0; i<numOfItems; i++)
    {
        Node *p;
        p=itemAt(i);
        std::cout << "( "
                  << p->getRow()
                  << "," << p->getColumn()
                  << "," << p->getValue()
                  << ")";
        if (i==numOfItems-1)
            std::cout<<"--";
        else
            std::cout<<"-->";

    }
    std::cout << "Tail_Ptr" << std::endl;
    std::cout << "=============================" << std::endl;
}

matrix-multiplication.cpp

#include <iostream>
#include "matrix-list.h"

using namespace std;

void createList(List &l,int n,int dimension)
{

    int row,e=0;
    int column;
    int value;
    for (int i=0;i<n;i++)
    {
        cout<<"Enter the Data "<<i<<" of the list: "<<endl;

        cout<<"ROW= ";

        while (e==0)
        {
            cin>>row;

            if ((row>=0) &&(row<dimension))
                e=1;
            else
                cout<<"Out of range \nRow= ";
        };
        e=0;

        cout<<"COLUMN=";
        while (e==0)
        {
            cin>>column;
            if ((column>=0 )&&(column<dimension)) e=1;  else cout<<"Out of range \ncolumn= ";
        };
        e=0;

        cout<<"VALUE= ";
        cin>>value;

        l.insertHead(row,column,value);

    };
    cout<<"========================================"<<endl;

};

void mlprint(List l, int n)
{
    Node *current;
    current=l.getHead();

    for (int i=0; i<n; i++)
    {
        for(int j=0; j<n;j++)
        {
            if ((current!=NULL)&&(current->getRow()==i && current->getColumn()==j))
            {
                cout<<current->getValue()<<" ";
                current=current->getNext();
            }
            else
                cout<<"0 ";
        }
        cout<<endl;

    }

};
void mlsorting(List &l)
{
    int d1,d2,d3;
    Node *current;
    Node *current2;
    for (int i=0; i<l.size()-1;i++)
    {
        current=l.itemAt(i);
        current2=current->getNext();
        for (int j=i+1;j<l.size();j++)
        {
            if ((((current->getRow())>(current2->getRow())))||(((current->getRow())==(current2->getRow()))&&((current->getColumn())>(current2->getColumn()))))
            {
                d1=current->getRow();
                d2=current->getColumn();
                d3=current->getValue();
                current->setData(current2->getRow(),current2->getColumn(),current2->getValue());
                current2->setData(d1,d2,d3);
            };

            current2=current2->getNext();
        };
    };

};

void merge(List l1,List l2,List &answer)
{
    Node *p1=l1.getHead();
    Node *p2=l2.getHead();

    while ((p1!=NULL) || (p2!=NULL))
    {
        if ((p1!=NULL) && (p2!=NULL))
        {
            if ((p1->getRow()==p2->getRow())&&(p1->getColumn()==p2->getColumn() ))
            {
                answer.insertHead(p1->getRow(),p1->getColumn(),p1->getValue()+p2->getValue());
                p1=p1->getNext();
                p2=p2->getNext();
            }
            else if ((p1->getRow()<p2->getRow()) || (( p1->getRow()==p2->getRow())&&(  p1->getColumn()<p2->getColumn())))
            {
                answer.insertHead(p1->getRow(),p1->getColumn(),p1->getValue());
                p1=p1->getNext();
            }
            else
            {
                answer.insertHead(p2->getRow(),p2->getColumn(),p2->getValue());
                p2=p2->getNext();
            }
        }
        else if ( (p1!=NULL) && (p2==NULL) )
        {
            answer.insertHead(p1->getRow(),p1->getColumn(),p1->getValue());
            p1=p1->getNext();
        }
        else
        {
            answer.insertHead(p2->getRow(),p2->getColumn(),p2->getValue());
            p2=p2->getNext();
        }
    };  

};

int main()
{
    int n1,n2,m;
    cout<<"Enter the dimension of the Matrix: ";
    cin>>m;
    cout<<"Enter the number of Items in the list1: ";
    cin>>n1;
    List l1,l2,sum;
    createList(l1,n1,m);
    l1.printList();mlsorting(l1);
    l1.printList();
    mlprint(l1, m);
    cout<<"Enter the number of Items in the list2: ";
    cin>>n2;
    createList(l2,n2,m);
    l2.printList();mlsorting(l2);
    l2.printList();
    mlprint(l2, m);
    merge(l1,l2,sum);

    sum.printList();mlsorting(sum);sum.printList();
    cout<<"================================="<<endl;
    cout<<endl;
    cout<<endl;
    l1.printList();
    l2.printList();
    sum.printList();cout<<"=======The first Matrix==========="<<endl;
    mlprint(l1, m);cout<<"========The second Matrix=========="<<endl;
    mlprint(l2, m);cout<<"===The sum of first and second Matrix==="<<endl;
    mlprint(sum,m);cout<<"=============================="<<endl;

    return 0;
}
Comments
Schol-R-Lea, you are much more patient that I am!
0

Now then, to repeat rubberman's question: what problem are you actually having with this code, and where is it occurring?

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.