Problem with recursive function, 2 parameters

0

I'm trying to write this code using recursion:

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

  int potencia(int i, int j)
  {
    return (j > 0) ? 1 : (potencia(i , j)i * j - 1) * i;
  }

  int main(void)
  {
    setlocale(LC_ALL, "");

    int valor1;
    int valor2;

    printf("Insira um valor: ");    scanf("%i", &valor1);
    printf("Insira outro valor: "); scanf("%i", &valor2);

    printf("Resultado da potencia: ", potencia(valor1, valor2));

    return 0;
  }

But an error is displayed to me

F:\Atividade 47.c|7|error: expected ')' before 'i'|

I have changed everything possible and so far nothing has worked, can anyone help me solve this problem?

    
asked by anonymous 06.03.2018 / 00:04

2 answers

1
(potencia(i , j) <qual operador vai aqui?> i * j - 1)

You can not have two operands without an operator. We need to put what needs to be done with the power of e and j related to i. Placing an operator there will work.

    
06.03.2018 / 00:23
1

Here are some things that are happening with your code:

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

  int potencia(int i, int j)
  {
    return (j > 0) ? 1 : (potencia(i , j)i * j - 1) * i;
    // ----------------------------------^
    //                                    \__ O Maniero falou desse operador aqui
  }

  int main(void)
  {
    setlocale(LC_ALL, "");

    int valor1;
    int valor2;

    printf("Insira um valor: ");    scanf("%i", &valor1);
    printf("Insira outro valor: "); scanf("%i", &valor2);

    printf("Resultado da potencia: ", potencia(valor1, valor2));
    // ---------------------------^
    //                             \__ faltou o indicador de formatação de inteiro; o que vem aqui?

    return 0;
  }

Enjoying, its recursive formula looks exactly like this:

                 / j > 0: 1
potência(i, j) ={
                 \ j <= 0: potência(i, j) i * j-1

If we try to find the value of potência(5, 1) , what would be the expected result? If we follow the logic of its formula, such as j > 0 , the result would be 1 . Is that right?

If we take the formula and try to calculate for potência(5, 0) ? I'll take the first step of the iteration:

potência(5, 0) ==> potência(5, 0) 5 * 0-1

Is this recursion right? Will it converge to some value eventually?

    
06.03.2018 / 01:13