Premise:
In
this repetitive algorithm we need to know how to count so that we can
know where to start counting and where to stop counting which determines
where to stop requesting the scores. And while doing this we will be
checking which of the score is the minimum among the n scores. However
the minimum score will be determine after the we might have collected
the whole scores. Make use of variables to keep your values.
Solution:
Major Steps
1. Request n
2. Calculate/Determine Minimum
3. Display Minimum
Stepwise Refinement
1. Request n
2. Calculate/Determine Minimum
2.1 count = 1
2.2 Request Score
2.3 Minimum = Score
2.4 count = count + 1
2.5 Request Score
2.6 If score < Minimum THEN
2.6.1 Minimum = Score
Else
2.6.2 Do nothing
2.7 Repeat step 2.4, 2.5, 2.6 until count <= n
3. Display Minimum
1. Request n
2. Calculate/Determine Minimum
3. Display Minimum
Stepwise Refinement
1. Request n
2. Calculate/Determine Minimum
2.1 count = 1
2.2 Request Score
2.3 Minimum = Score
2.4 count = count + 1
2.5 Request Score
2.6 If score < Minimum THEN
2.6.1 Minimum = Score
Else
2.6.2 Do nothing
2.7 Repeat step 2.4, 2.5, 2.6 until count <= n
3. Display Minimum
Explanation:
Wow this algorithm seems different. This example is based on Repetitive control structure algorithm.Step 1 will request for the number of scores needed.
Step 2.1 initialises the variable count to one (1), in other words the algorithm want to start counting from 1, 2, …, up to a specified number n.
Step 2.2 Request for the first scores, 5 say.
Step 2.3 Assigns this score to a variable called Minimum and this makes minimum to be equal to the score enter in step 2.2.
Step 2.4 Increase the variable count by one. Since count = one before, now count will be 1 + 1 = 2.
Step 2.5 Request for the second score. Bear in mind that when the algorithm requested for the first score count was one at step 2.1 and that it request for the second score count is now 2 at step 2.4.
Step 2.6 tests the truth of the matter. How? By setting up or placing condition(s) at the necessary place/lines. This is the condition(s):
If score < minimum Then do something, what is that to do? It contained in step 2.6.1. So if this condition is true control will fall to step 2.6.1 otherwise the sub steps under step 2.6 will be jumped/skipped.
Step 2.6.1 As discussed above if step 2.6 is true, the value of the score will be assigned to the minimum variable. Else if it is not true,
Step 2.6.2 Do nothing.
Step 2.7 Means that the process should continue but now from step 2.4 – 2.6 until count is less than or equal to n (the number of scores). Why should the process continue from step 2.4 and not from first step or step 2.1? Steps 2.1 to 2.3 are not need again for the process to continue; otherwise, the algorithm will be wrong.
If the Algorithm should return to step 2.1, this will return our count to 1 instead of increasing the count to next higher integer. Therefore, this will disorganise our counting. If we request for a new score and this score is assign to the variable minimum the nth (the last score) will be equal to minimum thus leading to a wrong algorithm.
Step 3 finally displays the minimum among the n scores.
Test:
Here
i will introduce to one of the methods i use in testing my algorithm.
You can develop any method you can understands better but however, the
method should be correct. That is the method should give the correct
result because your algorithm may be correct but the method being use to
test the algorithm may be wrong thereby causing you a lot of trouble.
The figure below show the test.n | Count=count+ 1 | Score | minimum | Is score < mini | Is count <= n | ||
3 | 1 | 5 | 5 | Is 5 < 5? | no | Is 1 <= 3? | no |
2 | 3 | 3 | Is 3 < 5? | yes | Is 2 <= 3? | no | |
3 | 13 | 3 | Is 13 < 3 | no | Is 3 <= 3 | Yes |
Test result of example five
In the table above 3 was supplied to be n. at this stage count is still one. and then score was request and 5 was supplied the next step says that minimum should be equal to score that is why in row 2 column 4 minimum has the value 5. A test was made to check if score (5) is less than minimum (5), this result to false. Row 2 column 7 test if count (1) is less than or equal to n (n) so that to see if the process will stop (i.e to stop accepting or requesting for scores).
I will jump to row 4. In row, four counts are now 3. Thirteen (13) were supplied as the score but note that in row 3 and 4 that the step will now continue from step 2.4. After 13 were supplied it will not be immediately assigned to minimum but make the test first to see if score is less than minimum. In the above table score is not less than minimum therefore step 2.6.2 will execute which say do nothing. This means that minimum should retain it former value, 3. In row 4 column 7 a test was made and it shows that count is now equal to the value n. so the process stop and the algorithm display the value of the minimum which is 3 and stop.
So using a chart/table like the one above you can test some algorithm while creating columns and rows depending on the variables or the demand of the algorithm.
SHARE THIS POST WITH YOUR FRIENDS...::
Tweet
0 comments:
Leave a Reply