Logo

Programming-Idioms

History of Idiom 43 > diff from v1 to v2

Edit summary for version 2 by :

Version 1

2015-03-30, 10:58:37

Version 2

2015-03-30, 10:59:11

Idiom #43 Break outer loop

Look for a negative value v in 2D integer matrix m. Print it and stop searching.

Idiom #43 Break outer loop

Look for a negative value v in 2D integer matrix m. Print it and stop searching.

Imports
#include <iostream>
Imports
#include <iostream>
Code
auto indices = findNegativeValue (m, 10, 20);
std::cout << m[indices.first][indices.second] << '\n';

std::pair<int, int> findNegativeValue (int **m, int rows, int columns) {
  for (int i = 0; i < rows; ++I) {
    for (int j = 0; j < columns; ++j) {
      if (m[i][j] < 0) return make_pair (i, j);
    }
  }
  throw "No negative value!";
}
Code
auto indices = findNegativeValue (m, 10, 20);
std::cout << m[indices.first][indices.second] << '\n';

std::pair<int, int> findNegativeValue (int **m, int rows, int columns) {
  for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < columns; ++j) {
      if (m[i][j] < 0) return make_pair (i, j);
    }
  }
  throw "No negative value!";
}
Comments bubble
Whenever the code is as complicated as you need to break the outer loop, it is the correct time to add a new function.
Comments bubble
Whenever the code is as complicated as you need to break the outer loop, it is the correct time to add a new function.