link factoring

Asked

Viewed 344 times

1

I am stubbing the book 6th edition of C as programming of the Deitel brothers and I am doubtful in an exercise, the exercise is the 4.14 of chapter 4 which is about factorization, it asks a program that the user type a number and that number is factored, for example if the user type the number 5, the program will do 5*4*3*2*1 resulting in 120. If anyone can give me some tip and not reply I’ll be very grateful thank you:

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

int main()
{
    int i, num, aux1=0, fat=0, aux=0;

    printf("digite um numero para ser fatorado:\n");
    scanf("%i", &num);
    printf("\nvamos fatorar entao o numero %i:\n\n", num);

    for(i=num;i>=1;i--)
    {
        aux=i;
        fat=aux*i;
        printf("%i! ", fat);
    }
    return 0;
}
  • 4

    Run your algorithm on paper and see what you’re doing wrong.

  • 2

    1a. tip: the way it’s being used, the variable aux is not doing any good. But you don’t even need it. The algorithm may be simpler than this. In the end, there will have to be an answer.

  • 5

    A quick comment. In mathematics the term "factoring" is used to decompose an integer into prime numbers. For example, 12 = 322. What you are asking for is the factorial.

  • Here’s what @Beet says is the best way to help you with small algorithms.

2 answers

1


Notice what you’re doing:

Suppose in a = 5;

   for(i=num;i>=1;i--)
    {
        aux=i;
        fat=aux*i;
        printf("\n%d ", fat);
    }

Take the test table:

i=5         |    i=4            |  ...
aux=5       |   aux=4           |
fat=5*5???  |  fat=4*4          |

What you’re looking for is this:

i=5         |    i=4            |  ...
aux=5       |   aux=fat   (5)   |
fat=5       |  fat=aux*i (5*4)  |

Note that there are several unnecessary variables and do not forget: 0! = 1.

Tip: You don’t need the aux variable, much less the Aux1 that you didn’t even use.

1

My tips:

  1. What is the factor of 0? This would be a better value to boot fat, since then you print the correct answer if the loop does not run at all.

  2. Note as the final result of fat depends only on the last value of aux , which depends only on the last value of i. I don’t think that’s why you created the fat...

  3. I prefer to iterate from 1 to N instead of N to 1. This makes it easier to write an invariant for the value of fat. If you don’t want to think about the invariant then whatever, obviously)


Hint of C:

You can declare variables after any {. It doesn’t have to be all at the top of the job:

{int i; for(i=n; i >= 1; i--){
   int aux = /* ... */
}}

Also, if you’re not using Visual Studio, you can compile your program using the C99 standard that lets you declare variables in the middle of the function

int num;
scanf("%d", &num);

for(int i=n; i >= 1; i--);
  • my doubt is the following now, I do not know how I will catch the 5 4 3 2 1 and multiply each of them my idea is that I use a helper to store them for example in the first loop I keep the 5 in the second I keep 54 and so it goes up to reach 1201 the problem is that it does not store it does 55 5 times and 44 five times and so on and so on

  • @Leonardov.Degasperin: What you want to do is right but it’s hard to give a hint without giving the whole answer at once. So a question: What is the purpose of each of the variables you created? What is the value that each of them will have to the program?

Browser other questions tagged

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