How to find the largest element of a vector recursively? [closed]

-1

I can only do the iterative but recursive way I have no idea how to start.

    
asked by anonymous 07.05.2018 / 00:40

1 answer

0

take a look at these links: recursion in java , question in forum-recursividad java .

I'm not going to do the whole code for you as this would take part of your learning, but I'll explain how it could work.

A recursive function is a function that calls itself. So what you could do is have a function / method that checks whether a given vector element is larger than the largest-found yet.

For this, you can have a global variable that stores the largest element of the vector, and go passing the current index that is "parsing" as a parameter in the recursive function.

A pseudo-code would look like this:

void achaMaior_recursivo(Vetor vet, Indice i){
    // se o índice atual tiver passado do último elemento do vetor, para a recursividade
    if (i >= vet.quantidadeDeElementos){
        return;
    } else {
        //se o elemento atual é o maior até agora, atualiza maiorEcontrado
        if (vet[i] > maiorEcontrado)
            maiorEncontrado = vet[i];

        //procura pelo maior elemento nos índices restantes
        achaMaior_recursivo(vet, i+1);
    }

}
    
07.05.2018 / 00:58