Why is not the break working? [duplicate]

0

Statement:

  

Read a 5x5 matrix. Also read an X value. The program should do   a search of 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 array it does not find, just the end.

Can anyone explain why this happens?

    
asked by anonymous 19.07.2018 / 07:26

2 answers

3

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 is to return break to for that is outside if you have already done break in for from within. This becomes more complicated or annoying depending on whether you have 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 you had more fors you would have to continue to do if break until you left all.

Functions

With functions it also solves the problem, and in my opinion is a much better solution, because when you want to leave the fors everyone just make a return . This by itself avoids any variables that work like flags that indicate whether to terminate or not, which simplifies the code. In addition, it ends up abstracting a part of the logic you have in your program for a separate function, greatly simplifying interpretation and reading.

In your example, you have the detail of getting the found values back, which you can also do in several ways. I'll be modeling with a function that returns a boolean indicating if it found, and updates two pointers to the appropriate values if you 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 main the usage would be:

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

See this example in Ideone

    
19.07.2018 / 12:17
1

In the first for you are setting the variable aux to false, so when it finds the position of the number in the array in the second for and puts the variable aux as true it exits the second for and returns to the first one that places the variable as false again. Try setting the aux variable to false outside for , like this:

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. ");
}
    
19.07.2018 / 08:05