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

0

Well, how can I resolve this exercise?

1- In the first line the user has to enter the size of the vector 2- In the second line, the user fills with integers 3 - make a recursive function that receives by reference the variables of greater and smaller values. The vector must be dynamically allocated.

I'm picking up a lot on this dynamic allocation. I made a sketch down here and so much more lost than blind in shooting -.-

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;
}
    
asked by anonymous 06.06.2017 / 04:39

1 answer

1

I do not know from which version of the C pattern you can allocate a vector on the stack, as you did, using a variable as the size index; or is C99 or C11. But if you want a code that is more compatible with C89 (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 for% w / o% itself, it is good to remember that recursion is an implementation of the mathematical induction technique, so you have to define a base case and an inductive step. in> Since 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 in a subvector of the current vector (eg, the vector starting from the second element). In kids:

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];
    }
}
    
06.06.2017 / 22:16