5 Replies - 5355 Views - Last Post: 14 March 2012 - 06:34 AM Rate Topic: -----

#1 ayyagari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-March 12

sparse matrix

Posted 13 March 2012 - 06:01 PM

I wrote program for sparse matrix or i think so...the required result is not getting printed please help me to...


#include <iostream>
using namespace std;



class sparse{
    
    
    
    int row;
    int column;
    int data;
    sparse *row_link;
    sparse *column_link;
public:
    sparse * create(sparse*);
    void display(sparse*);
    
    
};


sparse * sparse::create(sparse*h)
{
    cout<<"\nEnter the no. of rows ::";
    cin>>row;
    cout<<"Enter the no. of columns ::";
    cin>>column;
    
    
    // create the head node
    sparse *columnheader[column];
    sparse *rowheader[row];
    
    //create head node
    h=new sparse;
    h->row=row;
    h->column=column;
    h->data=0;
    h->row_link=columnheader[column];
    h->column_link=rowheader[row];
    
    
    //create rowheader
    
    sparse *ptr;
    int i,j,d;
    ptr=h;
    for (i=1;i<=row ; i++) {
        rowheader[i]=new sparse;
        rowheader[i]->row=i;
        rowheader[i]->column=0;
        rowheader[i]->row_link=h;
        rowheader[i]->column_link=rowheader[i];
            
   
        
        ptr->column_link=rowheader[i];
        ptr=rowheader[i];
        
        
        
    }
    
    //create columnheader
    
    ptr=h;
    for (i=1;i<=column ; i++) {
        columnheader[i]=new sparse;
        columnheader[i]->row=0;
        columnheader[i]->column=i;
        columheader[i]->row_link=columheader[i];
        columnheader[i]->column_link=h;
        
        ptr->row_link=columnheader[i];
        ptr=columnheader[i];
        
        
        
        
    }
    cout<<"\n now enter the non zero elements "
    <<"one by one\n";
    cout<<"\nEnter row number,column number,data\n";
    cout<<"Enter (0 0 0) to stop ::";
    cin>>i>>j>>d;
    if(i>row || j>column ||i<1 ||j<1)
    {
        cout<<" error input";
        exit(1);
    }
    while(i&&j&&d)
    {
        sparse * row_header=h->column_link;
        sparse * column_header=h->row_link;
        
        // find the correct row header and column header
        while(row_header->row<i)
            row_header=row_header->column_link;
        
        while(column_header->column<j)
            column_header=column_header->row_link;
        
        sparse *ptr1;
        sparse *ptr2;
        
        // find the correct position to insert
        sparse*row_ptr=row_header;
        while(row_ptr->column<j)
        {
            ptr1=row_ptr;
            row_ptr=row_ptr->row_link;
            if(row_ptr==row_header)
                break;
        }
        
        sparse*column_ptr=column_header;
        while(column_ptr->row<i)
        {
            ptr2=column_ptr;
            column_ptr=column_ptr->column_link;
            if(column_ptr==column_header)
                break;
        }
        
        sparse *node;
        node=new sparse;
        node->row=i;
        node->column=j;
        node->data=d;
        
        ptr1->row_link=node;
        ptr2->column_link=node;
        node->row_link=row_ptr;
        node->column_link=column_ptr;
        
        
        
        cout<<"\nEnter row number,column number,data\n";
        cout<<"Enter (0 0 0) to stop ::";
        cin>>i>>j>>d;
        if(i>row || j>column )
        {
            cout<<" error inpu";
            exit(1);
        }
    }
    return h;
}

void sparse::display(sparse*h)
{
    sparse *right=h->column_link;
    
    
    while(right->row!=h->column)
    {
        sparse *r=right;
        right=right->row_link;
        while(r->column !=h->row)
        {
            cout<<right->row
            <<'\t'<<right->column
            <<'\t'<<right->data<<endl;
            right=right->row_link;
        }
        right=right->column_link;
    }
}


int main()
{
    
    sparse s;
    sparse *h1=NULL;
    cout<<"Enter the values for sparse matrix \n";
    h1=s.create(h1);
    s.display(h1);
    
}




:code:

This post has been edited by r.stiltskin: 13 March 2012 - 06:38 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: sparse matrix

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: sparse matrix

Posted 13 March 2012 - 06:45 PM

