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?
– fernanda
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?– Jefferson Quesado
That’s right! How can I do that?
– fernanda
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– Jefferson Quesado