Doubt with recursive function in C

Asked

Viewed 82 times

0

Good night, you guys!

I am performing an activity whose goal is to use a recursive function to perform a whole division only through successive subtractions.

I made a while loop and only it (which I ran in a piece of code) worked. The problem is using it in the recursive function. I was able to compile it, but the result is not correct.

Looking at the code I still haven’t found the problem (I thought it was the re-turn i in function, but the result has not changed, or maybe in the declaration of variables - but I still can not say)... I started learning recursive functions recently, in case anyone can help, thank you much the help!

Tie that works itself:

#include <stdio.h>

int main (){

    int a, b, i = 0;
    
    printf ("Digite o dividendo (numero que será dividido):");
    scanf ("%d", &a);
    
    printf ("Digite o divisor:");
    scanf ("%d", &b);    
    
    
while (a > 0 && a >= b){
    a = a - b;
    i++;
}

    printf ("O resultado da divisao e: %d", i);

    return 0;
}

Recursive function that goes wrong:

#include <stdio.h>

int main (){
    int a,b;
    
    printf ("Digite o dividendo (numero que será dividido): ");
    scanf ("%d", &a);
    printf ("Digite o divisor: ");
    scanf ("%d", &b);  
    
    printf ("O resultado da divisao e: %d. \n", divide(a,b));

    
    return 0;
}

int divide (a,b){
    
    int i = 0;

    while (a > 0 && a >= b){
    a = a - b;
    i++;
    
    return i;
    }
    
}

2 answers

2


Program error

The error of your function is in the position of the return, in that passage:

while (a > 0 && a >= b){
    a = a - b;
    i++;

    return i;
}

The command return has two features:

  1. Closes a function
  2. Closes a function by returning a value

Based on this it is noted that when the while is activated occurs these three instructions:

  1. The value of a is subtracted
  2. i is incremented by 1
  3. Function is closed by returning the value of i

If the function is closed just after the value of i increase in 1 then the loop that the while does will not occur and this generates the unexpected result.

Solution

To solve this problem just leave the command return out of the loop, in this way:

while (a > 0 && a >= b){
    a = a - b;
    i++;
}

return i;

Thus the function will only be terminated when the loop break up.

Recursiveness

Even after solving the problem this function is not recursive.

A recursive function is a function that calls itself. Example:

int funcao(int a) {
    // Alguns codigos

    funcao(a); // funcao chamando a si mesma

    // Mais codigos

    return a;
}

The above is a draft of what would be a recursive function. One observation is that we should be careful when using recursion to not create a loop infinite.

Transforming your function into a recursive function

In order for its function to be recursive it must call itself. Example:

int divide_recursivo(int a, int b) {
    int i = 0;

    if (a > 0 && a >= b) {
        a = a - b;
        i++;
        i += divide_recursivo(a, b); // a funcao chamando a si mesma
    }

    return i;
}

In your logic using the while the value of i was incremented n times depending on this expression: a > 0 && a >= b. If the loop would repeat 10 vezes, then the recursive function needs to be called 10 vezes. The expression a > 0 && a >= b is who guarantees it. If it has not been very clear I recommend you do the table test.

See your code working here.

-1

I believe that for you to use recursiveness you would have to call the method within itself, for example:

int divide (a,b){
    
    int i = 0;

    while (a > 0 && a >= b){
    a = a - b;
    i++;
    
    return divide(a, b);
    }
    
}
  • Hi Bruno, I appreciate the answer! However if I use the Return divide(a, b), the result still remains the same as before... I also realized that most of the time the value of i ends up being equal to the value of b (I don’t know if he ended up doing the wrong calculation or if there was some conflict between the variables).

Browser other questions tagged

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