Calculate the factorial by reference passage

4

I am studying pointers I am trying to calculate a factorial of a number, but the result is a totally different value than expected. Here is my code

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

void calulcafatorial(int num, int *fatorial);

int main(void)

{
     int num, *fatorial;
     scanf("%d", &num);
     calulcafatorial(num, &fatorial;
     return 0;
}

void calulcafatorial(int num, int *fatorial)
{
   int fat;
   for(fat = 1; *fatorial > 1; *fatorial--)
   {
      fat *= *fatorial;
   }
   printf("%d\n", fat);

}
    
asked by anonymous 03.01.2018 / 11:34

2 answers

6

Calculations Factories involve > Very Large Numbers , and this should be taken into account because in practice, the available memory is always finite .

More robust codes are able to filter inputs that produce "untouchable" output due to machine memory limitations.

Taking your reasoning into consideration, we can implement a factorial function that can return an error if the input is invalid or uncountable, for example: n < 0 or n > 20 .

The largest number able to be stored in a variable of type unsigned long long is 18.446.744.073.709.551.615 .

The factorial of 20 can be perfectly stored in a unsigned long long since:

20! = 2.432.902.008.176.640.000 < 18.446.744.073.709.551.615

The factorial of 21 extrapolates the storage capacity of a unsigned long long since:

21! = 51.090.942.171.709.440.000 > 18.446.744.073.709.551.615

Taking your reasoning into account, here is an example:

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

int fatorial( int n, unsigned long long * f )
{
    /* Verifica se o calculo eh possivel*/
    if( n < 0 || n > 20 )
    {
        return -1;     /* retorna -1 em caso de erro */
    }

    *f = 1;

    while( n > 0 )
    {
        (*f) *= n;
        n--;
    }

    return 0; /* retorna 0 em caso de sucesso */
}


int main( int argc, char ** argv )
{
    int num = atoi(argv[1]);
    int ret = 0;
    unsigned long long fat = 0;

    ret = fatorial( num, &fat );

    if( ret < 0 )
    {
        printf( "Erro!\n" );
        return 1;
    }

    printf("%d! = %llu\n", num, fat );
    return 0;
}

Outputs:

$ ./fatorial 5
5! = 120

$ ./fatorial 10
10! = 3628800

$ ./fatorial 15
15! = 1307674368000

$ ./fatorial 20
20! = 2432902008176640000

$ ./fatorial 21
Erro!

$ ./fatorial -1
Erro!
    
03.01.2018 / 15:02
8

I'll answer what matters since the problem only exists because of another problem.

Study pointers the right way. This case is not meant to use pointers and so everything gets confusing. It can even work that way, but it will not be right.

Running and right are different things.

Howitworks:

#include<stdio.h>intcalulcafatorial(intnum){intfat=1;while(num>1)fat*=num--;returnfat;}intmain(void){intnum;scanf("%d", &num);
    printf("%d", calulcafatorial(num));
}

See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

I kept the syntax error that allowed compiling, but fixed what prevented compilation. I got the impression of the function because it calculates the factorial and does not calculate and print the factorial.

Yes, I know, I took the pointer that was the central question, but in this code the use of the pointer only serves to disturb. Now look for something that needs a pointer to learn right. Pointer is difficult, only use when there really is no better solution.

    
03.01.2018 / 11:51