Calculate the factorial by reference passage

Asked

Viewed 625 times

4

I’m studying pointers I’m trying to calculate a factorial of a number, only the result comes out 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);

}

2 answers

8

I will 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. You can make it work that way, but it won’t be right.

Working and right are two different things. Funciona, mas não está certo

That’s how it works:

#include<stdio.h>

int calulcafatorial(int num) {
    int fat = 1;
    while (num > 1) fat *= num--;
    return fat;
}

int main(void) {
    int num;
    scanf("%d", &num);
    printf("%d", calulcafatorial(num));
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

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

Yeah, 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 pointer to learn right. Pointer is hard, only use when you really have no better solution.

6


Calculations Factorial 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 "incalculable" outputs due to machine memory limitations.

Taking your reasoning into consideration, we can implement a function for factorial calculation capable of returning an error if the entry is invalid or incalculable, for example: n < 0 or n > 20.

The largest number capable of being stored in a type variable unsigned long long is 18.446.744.073.709.551.615.

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

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 for:

21! = 51.090.942.171.709.440.000 > 18.446.744.073.709.551.615

Taking your reasoning into consideration, follow 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;
}

Exits:

$ ./fatorial 5
5! = 120

$ ./fatorial 10
10! = 3628800

$ ./fatorial 15
15! = 1307674368000

$ ./fatorial 20
20! = 2432902008176640000

$ ./fatorial 21
Erro!

$ ./fatorial -1
Erro!

Browser other questions tagged

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