views:

567

answers:

11

Why the following code could/will result in infinite loop?

  x=0;
  y=0;

  while(x<100)
  {
    minnum=100;
    maxnum=100;
    while(y<150)
    {
    if(random[x][y]<minnum)
      {
      minnum=random[x][y];
      minX=x;
      minY=y;
      y++;
      }
    else if(random[x][y]>maxnum)
      {
      maxnum=random[x][y];
      maxX=y;
      maxY=y;
      y++;
      }
    }
    x++;
    y=0;
  }
+9  A: 

If both random[x][y]<minnum and random[x][y]>maxnum are false, you'll never exit out of the inner loop. More specifically if random[x][y] == 100 you're toast the first time through.

As a related question, what behavior do you see when you debug the source you've posted?

fbrereto
This is not correct - it doesn't matter what the values of either of the conditions of the if-then-else are ... it is still an infinite loop. y is reset to 0 each iteration of the loop, so it never goes above 0 (and never reaches 150)
Larry Watanabe
Sorry, mislead by the formatting - ignore my comment
Larry Watanabe
@Larry Watanabe: better than 'ignore my comment' is 'you delete your own erroneous comment'.
Jonathan Leffler
A: 

On the inner while loop, if random[x][y] == minnum, then y will never be incremented and the program will go into an infinite loop.

Chris Bunch
+1  A: 

If neither of the if and else if in the inner loop is satisfied, you never increment y, thus making that inner loop undending. Looks like the y++ should be OUTSIDE of any conditional statements.

Alex Martelli
This is not correct - it doesn't matter what the values of either of the conditions of the if-then-else are ... it is still an infinite loop. y is reset to 0 each iteration of the loop, so it never goes above 0 (and never reaches 150)
Larry Watanabe
Sorry, mislead by the formatting - ignore my comment (My mind only works when i have 4 char indentation :)
Larry Watanabe
A: 

If minnum <= random[x][y] <= maxnum then y is never incremented in the inner loop. Probably you should increment y in any case, even if none of the if-conditions are met. Best move the y++ outside of the ifs before the closing brace of the while loop.

You could also use for-loops to iterate over the possible values for x and y, which would reduce the probability of such mistakes.

sth
A: 

If random[x][y] is ever exactly 100, this will loop infinitely, since it never does anything in that case.

Reed Copsey
A: 

Here is my vision of this cycle - some conserns about how to write "good" code (from my point of view - I'm really not a guru in programming) - not essential to follow them, but I think they are not so bad.

  // It's a good rule - to initialize minimum and maximum with one
  // (any) element from the array you will be working with
  minnum=random[0][0];
  maxnum=random[0][0];

  // This is my hamble opinion but I'll prefer to have variables for array sizes
  sizeX=100;
  sizeY=150;

  // Cannot you use for instead of while?
  for(x = 0; x < sizeX; ++x)
      for(y = 0; y < sizeY; ++y)
      {
          if(random[x][y] < minnum)
          {
              minnum = random[x][y];
              minX = x;
              minY = y;
          }
          else if(random[x][y] > maxnum)
          {
              maxnum = random[x][y];
              maxX = x;
              maxY = y;
          }
    }
Mihail
maxX = y; *ahem*
Dave Jarvis
@Dave: Thanks for finding it! I've fixed this misprint...
Mihail
A: 

The problem is that you only promote y when random is smaller than minnum or larger than maxnum, creating a problem where neither occur.

But why do you promote it only if those conditions occur? You want y to be promoted regardless of those conditions.

You'll probably want to change to:

while(y<150)
{
if(random[x][y]<minnum)
  {
  minnum=random[x][y];
  minX=x;
  minY=y;
  }
else if(random[x][y]>maxnum)
  {
  maxnum=random[x][y];
  maxX=y;
  maxY=y;
  }
  y++;
}

This way both y and x are always promoted.

Liran Orevi
A: 

If you replace your while loops with for loops, your code will make more sense, be less error-prone, and this will solve your infinite loop problem:

minnum=100;
maxnum=100;
for (int x = 0; x<100; x++)
{
  for(int y=0; y<150; y++)
  {
    if(random[x][y]<minnum)
    {
      minnum=random[x][y];
      minX=x;
      minY=y;
    }
    else if(random[x][y]>maxnum)
    {
      maxnum=random[x][y];
      maxX=y;
      maxY=y;
    }
  }
}
Tim
A: 

I wonder if professors in universities now should know that students can go online like this and get the answers?

Anyway like everyone been saying if random[x][y]==100, then this will always be in the inside loop.

Daniel
+2  A: 

There were many good answers to the original question, but even so none of them will work! That's because there is a dutifully-preserved typo in the second "if" block -- should read

maxX = x; // not y!
maxY = y;
Sherm
Yes, but that was the trivial error `)
Liran Orevi
Also, it doesn't matter since [min|max][X|Y] is never used other than being assigned to. They're not causing any infinite loop.
Jim Buck
+1  A: 

This will enter an infinite loop any time your array value is between minnum and maxnum. Initially, this is only at 100. One the second pass, is is the difference between the minimum and maximum value you have seen. If the value is 3000 for instance, you will have minnum of 100 and maxnum of 3000. Now, any number between 100 and 3000 (inclusive) will cause an infinite loop. See other answers for how to fix the code.

Jacob

TheJacobTaylor