We design algorithms to solve problems and when we translate them into code then the computer is able to interpret the instructions we gave it. Coding solutions to problems straight away is challenging and expensive. You should always plan and think first. This post is meant to help you talk about writing programs and expressing computational thinking in an effective way.
We learn how to code for one reason: to be able to express algorithms in a programming language. In Chapter 3 we made the distinction between an algorithm versus a program.
Let's refresh your memory shall we?
Every instruction in the algorithm can be categorised into one of the following: an input instruction; a processing instruction; a feedback instruction or an output instruction.
If the diagram below is reminding you of the Main Modules of a Computer, it is no coincidence! After all don't our algorithms need to be designed to put the hardware into good use?
How can we express algorithms?
1. Simple English or Pictures
This is usually the starting point, most especially if the algorithm you want to end up with should solve a problem which is not particularly trivial. This is explained in detail in Chapter 50. The definition of the problem and the outcome should first be expressed in simple English or visually, and as a first step down further into steps (very much like a preparing a skeleton for a composition).
At this stage there is no technical detail. It is just a guideline for you to understand what the algorithm should do. Should be quick, but on the downside it is ambiguous. Algorithms that need to be performed by a computer cannot be so.
2. Flowchart
A flowchart is a type of diagram that represents a workflow or process. A flowchart can also be defined as a diagrammatic representation of an algorithm, a step-by-step approach to solving a task.
The flowchart shows the steps as shapes of various kinds, and their order by connecting them with arrows. This diagrammatic representation illustrates a solution model to a given problem. This implies that each shape can easily be mapped to a programming construct. Examples in the next section will illustrate this notion.
Might be good to note that it is not meant illustrate a complete solution. Unnecessary detail and complexity should be hidden. We call this abstraction. Flowcharts are very useful in the analysis phase and design phase.
3. Pseudocode
Pseudocode is an artificial and informal language that helps programmers develop algorithms quite quickly in a programming language.
The rules of Pseudocode are reasonably straightforward if you are comfortable with basic programming. All statements showing "dependency" (keywords) are to be emphasised using capitalisation and indentation.
These include but are not limited to: WHILE, FOR, IF and CASE. More examples are given further down to illustrate this notion.
Expressing basic programming constructs
Variable Declaration / Assignment and Arithmetic Expressions
Variables are declared to store data items. You should always declare a variable with a datatype e.g. Integer.
A data item can store a literal value e.g., 5 or the result of an expression that may or may not consist of variables e.g., TOTAL + AGE.
Data Types
Each data item you declare will be assigned a value of a particular type.
You need to know well: Integer, Real, Char, String, Boolean.
Integer | Whole numbers, positive and negative. Mostly used for situations which you need natural numbers. |
Real | Numbers with fractional part e.g. 2.5. Can be positive or negative. Mostly used when doing arithmetic. |
String | A sequence of characters of any length, e.g. "cat" has a length of three. There is the concept of an empty String: "", this has a length of 0. |
Boolean | TRUE or FALSE |
Conditional Statements
An algorithm can take a shape or another based on logical expressions.
When we use conditional statements in our algorithms then there are at least two ways in which an algorithm can behave.
Conditional statements are often used to decide which action to take.
You should be aware of how conditional statements look like in pseudocode, flow chart and in a programming language.
Looping Structures
Repetition of a section in the algorithm, also referred to as iteration.
Iterate for a pre-determined number of times (for loop).
Iterate at least once until some conditional statement is true (repeat until).
Iterate without guarantee, until some conditional statement is true (while loop).
Know when to pick the right loop.
You should know how an assignment looks like in pseudocode, flow chart and in a programming language.
Accepting Input and Displaying Output/Feedback
Input is the entry of data items that are going to be processed.
Feedback refers to intermediary results that require further processing. A simple example of this is a counter data item that is being incremented within a loop structure.
The output instruction is the provision of the final result of the process. View this as the ta-da moment; your algorithm taking a bow, like our friend Jerry.
Inputs and outputs will be clearly defined in exam questions (because this is a huge part of defining the problem, go figure). Feedback elements come quite naturally from concepts explained previously.
You should know how to accept input and display output in a programming language.
The Array Data Structure
An array in programming is a list of data items that share the same type and are grouped by the use of a single name. Each item in the list can be found by an index number which represents its position.
Common techniques involving arrays
Selection: Finding one of more elements, e.g. selecting all number greater than 5.
Repetition: For a number of times, do something.
Running Totals: Totalling the sum of all numbers in an array.
Counting: Counting all the elements that satisfy a condition, e.g. all data items that are not 0.
Naruto Better😹