M
M
mottoper2014-09-17 22:02:48
C++ / C#
mottoper, 2014-09-17 22:02:48

Why is the loop behaving so strangely?

In general, there is a loop within a loop that manipulates two-dimensional matrices:

for(int i = 0; i < x; ++i) {
    for(int j = 0; j < y; ++j) {
      int x1 = i, y1 = j;
      if(((abs(x1 - GO.x) == 1) || (abs(y1 - GO.y) == 1)) && *(desk+x1+y1))
        GOnbr[i][j] = abs(STAY.startX - x1) > abs(STAY.startY - y1) ? x-abs(STAY.startX - x1) : y-abs(STAY.startY - y1);
      else 
        GOnbr[i][j] = 0;
      if((i - STAY.x >= -1 && i - STAY.x <= 1) && (j - STAY.y >= -1 && j - STAY.y <= 1) && !(i - STAY.x == 0 && j - STAY.y == 0) && *(desk+i+j))
        STAYnbr[i][j] = 0;
      else 
        STAYnbr[i][j] = 1;
      GOnbr[i][j]*=STAYnbr[i][j];
      cout << j << ' ' << i << endl;
    }
  }

I apologize for the bad condition in the second ife, it should be the same as in the first, but it is identical to it.
So, for some reason this outputs:
...
0 0
1 0
2 0
0 1
1 1
2 1
0 2
1 2
2 2
0 3
1 3
2 3
0 0
1 0
2 0
0 1
1 1
2 1
0 2
1 2
2 2
...

And so on until you stop. Help me please. Tried with two compilers: clang and gcc 4.8. OS X 9

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tsarevfs, 2014-09-17
@tsarevfs

For such a code, I hope it does not repeat itself?

#include <iostream>
int main()
{
  int x = 3;
  int y = 3;

  for(int i = 0; i < x; ++i) 
  {
      for(int j = 0; j < y; ++j) 
      {
        std::cout << j << ' ' << i << std::endl;
      }
  }
  return 0;
}

If not, try making a minimal compilable example with the bug.

M
Maxim, 2014-09-17
@1kachan

please post the full source

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question