You're going to make more of an effort to solve your problem. You can't expect people to read through your 180 line program, figure out what it's supposed to do, devise test data and a driver program to test it, discover for themselves what it's doing wrong and then fix it for you.

You should be asking a specific question about a specific problem. At least you should explain what's wrong, what output you expect, what output you get, why the output is wrong, where you think the error is, and so on.
Was This Post Helpful? 1
  • +
  • -

#3 ayyagari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-March 12

Re: sparse matrix

Posted 13 March 2012 - 06:51 PM

it should show only the row column and data in the matrix which have non zero value which is pointed by the row header and column header...the logic seems correct but in the display its getting into a loop where the header and column header values are only getting printed..i.e 3 3 0,0 1 0,0 2 0,0 3 0,3 3 0, 0 1 0 and so on...
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: sparse matrix

Posted 13 March 2012 - 09:52 PM

Can you explain how you are trying to represent your sparse matrix, in other words, what kind of data structure you're trying to implement?

I can't figure out what you're trying to do. It seems that in create you're creating an entire "rows x columns" array of sparse objects, so in what respect is this matrix "sparse"?

There are a number of errors, such as running beyond the bounds of your arrays (remember that array indexing goes from 0 to size-1). In fact the code as you posted it even has a few errors that prevent it from compiling. But it doesn't make sense to deal with those details if the underlying concept is wrong, so first please explain your algorithm.

This post has been edited by r.stiltskin: 13 March 2012 - 09:56 PM

Was This Post Helpful? 0
  • +
  • -

#5 ayyagari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-March 12

Re: sparse matrix

Posted 13 March 2012 - 10:21 PM

kk let me put it to you this way a array of row headers should point to row pointers in the matrix and a array of column headers should point to column pointers in the matrix...each element in the matrix should node which should contain row number, column number, data,row link which points all the rows in the matrix (should be linked back to rowheader) and column link which points all the column elements in the the matrix(should also be linked bak to column header)....only non zero elements are shown in the list...


now what i am doing is creating spares objects rows and columns and entering the data which allows only non zero values and exists if zero....first depending on the row and column value the header files are identified and a sparse object is created having the vales of row,column and data which are pointed by row link and column link....actually the loops are running fine but i donít no whats going wrong and i feel my logic is correct...please help me

View Postr.stiltskin, on 13 March 2012 - 09:52 PM, said:

Can you explain how you are trying to represent your sparse matrix, in other words, what kind of data structure you're trying to implement?

I can't figure out what you're trying to do. It seems that in create you're creating an entire "rows x columns" array of sparse objects, so in what respect is this matrix "sparse"?

There are a number of errors, such as running beyond the bounds of your arrays (remember that array indexing goes from 0 to size-1). In fact the code as you posted it even has a few errors that prevent it from compiling. But it doesn't make sense to deal with those details if the underlying concept is wrong, so first please explain your algorithm.


kk let me put it to you this way a array of row headers should point to row pointers in the matrix and a array of column headers should point to column pointers in the matrix...each element in the matrix should node which should contain row number, column number, data,row link which points all the rows in the matrix (should be linked back to rowheader) and column link which points all the column elements in the the matrix(should also be linked bak to column header)....only non zero elements are shown in the list...


now what i am doing is creating spares objects rows and columns and entering the data which allows only non zero values and exists if zero....first depending on the row and column value the header files are identified and a sparse object is created having the vales of row,column and data which are pointed by row link and column link....actually the loops are running fine but i donít no whats going wrong and i feel my logic is correct...please help me
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: sparse matrix

Posted 14 March 2012 - 06:34 AM

You should use a debugger or add some cout statements to see if any of your data input ends up where it belongs.

Here are a few problems that stand out:

Where you create the "head node":
    h->row_link=columnheader[column];  // columnheader[column] is outside the array
    h->column_link=rowheader[row]; // rowheader[row] is outside the array



Where you create the rowheaders:
    for (i=1; i<=row ; i++) {    // should go from 0 to row-1



Where you create the columnheaders:
    for (i=1; i<=column ; i++) {  // should go from 0 to column-1



In the input loop:
/* This creates two new local pointers within the while loop.  These are not the class data members "row_header" and "column_header". */
        sparse * row_header=h->column_link;
        sparse * column_header=h->row_link;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1