weird loop behaviour

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dynamo
    New Member
    • Apr 2007
    • 51

    weird loop behaviour

    hi everyone,i dont understand the behaviour of my generate function using a nested loop that performs my insert function repeatedly.Inst ead of inserting the value of i,it seems to insert the maximum value of j
    repeatedlyi.e 7 where j<=7 for example.I think the problem might be with my insert function,howeve r i see no problems.Can you help?

    Code:
    #include <iostream>
    #include <iostream>  
    #include <string>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    
    class SparseMat{
          public:
          struct link{
                 int num;
                 link *next;
                 int posx;
                 int posy;
                 };
                 SparseMat(int sizex1,int sizey1);
                 SparseMat();
                 SparseMat(const SparseMat &m);
                 link *first;
                 int sizex;
                 int sizey;
                 int state;
                 int exists(int x,int y);
                 void insert(int n,int posx,int posy);
                 int numAt(int posx,int posy);
                 SparseMat operator +(SparseMat b);
                 SparseMat operator -(SparseMat b);
                 SparseMat operator *(SparseMat b);
                 SparseMat& operator =(const SparseMat &m);
                 };
                SparseMat::SparseMat(int sizex1,int sizey1){
                              state=0;
                              sizex=sizex1;
                              sizey=sizey1;
                              }
                SparseMat::SparseMat(){
                              state=0;
                              }
                              void SparseMat::insert(int n,int posx,int posy){
                                   link *current;
                                   if(state==0){
                                                state=1;
                                                first=new link();
                                                first->num=n;
                                                first->next=NULL;
                                                first->posx=posx;
                                                first->posy=posy;
                                                }
                                                else{
                                                     current=new link();
                                                     current->num=n;
                                                     current->posx=posx;
                                                     current->posy=posy;
                                                     current->next=first;
                                                     first=current;
                                                     }
                                                     }
                                         int SparseMat::numAt(int posx,int posy){
                                             link *current=first;
                                             for(int i=0;current->posx!=posx&&current->posy!=posy;i++){
                                                     current= current->next;
                                                     if(current==NULL){
                                                                       break;
                                                                       }
                                                     }
                                                   return  current->num;   
                                                   } 
    int SparseMat::exists(int x,int y){
        link *current=first;
        int state=0;
        for(int i=0;current!=NULL;i++){
                if(current->posx==x&&current->posy==y){
                                                       state=1;
                                                       break;
                                                       }
              current=current->next;
              }
              return state;
              }
      SparseMat SparseMat::operator +(SparseMat b){
                int n;
                SparseMat res(sizex,sizey);
              if(sizex==b.sizex&&sizey==b.sizey){
                                                 for(int i=0;i<=sizex;i++){
                                                         for(int j=0;j<=sizey;j++){
                                                                 if(exists(i,j)==1&&b.exists(i,j)==1){
                                                                     n=numAt(i,j)+b.numAt(i,j);
                                                                      res.insert(n,i,j);
                                                                      }                                                                             
                                   if(exists(i,j)==1&&b.exists(i,j)!=1){
                                                                     n=numAt(i,j)+0;
                                                                      res.insert(n,i,j);
                                                                      }
                                   if(exists(i,j)!=1&&b.exists(i,j)==1){
                                                                     n=0+b.numAt(i,j);
                                                                      res.insert(n,i,j);
                                                                      }
                                                                      }
                                                                      }
                                                                      }
                                                                      else{
                                                                           cout<<"error";
                                                                           }
                                                                           return res;
                                                                           }
      SparseMat SparseMat::operator -(SparseMat b){
                int n;
                SparseMat res(sizex,sizey);
              if(sizex==b.sizex&&sizey==b.sizey){
                                                 for(int i=0;i<=sizex;i++){
                                                         for(int j=0;j<=sizey;j++){
                                                                 if(exists(i,j)==1&&b.exists(i,j)==1){
                                                                     n=numAt(i,j)-b.numAt(i,j);
                                                                      res.insert(n,i,j);
                                                                      }                                                                             
                                   if(exists(i,j)==1&&b.exists(i,j)!=1){
                                                                     n=numAt(i,j)-0;
                                                                      res.insert(n,i,j);
                                                                      }
                                   if(exists(i,j)!=1&&b.exists(i,j)==1){
                                                                     n=0-b.numAt(i,j);
                                                                      res.insert(n,i,j);
                                                                      }
                                                                      }
                                                                      }
                                                                      }
                                                                      else{
                                                                           cout<<"error";
                                                                           }
                                                                           return res;
                                                                           } 
     SparseMat SparseMat::operator *(SparseMat b){
               SparseMat res(sizex,sizey);
               int n=0;
               if(sizex==b.sizex&&sizey==b.sizey){
           int rarr[sizex];
           int carr[sizey];
           for(int i=0;i<=sizey;i++){
                   for(int j=0;j<=sizex;j++){
                           if(exists(j,i)==1){
                           rarr[j]=numAt(j,i);
                           }
                           else{
                                rarr[j]=0;
                                }
                           }
                   for(int l=0;l<=sizex;l++){
                   for(int k=0;k<=sizey;k++){
                           if(b.exists(l,k)==1){
                           carr[k]=b.numAt(l,k);
                           }
                           else{
                                carr[k]=0;
                           }
                           } 
                   for(int m=0;m<=sizex;m++){
                              n= rarr[m]*carr[m]+n;
                               }
                   res.insert(n,l,i);
                   }
                   }
                   }
                   else{
                        cout<<"error";
                        }
                        return res;
                        } 
                       SparseMat::SparseMat(const SparseMat &m){
           first=m.first;
                  sizex=m.sizex;
                  sizey=m.sizey;
                  state=m.state;
                 }
                 SparseMat& SparseMat::operator =(const SparseMat &m){
                                    first=m.first;
                  sizex=m.sizex;
                  sizey=m.sizey;
                  state=m.state;
                  return *this;
                  }
                  
                  
     class LinearEqns:public SparseMat{
           public:
           SparseMat xh;
           LinearEqns(SparseMat x):SparseMat(){
                                xh=x;
                                }
           void generate(int coef);
           void solve();
           };
      void     LinearEqns::generate(int coef){
                                   
                                    this->SparseMat::sizex=coef+1;
                                    this->SparseMat::sizey=xh.sizey;
            
                   for(int j=0;j<=7;j++){
                           
                         for(int i=0;i<=7;i++){  
                           this->SparseMat::insert(j,i,j);
                           
                           }
                           }
                           }
     int main(){
         int x;
         SparseMat h(2,3);
         h.insert(1,0,0);
         h.insert(2,0,1);
         LinearEqns l(h);
         l.generate(5);
         cout<<l.numAt(1,2);
         cin>>x;
         }
    Last edited by JosAH; Jan 12 '09, 02:35 PM. Reason: added [code] ... [/code] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    The indentation of your code stinks; I added those code tags for you but it doesn't help much; your code is unreadable. Please fix the indentation first before you want us to plough through all that code.

    kind regards,

    Jos

    Comment

    Working...