Answer the question
In order to leave comments, you need to log in
How to exit multiple nested loops?
Please tell me how can I get out of 2+ nested loops? If at the very bottom the condition is triggered, then complete the work of all cycles at once? without goto
PS. C++/Py
for (int i = 0; i < LIMIT; i++)
{
for (int j = 0; j < LIMIT; j++)
{
int value = matrix[i][j];
for (int a = 0; a < LIMIT; a++)
{
for (int b = 0; b < LIMIT; b++)
{
if (value == matrix[a][b])
count++;
if (count == 2)
{
std::cout << "Indexes of elements: " << "[" << i << ", " << j << "], ";
std::cout << "[" << a << ", " << b << "]" << std::endl;
// exit
}
}
}
count = 0;
}
}
Answer the question
In order to leave comments, you need to log in
iamevg , this is not an answer to the question about exactly "how to get out of a dozen nested loops", it's just another solution to your problem that does not require this. More divisions, fewer comparisons:
std::vector<std::vector<int>> matrix = {{1,2,3,42},{5,6,7,8},{9,10,11,42}};
const size_t limit = matrix.size() * matrix.front().size();
const size_t cols = matrix.front().size();
size_t i = 0;
size_t j = 1;
while(i < limit - 1 && matrix[i / cols][i % cols] != matrix[j / cols][j % cols])
{
if(++j == limit) j = ++i + 1;
}
if(i < limit - 1)
{
std::cout << "Indices of elements: [" << i / cols << "," << i % cols << "]";
std::cout << "[" << j / cols << "," << j % cols << "]" << std::endl;
}
else std::cout << "Not found" << std::endl;
And now the task is to arrange it following the MISRA C standard - without break, without goto, without modifying the loop counter in the loop body, with one exit point from the function =)
Put it in a function that looks for what you need and returns as a result, like this:
/* looks for some indices */
std::pair<size_t, size_t> findSomething(/* some parameters */) {
// ...
return value;
// ...
}
// ...
auto result = findSomething(/* some arguments */);
std::cout << "[" << << result.first << ", " << result.second << "]" << std::endl;
The simplest way for c++ is to wrap it in a lambda and exit with return. A similar approach can be used to initialize variables, but the readability of such code is still debated.
LIMIT = ...
matrix = ...
count = ...
[&]{
for (int i = 0; i < LIMIT; i++) {
for (int j = 0; j < LIMIT; j++){
int value = matrix[i][j];
for (int a = 0; a < LIMIT; a++) {
for (int b = 0; b < LIMIT; b++) {
if (value == matrix[a][b]){
count++;
}
if (count == 2) {
std::cout << "Indexes of elements: " << "[" << i << ", " << j << "], ";
std::cout << "[" << a << ", " << b << "]" << std::endl;
return;
}
}
}
count = 0;
}
}
}();
In this particular case, you can "expand" the matrix into a one-dimensional array and get by with one cycle. Here, I jotted it down in haste.
#define MAX_SAME 2
...
void search_matrix( int* matr, const int limit )
{
int same_count = 0;
int i = 0;
int j = i + 1;
while ( same_count < MAX_SAME && i < limit*limit - 1 ) {
if ( matr[ i ] == matr[ j ] ) {
same_count++;
}
if ( same_count < MAX_SAME ) {
j++;
if ( j == limit*limit ) {
i++;
same_count = 0;
j = i + 1;
}
}
}
if ( same_count == MAX_SAME ) {
cout << same_count << ' ' << i << ' ' << j;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question