Why did I repeat it twice to get the answer right?

Asked

Viewed 43 times

0

Find, in any vector of integer values, a certain element of this, given its value in php:

<?php
 $vet=array(3, 9, 7, 5, 6);
  $chave = 9; // valor que se deseja localizar
$nao_localizado = true;
$candidato = 0; // indice do candidato no vetor

    while ( $nao_localizado && $candidato < $vet){
        if ( $vet[ $candidato ] == $chave ){
        $nao_localizado = false; // localizado o valor
        }else{
        $candidato++; // avanca para o proximo candidato/editing-help
}
    if ( $nao_localizado ){
        echo( "Nao foi localizado o valor " . $chave );
    }
    else{
        echo( "O valor " . $chave .
                " foi localizado na posicao " . $candidato );
    }
} 
    ?>
  • You could further detail your question. In order to make it clear what you want to know. If you can add an example and the codes you are using.

  • This code is working more or less, the key variable in the code is 9 as it has in the vector 9 it is to say the value and say the position only that at the time of showing this it is showing 2 messages with which it does not find and soon after it shows the right that I find and says the position.

  • And if I take out of the key variable the value 9 and put another number that does not have in the vector it appears the right answer, only if repeating and error soon after the answer

  • <?php $vet=array(3, 9, 7, 5, 6); $key = 9; // value you want to locate $nao_located = true; $candidate = 0; // candidate input in vector while ( $nao_located && $candidate < $vet){ if ( $vet[ $candidate ] == $key ){ $nao_localized = false; // located the value }Else{ $candidate++; } if ( $nao_localized ){ echo( "No value found " . $key ); } Else{ echo( "The value " . $key . " was located in position " . $candidate ); } } &#Xa a; ?>

  • Copy the code I made below and run on your server and tell me if you understand ok?

1 answer

0


The search you are doing is for each vector item. That is, it compares the item value to the vector item $vet with the value of the variable $chave you are looking for.

He’s writing because it’s probably code for academic purposes. In practice, you would only write when you FIND the value, that is, you could comment on the echo who says that he has not found.

Below your code in a way that I think will make it easier for you to understand.

$vet = array(3, 9, 7, 5, 6); // lista a procurar
$chave = 9; // valor que se deseja localizar

echo "Procurando por $chave na lista: " . implode(", ", $vet).  "."; 

$nao_localizado = true; // afirma que não achou e nega se achar
$candidato = 0; // indice do candidato no vetor

while ($nao_localizado && $candidato < $vet) { // para cada item da lista
    if ($vet[$candidato] == $chave) { // se achou
        $nao_localizado = false; // localizado o valor
    } 

    // escreve se achou ou não achou o item
echo "<br/>Ciclo $candidato do while: "; // marca o ciclo
    if ($nao_localizado) {
        echo( "Nao foi localizado o valor $chave na posicao $candidato do vetor.");
    } else {
        echo( "O valor $chave foi localizado na posicao $candidato do vetor." );
    }
    $candidato++; // avanca para o proximo candidato/editing-help
}

I hope I’ve helped. Any questions just ask.

  • Obg for your help!!!

  • Did you solve your problem? If so, I ask you to mark my answer as your chosen answer and also click the arrow up, this will help with my score on this site. Thank you!

Browser other questions tagged

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