Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem in loop. Please Help. Urgent!!

Status
Not open for further replies.

jjbuchan

Technical User
Aug 2, 2004
3
GB
The code below will print out:

Row: 0, Col: 0, p: 0, m: 0
Row: 0, Col: 1, p: 0, m: 0
Row: 0, Col: 2, p: 0, m: 0

Row: 1, Col: 0, p: 0, m: 0
Row: 1, Col: 1, p: 0, m: 0
Row: 1, Col: 2, p: 0, m: 0

Row: 2, Col: 0, p: 0, m: 0
Row: 2, Col: 1, p: 0, m: 0
Row: 2, Col: 2, p: 0, m: 0

Row: 0, Col: 3, p: 3, m: 0
Row: 0, Col: 4, p: 3, m: 0
Row: 0, Col: 5, p: 5, m: 0
Row: 0, Col: 6, p: 5, m: 0
Row: 0, Col: 7, p: 5, m: 0

etc.

Can someone please let me know why p suddenly changes to 5 (everything is fine before that). This is really annoying me and has to be sorted tonight.

Thanks in advance.


Code:
int row, col, n, m, p, q;
  Cellgroup cgroup;
  Coord co;
  m = 0; 
  p = 0;
  q = 0;
  n = 0;


while(m<=6) {
      while(p<=6) {
          for(row=m; row<m+3; row++) {
              for(col=p; col<p+3; col++) {
                  co.r = row;
                  co.c = col;
                  cgroup.grp[q] = co;
                  q++;
                  printf("Row: %d, Col: %d, p: %d, m: %d\n",row, col, p, m); 
              }
              printf("\n");
          }
          group[n] = cgroup;
          n++;
          p+=3;
      }
      p=0;
      m+=3;
  }
 
What is cellgroup defined as? At a guess, q has exceeded the bounds of the array and started overwriting the rest of the local variables in the stack.
 
See your snippet:
Code:
  p = 0;
...
  while(p<=6) {
     ....
     p = 0;
  }
Of course, it's loop forever...
 
Nevermind I sorted it.

There wasn't a never ending loop. It was that q would go upto 81, but the max stored within grp is 9.

A simple q = 0 in the right place solved it.
 
Sorry, p=0 was after loop. I was wrong...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top