Representation of Algorithms as Pseudocode and Flowcharts
Example 1.
Design an algorithm and the corresponding flowchart for adding the test scores as given below:
29, 38, 97, 85, 52, 55
Pseudocode
1. Start
2. Total = 0
3. Get the first testscore
4. Add first testscore to Total
5. Get the second testscore
6. Add to Total
7. Get the third testscore
8. Add to Total
9. Get the Forth testscore
10. Add to Total
11. Get the fifth testscore
12. Add to Total
13. Get the sixth testscore
14. Add to Total
15. Output the Total
16. Stop
Related Flow chart
The example 1, algorithms (pseudocode and flowchart) illustrate the steps for solving the problem of adding six testscores. Where one testscore is added to Total at a time.
Note that the flowchart should have a Start step at the beginning and at least one stop step at the end.
Since we want the sum of six testscores, then we should have a variable to store the resulting sum after each addition. In this example, the container is called Total and we make sure that Total should start with a zero value by step 2.
Example 2
Let us improve on Example 1.
Did you notice that in example 1, some of the steps appear more than once,. That is, step 5 gets second testscore, then step 7, gets the third testscore, and this continues until all six testscores are entered.
Let us consider the following pseudocode below:
Pseudocode
1. Start
2. Total = 0
3. Get a testscore
4. Total = Total + testscore
5. Go to step 3 to get next testscore
6. Output the Total
7. Stop
Note that the flowchart should have a Start step at the beginning and at least one stop step at the end.
Since we want the sum of six testscores, then we should have a variable to store the resulting sum after each addition. In this example, the container is called Total and we make sure that Total should start with a zero value by step 2.
Example 2
Let us improve on Example 1.
Did you notice that in example 1, some of the steps appear more than once,. That is, step 5 gets second testscore, then step 7, gets the third testscore, and this continues until all six testscores are entered.
Let us consider the following pseudocode below:
Pseudocode
1. Start
2. Total = 0
3. Get a testscore
4. Total = Total + testscore
5. Go to step 3 to get next testscore
6. Output the Total
7. Stop
In Example 2, the algorithm and flowchart are much shorter than those in example 1. For the algorithm, steps 3 to 5 are repeated to enter a testscore which is added to the Total. This set of repeated steps is shown by flow lines drawn up to the step 3 indicating that that section of the flowchart is being repeated.
However, we still need to refine this problem!
If you look again at the pseudocode and flowchart, you may notice that the steps 3 to 5 are repeated endlessly, resulting in an endless loop with no chance to Output the Total and end the program.
The algorithm needs to be improved to fix this problem.
In this way, our pseudocode and flowchart will be a finite algorithm which ends in a finite number of steps. There are different ways to improve on it, which includes using a loop to return to a set of instructions. Here are two ways below using a For and a While loop.
For loop
Since we know that there are six testscores, we can can use a FOR loop. Here is the flowchart that uses a FOR loop:
Pseudocode
1. Start
2. Total = 0
3. Count = 0 (to count the six testscores)
4. If the Count = 6, go to step 7
5. Get a testscore
6. Add the testscore to Total (Total = Total + testscore)
7. Add 1 to the Count
8. Go to step 4 to get next testscore
7. Output the Total
8. Stop
Flowchart
However, we still need to refine this problem!
If you look again at the pseudocode and flowchart, you may notice that the steps 3 to 5 are repeated endlessly, resulting in an endless loop with no chance to Output the Total and end the program.
The algorithm needs to be improved to fix this problem.
In this way, our pseudocode and flowchart will be a finite algorithm which ends in a finite number of steps. There are different ways to improve on it, which includes using a loop to return to a set of instructions. Here are two ways below using a For and a While loop.
For loop
Since we know that there are six testscores, we can can use a FOR loop. Here is the flowchart that uses a FOR loop:
Pseudocode
1. Start
2. Total = 0
3. Count = 0 (to count the six testscores)
4. If the Count = 6, go to step 7
5. Get a testscore
6. Add the testscore to Total (Total = Total + testscore)
7. Add 1 to the Count
8. Go to step 4 to get next testscore
7. Output the Total
8. Stop
Flowchart
While Loop
Here is another way, we need to add a last number to the list of numbers given. This number should be special, so unique so that each time we get a testscore, we test it to see if we have reached that special, unique number. Common numbers are -1, or 99. In this example, since we are using testscores, it may not be wise to use 99 as the unique number to stop the program.
Pseudocode
1. Start
2. Total = 0
3. Get a testscore
4. If the testscore is equal to -1, go to step 7
5. Add to testscore ( Total = Total + testscore)
6. Go to step 3 to get next testscore
7. Output the Total
8. Stop
Flowchart
In this flowchart we use a While loop. While we have not entered that special unique testscore of -1, the user is prompted to enter another testscore.
Here is another way, we need to add a last number to the list of numbers given. This number should be special, so unique so that each time we get a testscore, we test it to see if we have reached that special, unique number. Common numbers are -1, or 99. In this example, since we are using testscores, it may not be wise to use 99 as the unique number to stop the program.
Pseudocode
1. Start
2. Total = 0
3. Get a testscore
4. If the testscore is equal to -1, go to step 7
5. Add to testscore ( Total = Total + testscore)
6. Go to step 3 to get next testscore
7. Output the Total
8. Stop
Flowchart
In this flowchart we use a While loop. While we have not entered that special unique testscore of -1, the user is prompted to enter another testscore.