Click to like/follow us on:
Write an algorithm that will request for an integer number, n and a score x the algorithm will calculate and display the function x raise to the power n.?
Premise:
Don't panic, for the algorithm is simpler than the one above. It is just a modified version of example nine. So you will apply what you have learned.
Now you will see the important of count variable. If you have not studied the algorithm that teaches how to count, make sure you do.

If you don't know how the formula will look like, then study the diagram below and that is just the formula.
Before you continue, minimise this window (browser), think, perhaps pick up a rough paper and do a rough work. The rough work may be the correct solution to the problem. Remember the algorithm (s) provided here must not be the same with your own. What is needed is the best algorithm that leads to the solution of the problem.
Solution:
Major Steps
1. Request Data
2. Calculate Power
3. Display Power
Stepwise Refinement
1. Request Data
    1.1 Request n
    1.2 Request X
2. Calculate Power
    2.1 Power = 1
    2.2 count = 0
    2.3 if n = 0 THEN
        2.3.1 Do nothing
    Else
    2.4 count = count + 1
    2.5 Power = Power * X
    2.6 Repeat 2.4 and 2.5 until count = n
3. Display Power

Explanation:
Only what should be new here are: line 2.3, 2.5 and 2.5. Though if you have studied example ten, then you should be able to reason out what had happened in line 2.5.
This algorithm seems difficult at first but now I guess it becomes so easy for you because you have glanced over it.

Instance 1: If the value request in line 1.1 is two (i.e. n = 2) and the value requested in line 1.2 is 2 (i.e. x = 2) then the formula should be give as shown in below:

Instance 2: However if at time a user supplied the following values, n = 3 and x = 2. the formula will be:

How can you tell computer to be doing this up to n times (places) for any value of x? If you are able to do that, then how will computer know when to stop multiplying for; X raise to the power n?

At instance 1: 2 x 2 = 4 and at instance 2: 2 x 2 x2 = 8. However if care is not taken, computer will continue this process until a snail catch up with light in a running competition. The count variable helps us to multiply x up to n times (places) which should be the required value.


SHARE THIS POST WITH YOUR FRIENDS...::

0 comments:

Leave a Reply

All Posts

Access all posts here!

Categories

Unordered List

Sample Text

Lists of Jobs in the United States

List of Courses & Outlines For UNN Students

Popular Posts

Like us on Facebook

Recent Posts

    Text Widget