Why isn’t the break working?

Asked

Viewed 209 times

1

Enunciation:

Read a 5x5 matrix. Also read an X value. The program should do a search for this value in the matrix and, at the end, write the location (row and column) or "not found) message"

Code:

#include <stdbool.h>
#include <stdio.h>
#define TAM 5

int main(){
    int matriz[TAM][TAM], c, c2, num, p1, p2;
    bool aux;

    for (c=0; c<TAM; c++){
        for (c2=0; c2<TAM; c2++){
            scanf("%d", &matriz[c][c2]);
        }
    }

    printf("Que numero deseja encontrar? ");
    scanf("%d", &num);

    for (c=0; c<TAM; c++){
        aux = false;

        for (c2=0; c2<TAM; c2++){
            if (matriz[c][c2] == num){
                p1 = c;
                p2 = c2;
                aux = true;
                break;
            }
        }
    }

    if (aux){
        printf("[%d][%d]", p1, p2);
    } else {
        printf("Numero nao encontrado. ");
    }

    return 0;
}

And the problem is this, when I put to find a value that is at the beginning of the matrix it does not find, only at the end.

Can someone explain to me why this happens?

  • i was made Lgus tests, and it just doesn’t stop testing when it enters the if. how to make it stop?

  • The break, in the [tag:c], it is only able to break a single loop. From what I understand, you want to get out of both ties (both inward and outward), correct?

  • That’s right! How can I do that?

  • So easy without using functions? Do not recommend, think only goto. The answer to your question you can find here: https://answall.com/q/12346/64969 or here: https://answall.com/q/97300/64969

2 answers

4


The break forever only the inside loop. Soon analyzing your code:

for (c=0; c<TAM; c++){
    aux = false;

    for (c2=0; c2<TAM; c2++){ // este é o loop que para
        if (matriz[c][c2] == num){
            p1 = c;
            p2 = c2;
            aux = true;
            break; // quando este break é executado
        }
    }
}

There are several ways to solve this problem.

Multiple breaks

One of the ways to solve it is to do it again break in the for who is out if ever made break in the for from within. This becomes more complicated or boring as it has more fors to break.

In your example it would look like this:

for (c=0; c<TAM; c++){
    aux = false;

    for (c2=0; c2<TAM; c2++){
        if (matriz[c][c2] == num){
            p1 = c;
            p2 = c2;
            aux = true;
            break;
        }
    }

    if (aux){ //se apanhou true e teve um break no outro for, faz break a este tambem
        break;
    }
}

If only I had more fors would have to continue to do more if break until you leave everyone.

Function

With functions also solves the problem, and in my opinion is a much better solution, because when you want to get out of fors all just make a return. This in itself avoids any variables that function as flags which indicate whether to terminate or not, which simplifies the code. In addition it ends up abstracting a part of the logic that has in its program for a function apart, considerably simplifying the interpretation and reading.

In his example has the detail of getting the values found back, which we can also do in various ways. I will illustrate with a function that returns a boolean to indicate if found, and update two pointers to the appropriate values if found:

bool posicao_numero(int matriz[TAM][TAM], int num, int *linha_num, int *coluna_num){
    int c, c2;
    for (c=0; c<TAM; c++){
        for (c2=0; c2<TAM; c2++){
            if (matriz[c][c2] == num){
                *linha_num = c;
                *coluna_num = c2;
                return true;
            }
        }
    }
    return false;
}

Now in the main use would be:

if (posicao_numero(matriz, num, &p1, &p2)){
    printf("[%d][%d]", p1, p2);
} else {
    printf("Numero nao encontrado. ");
}

See this example in Ideone

1

In the first for you are setting the variable aux as false, so when it finds the position of the number in the second matrix for and puts the variable aux as true he comes out of the second for and go back first to set the variable to false again. Try to set the variable aux as false outside the for, thus:

aux = false;
for (c=0; c<TAM; c++){
    for (c2=0; c2<TAM; c2++){
        if (matriz[c][c2] == num){
            p1 = c;
            p2 = c2;
            aux = true;
        }
    }
}

if (aux){
    printf("[%d][%d]", p1, p2);
} else {
    printf("Numero nao encontrado. ");
}
  • Keeps giving the trouble of not stopping the noose.

  • But it solves the problem of not finding the value at the beginning of the matrix, which she mentions at the end of the question, I believe she doesn’t even need the break in this specific case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.