Thread: Problem in loop. Please Help. Urgent!

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    Problem in loop. Please Help. Urgent!

    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;
      }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    remove Cellgroup and coord, the program runs ok with VC++ 6.0 compiler. Maybe there is a buffer overflow in Cellgroup.grp array that is trashing memory.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    Thanks that helped. But the thing is I am only meant to be using one global variable - group.

    I noticed it will work if you only take Cellgroup out of the method and leave Coord, however is there anyway you know of that I could keep both within the method?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    post the Cellgroup structure. How big is the grp array? It should be something like this. Note that grp array must be at least 82, because the values of q range from 0 to 81.
    Code:
    struct Coord
    {
    	int r,c;
    };
    
    struct Cellgroup
    {
    	Coord grp[82];
    };

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    Oh yeh, i never noticed that before. thanks
    Last edited by jjbuchan; 11-10-2005 at 04:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. Replies: 8
    Last Post: 12-09-2008, 12:07 PM
  4. while loop problem
    By tortan in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2006, 11:11 AM
  5. Segmentation Fault Problem: Urgent Help
    By bodydrop in forum C Programming
    Replies: 3
    Last Post: 05-05-2006, 08:02 PM