473,785 Members | 3,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

weird loop behaviour

51 New Member
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?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iostream>  
  3. #include <string>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. class SparseMat{
  12.       public:
  13.       struct link{
  14.              int num;
  15.              link *next;
  16.              int posx;
  17.              int posy;
  18.              };
  19.              SparseMat(int sizex1,int sizey1);
  20.              SparseMat();
  21.              SparseMat(const SparseMat &m);
  22.              link *first;
  23.              int sizex;
  24.              int sizey;
  25.              int state;
  26.              int exists(int x,int y);
  27.              void insert(int n,int posx,int posy);
  28.              int numAt(int posx,int posy);
  29.              SparseMat operator +(SparseMat b);
  30.              SparseMat operator -(SparseMat b);
  31.              SparseMat operator *(SparseMat b);
  32.              SparseMat& operator =(const SparseMat &m);
  33.              };
  34.             SparseMat::SparseMat(int sizex1,int sizey1){
  35.                           state=0;
  36.                           sizex=sizex1;
  37.                           sizey=sizey1;
  38.                           }
  39.             SparseMat::SparseMat(){
  40.                           state=0;
  41.                           }
  42.                           void SparseMat::insert(int n,int posx,int posy){
  43.                                link *current;
  44.                                if(state==0){
  45.                                             state=1;
  46.                                             first=new link();
  47.                                             first->num=n;
  48.                                             first->next=NULL;
  49.                                             first->posx=posx;
  50.                                             first->posy=posy;
  51.                                             }
  52.                                             else{
  53.                                                  current=new link();
  54.                                                  current->num=n;
  55.                                                  current->posx=posx;
  56.                                                  current->posy=posy;
  57.                                                  current->next=first;
  58.                                                  first=current;
  59.                                                  }
  60.                                                  }
  61.                                      int SparseMat::numAt(int posx,int posy){
  62.                                          link *current=first;
  63.                                          for(int i=0;current->posx!=posx&&current->posy!=posy;i++){
  64.                                                  current= current->next;
  65.                                                  if(current==NULL){
  66.                                                                    break;
  67.                                                                    }
  68.                                                  }
  69.                                                return  current->num;   
  70.                                                } 
  71. int SparseMat::exists(int x,int y){
  72.     link *current=first;
  73.     int state=0;
  74.     for(int i=0;current!=NULL;i++){
  75.             if(current->posx==x&&current->posy==y){
  76.                                                    state=1;
  77.                                                    break;
  78.                                                    }
  79.           current=current->next;
  80.           }
  81.           return state;
  82.           }
  83.   SparseMat SparseMat::operator +(SparseMat b){
  84.             int n;
  85.             SparseMat res(sizex,sizey);
  86.           if(sizex==b.sizex&&sizey==b.sizey){
  87.                                              for(int i=0;i<=sizex;i++){
  88.                                                      for(int j=0;j<=sizey;j++){
  89.                                                              if(exists(i,j)==1&&b.exists(i,j)==1){
  90.                                                                  n=numAt(i,j)+b.numAt(i,j);
  91.                                                                   res.insert(n,i,j);
  92.                                                                   }                                                                             
  93.                                if(exists(i,j)==1&&b.exists(i,j)!=1){
  94.                                                                  n=numAt(i,j)+0;
  95.                                                                   res.insert(n,i,j);
  96.                                                                   }
  97.                                if(exists(i,j)!=1&&b.exists(i,j)==1){
  98.                                                                  n=0+b.numAt(i,j);
  99.                                                                   res.insert(n,i,j);
  100.                                                                   }
  101.                                                                   }
  102.                                                                   }
  103.                                                                   }
  104.                                                                   else{
  105.                                                                        cout<<"error";
  106.                                                                        }
  107.                                                                        return res;
  108.                                                                        }
  109.   SparseMat SparseMat::operator -(SparseMat b){
  110.             int n;
  111.             SparseMat res(sizex,sizey);
  112.           if(sizex==b.sizex&&sizey==b.sizey){
  113.                                              for(int i=0;i<=sizex;i++){
  114.                                                      for(int j=0;j<=sizey;j++){
  115.                                                              if(exists(i,j)==1&&b.exists(i,j)==1){
  116.                                                                  n=numAt(i,j)-b.numAt(i,j);
  117.                                                                   res.insert(n,i,j);
  118.                                                                   }                                                                             
  119.                                if(exists(i,j)==1&&b.exists(i,j)!=1){
  120.                                                                  n=numAt(i,j)-0;
  121.                                                                   res.insert(n,i,j);
  122.                                                                   }
  123.                                if(exists(i,j)!=1&&b.exists(i,j)==1){
  124.                                                                  n=0-b.numAt(i,j);
  125.                                                                   res.insert(n,i,j);
  126.                                                                   }
  127.                                                                   }
  128.                                                                   }
  129.                                                                   }
  130.                                                                   else{
  131.                                                                        cout<<"error";
  132.                                                                        }
  133.                                                                        return res;
  134.                                                                        } 
  135.  SparseMat SparseMat::operator *(SparseMat b){
  136.            SparseMat res(sizex,sizey);
  137.            int n=0;
  138.            if(sizex==b.sizex&&sizey==b.sizey){
  139.        int rarr[sizex];
  140.        int carr[sizey];
  141.        for(int i=0;i<=sizey;i++){
  142.                for(int j=0;j<=sizex;j++){
  143.                        if(exists(j,i)==1){
  144.                        rarr[j]=numAt(j,i);
  145.                        }
  146.                        else{
  147.                             rarr[j]=0;
  148.                             }
  149.                        }
  150.                for(int l=0;l<=sizex;l++){
  151.                for(int k=0;k<=sizey;k++){
  152.                        if(b.exists(l,k)==1){
  153.                        carr[k]=b.numAt(l,k);
  154.                        }
  155.                        else{
  156.                             carr[k]=0;
  157.                        }
  158.                        } 
  159.                for(int m=0;m<=sizex;m++){
  160.                           n= rarr[m]*carr[m]+n;
  161.                            }
  162.                res.insert(n,l,i);
  163.                }
  164.                }
  165.                }
  166.                else{
  167.                     cout<<"error";
  168.                     }
  169.                     return res;
  170.                     } 
  171.                    SparseMat::SparseMat(const SparseMat &m){
  172.        first=m.first;
  173.               sizex=m.sizex;
  174.               sizey=m.sizey;
  175.               state=m.state;
  176.              }
  177.              SparseMat& SparseMat::operator =(const SparseMat &m){
  178.                                 first=m.first;
  179.               sizex=m.sizex;
  180.               sizey=m.sizey;
  181.               state=m.state;
  182.               return *this;
  183.               }
  184.  
  185.  
  186.  class LinearEqns:public SparseMat{
  187.        public:
  188.        SparseMat xh;
  189.        LinearEqns(SparseMat x):SparseMat(){
  190.                             xh=x;
  191.                             }
  192.        void generate(int coef);
  193.        void solve();
  194.        };
  195.   void     LinearEqns::generate(int coef){
  196.  
  197.                                 this->SparseMat::sizex=coef+1;
  198.                                 this->SparseMat::sizey=xh.sizey;
  199.  
  200.                for(int j=0;j<=7;j++){
  201.  
  202.                      for(int i=0;i<=7;i++){  
  203.                        this->SparseMat::insert(j,i,j);
  204.  
  205.                        }
  206.                        }
  207.                        }
  208.  int main(){
  209.      int x;
  210.      SparseMat h(2,3);
  211.      h.insert(1,0,0);
  212.      h.insert(2,0,1);
  213.      LinearEqns l(h);
  214.      l.generate(5);
  215.      cout<<l.numAt(1,2);
  216.      cin>>x;
  217.      }
  218.  
