Create a recursive function in C that returns the largest and smallest value of a vector

Asked

Viewed 2,314 times

0

Guys, how can I solve this exercise?

1-on the first line the user has to type the vector size 2- in the second line, the user fills in integers 3- make a recursive function that receives by reference the variables of higher and lower values. The vector should be dynamically allocated.

I’m picking up a lot on this dynamic allocation. I did a sketch down here and I’m more lost than blind in a gunfight -.-

void procurar(int vetor[], int *tamanho, int *maior, int *menor){    

int i, j, aux; 

  for(i = 1; i < *tamanho; i++){ 
    j = i; 

    while((j != 0) && (*vetor[j] > *vetor[j - 1])) { 
      aux = *vetor[j]; 
      *vetor[j] = *vetor[j - 1]; 
      *vetor[j-1] = aux; 
      j--;     
    } 
  }

  *maior = *vetor[*tamanho];
  *menor = *vetor[0];
}



int main(){

    int tamanho, i, maior, menor;

    maior = 0;

    scanf("%d", &tamanho);

    int vetor[tamanho];

    for(i=0; i<tamanho; i++){
        scanf("%d", &vetor[i]);
    }

    procurar(&vetor, &tamanho, &maior, &menor);


    printf("%d %d", maior, menor);``

    return 0;
}
  • 2

    There will always be someone who disagrees, and who will give you the solution, but it bothers me if making a recursive algorithm when clearly an iterative works better. At bottom is teaching wrong. (in the sense of using the least suitable tool for the specific problem)

1 answer

1

I don’t know from which version of the C pattern one can allocate an array on the stack, as you did, using a variable as size index; or it’s C99 or C11. But if you want more C89-compatible code (which, for example, works with MSFT compilers), you’re better off using malloc():

#include <stdio.h>
#include <stdlib.h>

/* implementação de "procurar()" omitida */

int main() {
    int tamanho, i, maior, menor;
    int * vetor;

    if (scanf("%d", &tamanho) < 1 || tamanho < 1) {
        fputs("Não consegui ler o tamanho do vetor\n", stderr);
        return EXIT_FAILURE;
    }
    vetor = malloc(tamanho * sizeof (int));
    if (vetor == NULL) {
        fputs("Não consegui alocar o vetor\n", stderr);
        return EXIT_FAILURE;
    }

    for (i = 0; i < tamanho; i ++) {
        if (scanf("%d", &vetor[i]) < 1) {
            fprintf(stderr, "Não consegui ler o %dº valor do vetor\n", i + 1);
            return EXIT_FAILURE;
        }
    }

    procurar(vetor, tamanho, &maior, &menor);
    fprintf("O maior número é %d e o menor, %d\n", maior, menor);

    return EXIT_SUCCESS;
}

As to the procurar() proper, it is good to remember that recursion is an implementation of the mathematical technique of induction, so you have to define a base case and a inductive step. As you are dealing with vectors, a good variable to induce is the size of the vector; then, in this case, the base case is when you have a vector of length 1 and the inductive step is the result of the function on a subvector of the current vector (for example, the vector starting from the second element). As a kid:

void procurar(int vetor[], int tamanho, int * maior, int * menor) {
    if (tamanho == 1) { /* caso base */
        * maior = * menor = vetor[0];
    } else { /* passo indutivo */
        procurar(vetor + 1, tamanho - 1, maior, menor);
        if (vetor[0] > * maior) * maior = vetor[0];
        if (vetor[0] < * menor) * menor = vetor[0];
    }
}
  • Thank you very much @Wtrmute. I compiled your code into Devc++ and had to make a few minor changes according to what my college exercise asked for. But for the rest, the search function you did is right, and it was very useful for me to learn. Brigadão :D

Browser other questions tagged

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