Блог пользователя electro177

Автор electro177, история, 3 года назад, По-английски

while this is working fine ~~~~~ Your code here... bool emp (int i, int j) { if ( i >= 0 && i < n && j >= 0 && j < m && s[i][j] == '.' )return 1; else return 0;

} ~~~~~

this is giving run time error ~~~~~ Your code here...

bool emp (int i, int j) { if ( s[i][j] == '.' && i >= 0 && i < n && j >= 0 && j < m )return 1; else return 0;

} ~~~~~

does anyone know the reason

  • Проголосовать: нравится
  • -20
  • Проголосовать: не нравится

»
3 года назад, # |
Rev. 2   Проголосовать: нравится +17 Проголосовать: не нравится

It happens because operator&& is calculated lazily. It means that if you have expression of kind: A && B && C && D && ... and, for example, A == false, then B, C and others will not be calculated.

In the first case you firstly check that i and j are in a good range and then check array element. It's OK because if indexes are incorrect, you won't check s[i][j] == '.'. In the second case you do it in another order and can go out of range.

»
3 года назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

emp(-1, -1)