Jan 12 '09 #1
1 1587
JosAH
11,448 Recognized Expert MVP
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
Jan 12 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
2066
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << " and &s = " << &s << "\n";
3
1658
by: s_m_b | last post by:
Anyone ever seen this before? A page that generates a calendar view, works just fine without 'option explicit' . Add this in, but have missed some dim statements out in error, so page should error . Instead of error, the un-dim-ed variables are ignored and their portions of the code simply are not used - behavour as though entire loops etc are commented out. As soon as I add their dim statements, the code executes fine.
8
1436
by: Deano | last post by:
Here's the code; Private Sub txtTeachName_LostFocus() If IsNull(Me.txtName) Then 'line A Forms!frmMainform!frmSubform.Locked = True GoTo Exit_txtName Else 'line B Forms!frmMainform!frmSubform.Locked = False 'line C
8
2393
by: Jeff | last post by:
Hello everybody, I was doing one of the exercises in the K&R book, and I got something really strange. Here's the source code: /* * Exercise 2-2 from the K&R book, page 42 */ #include <stdio.h>
1
1293
by: Pankaj | last post by:
Hi All, I use a Hashtable in my program to keep unique items...at one instance I need to repopulate this hashtable through a loop. when starting repopulation hashtable.count() returns ZERO hence first item gets added successfully but as soon as the control reaches top of foreach loop somehow a second item which indeed existed before repoulation started appears in hashtable and count returns two hence this second item can not add...
8
1539
by: Daniel Yelland | last post by:
Hi, I have developed a number of code libraries in Win32 DLLs and have written a number of test suite executables that implicitly link to these libraries in order to test them. In one of my test applications, which runs fine in Debug mode, it is crashing in the destructor of a local object on the stack when it is built in release mode. An example of the C++ that causes the problem is as follows (apologies for the contrived example): -
10
4029
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
12
2202
by: Mick_fae_Glesga | last post by:
OK, the solution to this is probably blindingly obvious to everyone, but... surely it can't be right. I am compiling with borland bcc32 free compiler this piece of code is designed to identify the most significant bit in a given element in an array of unsigned longs. Now I realise there may be a more efficient way to do this, and if you know a better way please let me know.
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.