Factorial not being calculated

4
  

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

I tried to make people but something is wrong and I can not see what it is, if someone can point the error I thank you and it's already worth 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]);
    }
    
asked by anonymous 24.09.2015 / 16:27

3 answers

5

Solution without using recursion (which pragmatically is not the solution to 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]);
    }
    return 0;
}

See running on ideone .

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

Unless you were going to do something else you had variable left over.

You are not making the loop that calculates the factorial in 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 just one number, so you would not need the vectors.)

Note that I reset the sum value every time it 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.

    
24.09.2015 / 17:16
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 index 0 . Your example can 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]);
}
    
24.09.2015 / 16:37
1

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

    
24.09.2015 / 16:47