Click to like/follow us on:
Write an algorithm that will count and display the integer numbers from 1 to n?
Premise:
If you don't know how to count this algorithm will teach that. The structure of this algorithm will be like that of example ten that sum numbers. I am sure that you noticed that the question did require us to request for any number. Therefore, you the designer of the algorithm know the rout(s) to the solution of the problem, on your own, you should determine if you are to request or read data. What determine if you should request/read data/input are the routs/paths/steps to the solution of the problem.
Solution:
Major Step
1. Request Data
2. Start counting numbers
3. Display the numbers
Stepwise Refinement
1. Request Data
    1.1 Request n
2. Start couning numbers
    2.1 count = 1
    2.2 count = count + 1
    2.3 Repeat 2.3 and 2.4 until count <= n
3. Display count

Explanation:
Because the question says that I should start counting from one (1), that is why I initialised count to be one. If I had initialised count to be zero (0), then the algorithm will start counting from zero and what is the implication? Yes of course if:
count = 0 and n = 10
Then the nature of the count will be (i.e. what the algorithm will display):
0 1 2 3 4 5 6 7 8 9.
Why? count the numbers yourself, of course there are ten of them. So the tenth number is nine (9).
However if count = 1 and n = 10,
Then the algorithm will display the following:
1 2 3 4 5 6 7 8 9 10.
Note that in each case when count increases the previous value is erased and the current value replaces it immediately.
For example: count = 1 at line 2.1 however in line 2.2 it is no longer 1 but 2, because 1 + 1 = 2. So at that line; line 2.2; the algorithm erased the previous value of count in this form:
count = 1(previous value of count) + 1(new value of count)
count = 2 (current value of count) this keep happening until the value of n is reached.
As an exercise: Write an algorithm that will count and display integer number from -10 to 10?
If you encounter any problem make use of the comment box below.

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