Sparse Array !


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Sparse Array !

  1. #1
    Join Date
    Oct 2005
    Posts
    12

    Sparse Array !

    Hello ..
    I've tried to solve this prob. but I could not :(

    Assume that you have a doubly dimention sparse array A[6][6]
    List is a class represinting the value A[i][j], if different to zero ..

    Let list *row[6] be an array of pointers such that :
    row[k] is a pointer on the first element of the list of elemints different to zero in the row k of the matrix A ..


    And this is my trial ..


    PHP Code:
    # include<iostream.h>

    class node{
    public :
    int data;
    int i,j;
    node *next;
    node(){ next=0; }
    nodeint d,int i,int j,node *p)
    {
    data=d;
    i=i;
    j=j;
    next=p;
    }

    };

    /////////////////////////////

    class list{
    public :
    list(){ 
    head=tail=0; }

    int isempty(){
    if(
    head==0)
    return 
    1;
    else
    return 
    0;
    }

    void addtotail(int,int,int);


    void print()const;

    int size();


    private:
    node *head,*tail;

    };

    void list::addtotail(int d,int i,int j)
    {
    if(
    tail!=0)
    {
    tail->next=new node(d,i,j,0);
    tail=tail->next;
    }
    else
    head=tail=new node(d,i,j,0);
    }



    void list::print() const
    {
    for(
    node *q=head;q!=0;q=q->next)
    cout<<q->data<<"["<<q->i<<"]"<<"["<<q->j<<"]"<<"  ";
    cout<<endl;
    cout<<endl;
    }

    //////////////////////


    int list::size(){
    int c=0;
    for(
    nodep=head;p!=0;p=p->next)
    c++;
    return 
    c;

    }

    ////////////////////////////////




    void main()
    {
    int a[6][6];
    for(
    int i=1;i<6;i++)
    {
    for(
    int j=1;j<6;j++)
    a[i][j]=1;
    }

    a[1][1]=0;
    a[2][2]=0;
    a[2][3]=0;
    a[3][5]=0;
    a[4][4]=0;


    for(
    int k=1;k<6;k++)
    {
    for(
    int l=1;l<6;l++)
    cout<<a[k][l]<<"  ";
    cout<<endl;
    }

    list *
    l[6];
    for(
    int z=1;z<6;z++){
    for(
    int q=1;q<6;q++)
    {
    if(
    a[z][q]!=0)
    l[z]->addtotail(a[z][q],z,q);

    }
    l[z]->print();
     }




  2. #2
    Join Date
    Oct 2005
    Posts
    12
    Plese Help :(

  3. #3
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    Code:
    ...
    for(int z=1;z<6;z++){   	l[z] = new list();
    ...
    this is required, but i didnt test rest of it

  4. #4
    Join Date
    Oct 2005
    Posts
    12
    Well ..

    I did it .. But it seems that there is an error in printing !! od in adding to tail

    this is the result .. But is totally wrong ..


  5. #5
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    Code:
    node( int d,int i,int j,node *p) 
    { 
    data=d; 
    i=i; // problem
    j=j; // problem
    next=p; 
    }
    there is a problem in your code,
    i and j are local variables not member variables in that scope.
    so your node's i and j are not initalised, they are randomise.
    change parameter names or member variable's names

  6. #6
    Join Date
    Oct 2005
    Posts
    12
    Thank You Soooo Much :D

    it is working now =)

Similar Threads

  1. Dynamically allocating a 2d array
    By nnp in forum C++
    Replies: 6
    Last Post: 11-01-2005, 10:05 AM
  2. Indexing an array
    By java50031 in forum Java
    Replies: 16
    Last Post: 10-17-2005, 02:26 PM
  3. Replies: 2
    Last Post: 04-15-2005, 10:06 PM
  4. SafeArrayCopy SLOWER than iterating string array!
    By Mark Alexander Bertenshaw in forum VB Classic
    Replies: 10
    Last Post: 06-16-2000, 06:34 AM
  5. SafeArrayCopy SLOWER than iterating string array!
    By Mark Alexander Bertenshaw in forum VB Classic
    Replies: 0
    Last Post: 06-12-2000, 09:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center