Sparse matrix

This is a discussion on Sparse matrix within the C++ Programming forums, part of the General Programming Boards category; hello. Everybody Im new in programming. Still learning. Currently I have a problem when designing a Sparse Matrix class. Class ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    2

    Sparse matrix

    hello. Everybody Im new in programming. Still learning. Currently I have a problem when designing a Sparse Matrix class. Class is based on a linked list which records only the non-zero elements. Source file seems to compile fine, but at a run it crashes. Debug reveals a segmentation fault, which is as far as I know connected with inproper memory allocation. However I don't seem to find the problem. This is just a base class which allows to insert non-zero elements and display them. I am using bloodshed Dev C++. Any help would be appreciated. thanks Milenkom.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Sparse
    {
        private:
            class Elem
            {
                private:
                    int row,column;
                    double*elem;
                    Elem*next;
                public:
                    Elem(){next=NULL;}
                    Elem(int a,int b,double h);
                    ~Elem(){delete elem;}
                    Elem*& Next(){return next;}
                    double& Value()const{return *elem;}
                    int& Row(){return row;}
                    int& Column(){return column;}
            };
            Elem*first;
            int rows;
            int columns;
        public:
            Sparse(int,int);
            ~Sparse();
            void Insert(int a,int b,double h);
            friend ostream& operator<<(ostream&,Sparse&);                 
    };
    
    Sparse::Elem::Elem(int a,int b,double h)
    {
        elem=new double;
        *elem=h;
        next=NULL;
        row=a;
        column=b;   
    }
    
    Sparse::Sparse(int a,int b):rows(a),columns(b)
    {
        
    }
    
    Sparse::~Sparse()
    {
        Sparse::Elem*handy=first;
        Sparse::Elem*prev;
        while(handy!=NULL)
        {
            prev=handy;
            handy=handy->Next();
            delete prev;
        }
    }
    
    
    void Sparse::Insert(int a,int b,double h)
    {
            Sparse::Elem*prvi=new Sparse::Elem(a,b,h);
            Sparse::Elem*handy;
            if(first==NULL)
            {
                first=prvi;
            }
            else
            {
                handy=first;
                while(handy->Next()!=NULL)
                {
                    handy=handy->Next();
                }
                handy->Next()=prvi;
            } 
    }
    
    ostream& operator<<(ostream &os,Sparse &s)
    {
        for(int j=1;j<=s.columns;j++)
        {
            for(int i=1;i<=s.rows;i++)
                if(s.first->Column()==j && s.first->Row()==i)
                {
                    cout<<s.first->Value()<<"  ";
                    s.first=s.first->Next();
                }
                else
                cout<<0<<"  ";
                cout<<"\n";
        }
        return os;
    }
    
    
    int main()
    {
        Sparse s1(10,10);
        s1.Insert(1,1,10);
        s1.Insert(2,1,20);
        s1.Insert(3,2,30);
        cout<<s1;
        system("PAUSE");
        return 0;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It looks like the constructor of Sparse leaves Elem* first uninitialized (it won't be 0).

    There are other strangeness, such as allocating the double dynamically in Elem. Any reason why you don't use the standard containers for the underlying low level stuff?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    You could be more consistent with your use of constructor initialiser lists and use them in Elem's constructor too. Initialise all variables that you can this way.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    2
    Don't worry I already fixed the problem. I also overloaded (int,int) operator which really simplifies the code for ostream<< operator on few lines.

    I could use member initializer list, but its not really necessary. Currently i am still learning, although almost at end. The book i used Primer C++ is very thourough, but the examples are too simple Im afraid. Thats why Im trying to solve more demanding examples (this one is from Sharam Hekmat).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Incidentally,
    Code:
    ostream& operator<<(ostream &os,Sparse &s)
    should be:
    Code:
    ostream& operator<<(ostream &os, const Sparse &s)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Sparse Matrix
    By brb9412 in forum C Programming
    Replies: 3
    Last Post: 01-02-2009, 12:12 PM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21