Before we write any code to solve a particular problem, we need to understand the problem by dividing it up into individual tasks.
From this design we can start to analyse each stage and set out our solution by writing an algorithm. An algorithm is a sequence of events that when followed will solve the individual tasks and ultimately provide a solution to our problem.
To assist us with the production of our algorithm we introduced the concept of Structure English at the Design and Algorithms tutorial.
Structure English uses a few key words which help us to describe each stage of our algorithm.
For all programs you will ever write there will be a few clearly identifiable stages:
Input of Data
Process the Data
Output of results
Processing the data will include some calculations which might include some selection and iteration.
To help use clarify what we are doing at each stage we write our algorithm as a mixture of “keywords” and descriptive text. The keywords will help with the code structure when we come to implement our algorithm in our coding language.
Remember a good algorithm can be handed to a coder who should be able to implement your work in their choice of coding language.
For this module we shall only be looking at a few “keywords” so consider using the following set in your production of your algorithms (not all of these were covered at the tutorial):
Action. Structure English Keyword(s)
Input of data. INPUT, SET
Output of results DISPLAY, OUTPUT
Iteration REPEAT, REPEAT UNTIL
Decision making IF THEN … ENDIF, IF THEN… ELSE… ENDIF
Calculations EVALUATE, CALCULATE, COMPUTE
End the algorithm STOP
NOTE: Use capital letters to show Structure English Keywords.
You will be adding to this list as your experience grows.
Our algorithm will naturally form “blocks” where each block is completing a particular task. To highlight this feature, we use indentation to show lines of the algorithm that are in that block. See below.
Examples of usage
Example 1:
INPUT length and width of the rectangle
CALCULATE area = length time width
DISPLAY area
STOP
STOP was not covered at the tutorial but its inclusion show the end of your algorithm.
Example 2:
SET pass mark to 40
REPEAT for all students in class
INPUT student name and result
IF result is less than pass mark THEN
DISPLAY this is a failure
ELSE
DISPLAY this is a pass
ENDIF
STOP
So, try to use Structured English to produce an algorithm before you start coding your solutions to the given activities and TMA questions.