Factorial is not being calculated

Asked

Viewed 847 times

4

Make a program to read an array A of type vector with 15 elements. Construct an array B of the same type, each element of matrix B being the factorial of the corresponding element of matrix A . Display matrices A and B.

I tried to make people but something is wrong and I can’t see what it is, if anyone can point out the mistake I appreciate and already thanks for the help...

int matriza[5], matrizb[5], matrizc[5], indice, cont = 0, soma = 0;

for(indice = 1; indice <= 5; indice++)
    {
        printf("\n digite um valor: ");
        scanf("%d",&matriza[indice]);
    }

cont = matriza[indice];

for(soma = 1; cont >= 1; cont = cont - 1)
    {
        soma = soma * cont;

        matrizb[indice] = soma;         
    }

for(indice = 1; indice <= 5; indice++)
    {
        printf("\n O valor do vetor B e: %d \n", matrizb[indice]);
    }
  • You need to calculate the factorial of each vector value?

3 answers

5


Solution without using recursion (which is pragmatically not the solution for everything) since you should not understand what this is yet:

#include <stdio.h>

int main(void) {
    int matriza[5], matrizb[5], indice, cont, soma;
    for (indice = 0; indice < 5; indice++) {
        printf("\n digite um valor: ");
        scanf("%d",&matriza[indice]);
        for (soma = 1, cont = matriza[indice]; cont > 1; cont--) soma *= cont;
        matrizb[indice] = soma;
    }
    for (indice = 0; indice < 5; indice++) printf("\n O valor do vetor B e: %d \n", matrizb[indice]);
}

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

Note that I am working with the indexes of the vector of 0 to 4 instead of 1 to 5, as in the original code that will end up corrupting the memory.

Unless I was gonna do something else I had variable left.

You are not making the loop that calculates the factorial at the right place. You have to do it with each number entered and not just once (unless all the initial code is wrong and you wanted to calculate the factorial of only one number. Then you wouldn’t need the vectors.

Note that I reset the sum value every time I will start a new factorial calculation, otherwise it would accumulate the previously calculated factorial.

There are other small improvements that could be made but basically this is it.

  • I saw somewhere here on the site that one should avoid saying thank you, but porxa bigown you are the guy always saving me several times, thanks even guy very much, I know that maybe I get penalized, it’s for a good reason, thanks.

  • Avoiding it doesn’t mean it’s forbidden. Is that the most certain is to vote on the answers (not only given in your question) and obviously accept the best, what you do. I do not know if you did a all. The people are not so rigid either. It is only penalized those who make "serious things".

  • I almost forgot I want to thank all the answers, because each one contributes to better my and the knowledge of others who can see this page, I hope that one day I can also be helping new users here on the site.

4

You can use a recursive function to solve this type of problem. For example (see comments):

int fatorial(int n)
{
   // caso n seja menor ou igual a 1, retorna 1, 
   // ao contrário multiplica n pelo retorno da chamada da própria função
   // passando n - 1. Portanto, para fatorial(5) = 5*4*3*2*1 = 120
   return n <= 1 ? 1 : n * fatorial(n - 1);
}

In your example, always start reading your vector with the Indice 0. Your example, it could be something like this:

int matriza[5], matrizb[5], matrizc[5];

for(indice = 0; indice <= 5; indice++)
{
   // le o valor
   printf("\n digite um valor: ");
   scanf("%d",&matriza[indice]);

   // chama a função fatorial e passa o valor da matriza lido anteriormente.
   matrizb[indice] = fatorial(matriza[indice]);
}

for(indice = 0; indice <= 5; indice++)
{
   printf("\n O valor do vetor B e: %d \n", matrizb[indice]);
}

1

When leaving the first one is the value of the variable "Indice" is 6, and an error will occur at this point, since the matrix does not have this position. It would be interesting to have the statement of your problem to have an idea of what to expect from the solution.

Browser other questions tagged

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