Professional Documents
Culture Documents
Algorithms
A program is an ordered set of functions designed to perform a specic task. It is written according to a set of dened rules so that it can be translated to the binary codes that the computer understands.
The programming language is the set of instructions that can be used to construct a program. Before writing a program, the programmer must clearly understand the problem, and how the proposed program will solve it.
Algorithms
An algorithm is the step-by-step sequence of instructions that describes how to perform the task. In other words it is the method used to solve a problem. For example: Problem: Find the sum of 2 numbers: Algorithm: Get rst number Get second number Add the two numbers Display the result.
Algorithms
There may be more than one way to perform the task. Therefore the one problem may have more than one possible algorithm.
Flowchart Symbols
Input/Output Block
Generalized Input/Output Block; reading data from an input medium or writing data to an output medium. This block should be used in situation were data is being sent in and out of the processor via some sort of I/O peripheral.
Flowchart Symbols
Process Block
Any process step; an operation or group of operations that cause a change in value, form or location of the data. This can consist of arithmetic or logical operators or even move commands.
Flowchart Symbols
Flowline
Sequence of operations and direction of data flow; arrowheads are required if linkage is not left-to-right or top- to-bottom. Generally arrowheads are included to avoid confusion.
Flowchart Symbols
Annotation
Additional explanation or comments. This block is used for providing additional information to any other block in the flowchart.
Flowchart Symbols
Decision
Decision-making or switching type of operation, usually based on a comparison, that determines which of a number of paths should be followed.
Flowchart Symbols
Preparation
An operation performed on the program itself for control, initialization, overhead or cleanup; examples are to set a switch, modify an index register, or set a limiting value for iteration control.
10
Flowchart Symbols
Predefined Process
One or more operation defined in more detail elsewhere, such as in a booklet or on a different flowchart, but not on another part of the flowchart in which this symbol appears.
11
Flowchart Symbols
Terminal
Terminal point in a flowchart stop, start or break in the line of flow.
12
Flowchart Symbols
Connectors
Entry to or exit from another part of the flowchart; if to or from step is on another page then the page reference should also be stated.
13
Anatomy of a Program
Flow of Control refers to the order in which a programs statements are executed.
Every program has common flow structures. The control structure is the lowest fundamental unit in a program. Each control structure has one entry point and one exit point.
14
Anatomy of a Program
The basic control structures are: Sequence (Single Step) Selection (Conditional)
15
Anatomy of a Program
Iteration (Loops)
while do repeat until for
Control structures may be used in combination with each other. This is called nesting.
16
Sequence
Simplest control structure. Performs one step followed by another in the order they are placed in the program. The normal flow of a program is sequential, unless the program is directed otherwise. Step 1 Step 2 Step 3
17
Examples
!"#$"% )*+,-% .$/'"%)%
&'(%
18
Selectional
Selectional also called conditional, branching, testing. if - then structure
if - then structures test a condition and act upon it. if the condition is true, a task is performed and the structure is exited. if the condition is false, no task is performed and the structure is exited. Note that the THEN part is always the true condition.
19
Examples
!"#$"% )*+,-%
)12% 7#896%
4$56%
.$/'"% 0)123%
&'(%
20
Selectional
if then - else structure
if - then - else structures test a condition and act upon it if the condition is true, task 1 is performed and the structure is exited if the condition is false, task 2 is performed and the structure is exited The THEN always refers to the true condition. The ELSE always refers to the false condition.
True
TASK2
21
Examples
!"#$"% )*+,-%
5$67%
.$/'"% 0)123%
&'(%
22
Selectional
case structure
A case structure is used instead of many if-then statements. A case structure is used when one selection is made from many alternatives. A case structure takes a value and compares that value to the choices from the first one down to the last one. Once a match is found the then statement is executed and the case statement is exited.
True IF THEN
23
Example
!"#$"% )*+,-%
5$67%
.$/'"% 0)123%
.$/'"% 0)423%
&'(%
24
Iteration
Iterative Structures also called loops Used where the same program steps must be repeated many times Always involves testing a condition to leave the loop.
25
Iteration
while - do structure
while - do tests a condition at the top of the loop and then performs the task if condition is true, perform the task and loop back to test again if condition is false, exit the structure
WHILE False
True
DO
26
Examples
!"#$"% )'*"!+),)-./ 012.3% 789:;<% >#?@5% &'(% 45"+6#$,)-./ 012.73% 2$=5%
27
Iteration
repeat - until structure
repeat - until performs a task and then tests a condition at bottom of the loop if condition is false, loop back to perform task again if condition is true, exit the structure
REPEAT
UNTIL True
False
28
Examples
!"#$"% )'*"!+),)-./ 012.3% 45"+6#$,)-./ 012.73% 7889:;% 2$?5% &'(%
<#=>5%
29
Iteration
for structure
A for structure is used when the starting and ending values of a loop are known. A for structure is a loop that requires the bounds of the loop to be specified. A counter is used as an indicator of the progress through the loop. The starting value of the loop must be specified, the ending value of the loop must be specified, and the step size (or how the counter is altered) must be specified.
START BODY
END? YES
NO
30
Iteration
for structure
A for structure initialises the counter to the starting value. The counter is compared to the ending value. If the counter is greater than the ending value then the for structure is exited. If the counter is less than or equal to the ending value, then the loop body is executed. After the loop body has been executed the counter is incremented by the step size ( ie counter = counter + step_size) and the end condition tested again.
START BODY
END? YES
NO
31
Examples
!"#$"% )*+%
32
Examples
!"#$"% )*+%
33
Iteration
Which type of Loop to use?
The while-do, repeat-until structures are used when then number of times the loop will be repeated is not known. These are typically problems that rely on some condition to stop the loop executing. while the x key has not been pressed do repeat ... until the x key is pressed
For Example
34
Variables
Variables are where the program / algorithm stores its data. The variables are located in the memory of the computer. The size of the variable depends upon the type of data stored in it. Variables are given a unique name to distinguish them from each other. Variables should be given a name to reect their use. For example, if you need a variable to store the number of cars in the car park, then you should name the variable number_of_cars. Generally, the more descriptive the name of the variable, the easier it is to understand what the program does, and what the variable is used for.
35
Variables
Variables can be used to store numbers, letters, strings, or arrays. Strings are groups of letters. Strings are normally shown between single quotation marks () For example Hello World, Coffee Time, etc
36
Modular Programming
A well designed program requires careful forethought and planning. A program should be designed so that it is easy to develop, easy to correct (debug), and easy to modify. Experience has shown that the best method of programming is to break a program up into smaller tasks called modules. This is known as modular programming.
37
Modular Programming
Module 4
38
Modular Programming
Each module must do what is required of a small program, it should receive data, process the data, and produce a result. Large modules should be broken up into smaller modules. It is easier to develop the code for a many small tasks, than to develop the code for a large program made up of many tasks. Each module should be no bigger than about a screen or two of code.
39
Modular Programming
Each module should be able to be veried by itself. When testing the program it is much easier to setup the test data and nd the error in a small block of code (say 10 lines), than it is to setup the test data and nd the error in a whole program (say 100 lines of code). Once each module has been tested individually and found to be working correctly then all the modules can be integrated together in the main module. Since each of the modules have been veried then the whole program should work when put together.
40
Subroutines are modules. Subroutines are called to perform a specic task by the main module or other modules. A subroutine can have values passed to it. These values are called the arguments. Arguments are passed to a subroutine through the processors registers or the stack. For example LDAB JSR #B PutChar
41
43
43
43
43
43
43
43
The top level consists of the problem being broken up into smaller very general blocks. The next level consists of the blocks in the top level being broken up into smaller blocks. This is called successive renement. The breaking up of the blocks in the level above is continued until the task is broken up into blocks small enough to be individual modules (subroutines).
44
Example
Party of the Century
Venue Music
45
Modular Flowchart
!"#$"% )#*+,-./0.1% 23)0%65% 67.89%
,7:%
&'(%
46
47
Important Techniques for developing data ow in the system or Flowcharts Chart the main line of
program rst, then incorporate detail in later owcharts.
Write within symbols avoid using too many words. If necessary use the annotation symbol. Choose wording to suit the anticipated readers of the owchart. Be legible, neatness counts. If owchart becomes complex use connector symbols to reduce the number of ow lines.
48
49
50