Error in one of the program functions calculating a factor

Asked

Viewed 198 times

2

I can’t fix the error of the last "factorial" function, which in this case would be the factorial at the bottom of this calculation:inserir a descrição da imagem aqui

Program:

    #include <stdio.h>
#include <conio.h>


double fatorial(int n);
double fatorial2(int x, int y);

int main()
{
    int n;
    int k;
    double A;

    printf("Digite os valores de N e K: ");
    scanf("%d", &n);
    scanf("%d", &k);


    A = (float)fatorial(n) / fatorial2(n, k);

    printf("Fatorial = %.0lf", A);

    getch();
    return 0;
}


double fatorial(int n)
{

    double cima;

    if ( n <= 1 )

        return (1);
    else
    {

        cima = n * fatorial(n - 1);
        return (cima);
    }
}
double fatorial2(int x, int y)
{

    double baixo;

    if ( (x <= 1) && (y <= 1))
        return (1);
    else
    {

        baixo = (x - y) * fatorial2((x - y) - 1);
        return (baixo);
    }
}
  • 2

    And what mistake you’re making?

  • 1

    The function signature in the question asks to return int nay double. I take advantage and reinforce the question of @prmottajr , what the error? What is happening or failing to happen?

  • the function I quoted is giving error, and displaying the following message "Little argument for the factorial function2'"

  • 2

    I cannot understand why you have implemented two factorial functions when you can just use one, doing A = (float)fatorial(n) / fatorial(n - k);

  • 1

    Nothing better than trying it out. Take your code and remove all references to the fatorial2, and just leave that line as I pointed out for the calculation of A, and make sure it is not the result you are looking for. Remember that fatorial(n-k) the parameter is 1 in the same, which is the subtraction between 2 values. If n for 10 and k for 4 will amount to doing fatorial(6)

  • Damn, it worked... kkkkk

Show 1 more comment

1 answer

2


Test like this here:

#include <stdio.h>
#include <conio.h>


double fatorial(int n);
double fatorial2(int x, int y);

int main()
{
  int n;
  int k;
  double A;

  printf("Digite o valor de N: ");
  scanf("%d", &n);
  printf("\nDigite o valor de K: ");
  scanf("%d", &k);


  A = (float)fatorial(n) / fatorial2(n, k);

  printf("\nFatorial = %.0lf", A);

  getch();
  return 0;
}


double fatorial(int n)
{

   double cima;

   if ( n <= 1 )

     return (1);
   else
   {
     cima = n * fatorial(n - 1);
     return (cima);
   }
}

double fatorial2(int x, int y)
{

   double baixo;

   if ( (x <= 1) && (y <= 1))
      return (1);
   else
   {
      baixo = (x - y) * (fatorial((x-y)-1));
      return (baixo);
   }
}
  • 2

    If you explained what you did in the code, it would be a valid answer. Taking advantage, in the question is asked that the function fatorial returning whole

  • 1

    factorial function calculates the factorial of n (the top part) and factor2 calculates the bottom part (n-k)! , dps, use the two functions for formula n!/(n-k)! , ai is A which is the result, and (float) factorial (n)/ factor2(n,k) for the above-mentioned formula....

Browser other questions tagged

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