Factor function passed with reference

Asked

Viewed 182 times

5

Good afternoon gentlemen

I need to make an algorithm with a procedure that takes a variable by reference and then updates the value of the variable to its corresponding factorial.

That’s the algorithm I made:

#include<stdio.h>
int fatorial(int *n){
    if ((*n==1) || (*n==0)) return 1;               
    else return fatorial(*n-1)*n;
}
int main(){
    int n = 5;
    printf("%d\n",fatorial (&n));
}

But you’re making a mistake on the line:

else return fatorial(*n-1)*n;

Error:

[Warning] passing argument 1 of 'fatorial' makes pointer from integer without a cast
[Note] expected 'int *' but argument is of type 'int'
[Error] invalid operands to binary * (have 'int' and 'int *')

Can someone help me solve this problem?

  • the argument needs to be a pointer?

  • 1

    yes... You need to pass the memory address of the

3 answers

3


In your question you have very well described just what you have not implemented:

... receive a variable by reference and then update the variable value...

If you are receiving by reference and need to change the reference itself, the function will not return the result, but must assign the result to the pointer itself.

void fatorial(int* n) {
    int temp, resultado = 1;

    if (*n > 1) {
        temp = *n - 1;
        fatorial(&temp);
        resultado = (*n) * temp;
    }

    *n = resultado;
}

So, when doing, for example:

int n = 5;
fatorial(&n);

printf("%d", n);

It will be displayed the value 120, referring to the value of 5! calculated.

The idea of the code is:

  1. Defines the variables temp, which will store the result of fatorial(n-1) if necessary, and resultado, which will store the final result;
  2. If the value of n is greater than 1, recursively calculates the factorial;
  3. Sets the value of temp as being the value of n down by 1;
  4. Calls the function of fatorial passing the reference of temp, thus temp will have the factor value of n-1;
  5. Sets the result value as the value of n multiplied by the value of temp;
  6. Updates the reference value to the resultado;
  • Thanks, solved but, I did not understand right how this algorithm works, could explain me?

  • @Arthur Which part you don’t understand?

  • you call the factorial function by passing the &temp?

  • and *n becomes &temp at the end the temp will be worth 0 pq will stay -1 until the n is > 1 which makes no sense, because n will be worth 1, result will be 10 .............................. how it reached 120?

  • @Arthur Yes, because I defined that temp is n-1, so it will calculate the value of fatorial(n-1).

  • but the result line = (*n) * temp; will never be executed

  • because before it always called the factorial function

  • will keep calling the factorial function until *n is less than 1 when it is smaller will not enter if ...... and this line will never run

  • 1

    @Arthur will be executed yes, because, as expected, the recursive function has a stop condition. If *n is 1, recursive call will not be made. I recommend that you try to build the table test to better understand

  • i am doing and... the result gave 1 at the end of all *n gave 1 and time 0 ...

Show 6 more comments

0

#include<stdio.h>
int fact(int *a,int *p) {
    while(*a>1){
            *p*=*a;
            *a=*a-1;
            }
    }
 int main() {
        int num, factorial=1,c,*p;
        p=&factorial;
        printf("entre um numero:");
        scanf("%d",&num);
        fact(&num,p);
        printf("fatorial do numero é:%d",*p);

        return 0;
        }
  • You need to pass n by reference

  • What are a and p of function?

-1

Implementation of factorial in C:

int factorial(int n)
{
    if (n >= 1)
        return n*factorial(n-1);
    else
        return 1;
}

The error you have is because you are passing an int parameter and you need to pass an int reference to what you had previously

int *n

Passing value by reference using a loop:

void factorial(int *f){
    int i, temp = 1;
    for (i = 1; i <= *f; i++)
        temp = temp * i;
    *f = temp;
}
  • This way it didn’t work...

  • What didn’t work out?

  • You need to pass the n memory address

  • Can do without recursion and just using a loop?

  • No, you need a recursion

  • and need to pass the N by reference

  • 1

    thanks for helping me

  • nothing, today you are the need and another day I can be me ;)

  • 2

    Note that you have n parameter and another n declared as local variable.

Show 4 more comments

Browser other questions tagged

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