You are on page 1of 57

Topic Video 02

Programming, Algorithms and Flowcharts

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

Algorithms

There may be more than one way to perform the task. Therefore the one problem may have more than one possible algorithm.

The algorithm can be described in any language or symbolic code.

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

Flowchart Symbols
Annotation
Additional explanation or comments. This block is used for providing additional information to any other block in the flowchart.

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

11

Flowchart Symbols
Terminal
Terminal point in a flowchart stop, start or break in the line of flow.

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

14

Anatomy of a Program
The basic control structures are: Sequence (Single Step) Selection (Conditional)

if then if then else

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

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

Tuesday, 14 July 2009

17

Examples
!"#$"% )*+,-% .$/'"%)%

&'(%

Tuesday, 14 July 2009

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.

True IF False THEN

Tuesday, 14 July 2009

19

Examples
!"#$"% )*+,-%

)12% 7#896%

4$56%

.$/'"% 0)123%

&'(%

Tuesday, 14 July 2009

20

Selectional
if then - else structure

TASK1 IF False ELSE THEN

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

Tuesday, 14 July 2009

21

Examples
!"#$"% )*+,-%

)12% 8#9:7% .$/'"% 0)4*23%

5$67%

.$/'"% 0)123%

&'(%

Tuesday, 14 July 2009

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

False ELSE True IF THEN False ELSE

Tuesday, 14 July 2009

23

Example
!"#$"% )*+,-%

)12% 8#9:7% )42%

5$67%

.$/'"% 0)123%

5$67% 8#9:7% .$/'"% 0)*23%

.$/'"% 0)423%

&'(%

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

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

Tuesday, 14 July 2009

26

Examples
!"#$"% )'*"!+),)-./ 012.3% 789:;<% >#?@5% &'(% 45"+6#$,)-./ 012.73% 2$=5%

Tuesday, 14 July 2009

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

Tuesday, 14 July 2009

28

Examples
!"#$"% )'*"!+),)-./ 012.3% 45"+6#$,)-./ 012.73% 7889:;% 2$?5% &'(%

<#=>5%

Tuesday, 14 July 2009

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

Tuesday, 14 July 2009

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

Tuesday, 14 July 2009

31

Examples
!"#$"% )*+%

,$-'"%)% )*)67% 0$12%

).*/% 3#452% &'(%

Tuesday, 14 July 2009

32

Examples
!"#$"% )*+%

,$-'"%)% )*)67% 0$12%

).*/% 3#452% &'(%

Tuesday, 14 July 2009

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

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

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

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

37

Modular Programming

Each module can be considered as a separate subprogram.

Main Module Module 2 Module 3


An example of modular programming.

Module 4

Tuesday, 14 July 2009

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.

Tuesday, 14 July 2009

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

Tuesday, 14 July 2009

Modular Programming: Subroutines


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

Tuesday, 14 July 2009

Modular Programming: Subroutines How subroutines are executed


Each program line is executed sequentially until the line that jumps to the subroutine is reached. When the processor jumps to the subroutine, execution starts at the first line of the subroutine. When the end of the subroutine is reached, the processor returns to the line immediately after the line that caused it to jump.
Tuesday, 14 July 2009 42

Modular Programming: Subroutines


Do something Jump to Subroutine (JSR) Sub1 Do something Top-Level (Level 0)

Subroutine: Sub1 Do something Do something

Sub 1 (Part of Level 1)

Tuesday, 14 July 2009

43

Modular Programming: Subroutines


Do something Jump to Subroutine (JSR) Sub1 Do something Top-Level (Level 0)

Subroutine: Sub1 Do something Do something

Sub 1 (Part of Level 1)

Tuesday, 14 July 2009

43

Modular Programming: Subroutines


Do something Jump to Subroutine (JSR) Sub1 Do something Top-Level (Level 0)

Subroutine: Sub1 Do something Do something

Sub 1 (Part of Level 1)

Tuesday, 14 July 2009

43

Modular Programming: Subroutines


Do something Jump to Subroutine (JSR) Sub1 Do something Top-Level (Level 0)

Subroutine: Sub1 Do something Do something

Sub 1 (Part of Level 1)

Tuesday, 14 July 2009

43

Modular Programming: Subroutines


Do something Jump to Subroutine (JSR) Sub1 Do something Top-Level (Level 0)

Subroutine: Sub1 Do something Do something

Sub 1 (Part of Level 1)

Tuesday, 14 July 2009

43

Modular Programming: Subroutines


Do something Jump to Subroutine (JSR) Sub1 Do something Top-Level (Level 0)

Subroutine: Sub1 Do something Do something

Sub 1 (Part of Level 1)

Tuesday, 14 July 2009

43

Modular Programming: Subroutines


Do something Jump to Subroutine (JSR) Sub1 Do something Top-Level (Level 0)

Subroutine: Sub1 Do something Do something

Sub 1 (Part of Level 1)

Tuesday, 14 July 2009

43

Top Down Design

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).

Tuesday, 14 July 2009

44

Example
Party of the Century

Guests How Many? Who Why?

Venue Music

Entertainment Alcohol Food

Tuesday, 14 July 2009

45

Modular Flowchart
!"#$"% )#*+,-./0.1% 23)0%65% 67.89%

,7:%

)#*+,-./0, 1%23)045%% &'(%

&'(%

Tuesday, 14 July 2009

46

Important Techniques for developing Flowcharts



Use a standardized owcharting template, with clearly recognizable symbols. Follow ANSI recommendations for symbol use. Do not crowd or clutter the owchart, ensure proper spacing between symbols. Number the pages of your owchart sequentially. Specically the title of program, the date and the author on each separate page.

Tuesday, 14 July 2009

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

Tuesday, 14 July 2009

Important Techniques for developing Flowcharts



Collect incoming and outgoing ow lines so that the number of lines entering or leaving a symbol are minimized. Use the owchart as a guide when coding; change it when necessary to ensure the owchart reects the steps implemented in the code. Cross-reference portion of the owchart to the source language code.

Tuesday, 14 July 2009

49

Important Techniques for developing Flowcharts



Be consistent with the level of detail shown in the owchart. Do not chart every detail, but do not leave out important details. Put yourself in the position of the reader; try to anticipate the readers problems in understanding the owchart.

Tuesday, 14 July 2009

50

Need Further Assistance?


Ask your Demonstrator, Post a question on the Forum, Email the Convener, or Make an appointment.
Tuesday, 14 July 2009 51

You might also like