Solution:
Major Steps
1. Request n
2. Determine factorial
3. Display factorial
Stepwise Refinement
1. Requst n
2. Determine factorial
2.1 factorial = 1
2.2 count = 1
2.3 IF n = 0 THEN
2.3.1 Do nothing
ELSE
2.4 count = count + 1
2.5 factorial = factorial * count
2.6 Repeat 2.3 until count = n
3. Display Factorial
1. Request n
2. Determine factorial
3. Display factorial
Stepwise Refinement
1. Requst n
2. Determine factorial
2.1 factorial = 1
2.2 count = 1
2.3 IF n = 0 THEN
2.3.1 Do nothing
ELSE
2.4 count = count + 1
2.5 factorial = factorial * count
2.6 Repeat 2.3 until count = n
3. Display Factorial
Explanation:Of course I initialised count to be, "count = 1", so that it will not cause problem at line 2.5 if it were initialised to be, "count = 0".
The science behind the computation is at line 2.5.
Note that 5! (Five factorial) is:
Instance 1. 5 x 4 x 3 x 2 x 1 = 120.But at line 2.5 this is how the algorithm will compute the factorial:
Instance 2. 1 x 2 x 3 x 4 x 5 = 120.Why? Yes you are right! Multiplication is associative!
Why is it so? Because the value of n can change; we calculated 5! above, you may decided at another time to calculate 20! or at some other at time to calculate n!
At instance one above we don't know what the value of n will be suppose it is not five (5), because of that we will never know where to start multiplying. I cannot just come and tell the algorithm to start multiplying from 10 or 9, what if the user supplied 11 or any other number, what will the algorithm do it? It will just calculate the factorial of 10 or 9 as you instructed it.
SHARE THIS POST WITH YOUR FRIENDS...::
Tweet
0 comments:
Leave a Reply