Date post: | 14-Apr-2015 |
Category: |
Documents |
Upload: | brett-wildermoth |
View: | 23 times |
Download: | 2 times |
Programming, Algorithms and Flowcharts
Topic Video 022
1Tuesday, 14 July 2009
Algorithms A program is an ordered set of functions designed to perform a specific task. It is written according to a set of defined 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.
2Tuesday, 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 first number
Get second number
Add the two numbers
Display the result.
3Tuesday, 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.
4Tuesday, 14 July 2009
Flowchart Symbols
Input/Output BlockGeneralized 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.
5Tuesday, 14 July 2009
Flowchart Symbols
Process BlockAny 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.
6Tuesday, 14 July 2009
Flowchart Symbols
FlowlineSequence 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.
7Tuesday, 14 July 2009
Flowchart Symbols
AnnotationAdditional explanation or comments. This block is used for providing additional information to any other block in the flowchart.
8Tuesday, 14 July 2009
Flowchart Symbols
DecisionDecision-making or switching type of operation, usually based on a comparison, that determines which of a number of paths should be followed.
9Tuesday, 14 July 2009
Flowchart Symbols
PreparationAn 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.
10Tuesday, 14 July 2009
Flowchart Symbols
Predefined ProcessOne 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.
11Tuesday, 14 July 2009
Flowchart Symbols
TerminalTerminal point in a flowchart – stop, start or break in the line of flow.
12Tuesday, 14 July 2009
Flowchart Symbols
ConnectorsEntry 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.
13Tuesday, 14 July 2009
Anatomy of a Program
Flow of Control refers to the order in which a program’s 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.
14Tuesday, 14 July 2009
Anatomy of a Program
The basic control structures are: Sequence (Single Step) Selection (Conditional)
if then if then else
15Tuesday, 14 July 2009
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.
16Tuesday, 14 July 2009
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
17Tuesday, 14 July 2009
Examples!"#$"%
&'(%
)*+,-%
.$/'"%)%
18Tuesday, 14 July 2009
Selectional• Selectional also called conditional, branching, testing.
– if - then structure
IF THENTrue
False
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.
19Tuesday, 14 July 2009
Examples!"#$"%
&'(%
)*+,-%
.$/'"%
0)123%)12%
4$56%
7#896%
20Tuesday, 14 July 2009
Selectional• if – then - else structure
IF THENTrue
FalseELSE
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.
TASK1
TASK2
21Tuesday, 14 July 2009
Examples!"#$"%
&'(%
)*+,-%
.$/'"%
0)123%)12%
.$/'"%
0)4*23%
5$67%
8#9:7%
22Tuesday, 14 July 2009
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.
IF THENTrue
FalseELSE
IF THENTrue
FalseELSE
23Tuesday, 14 July 2009
Example!"#$"%
&'(%
)*+,-%
.$/'"%
0)123%)12%
.$/'"%
0)423%
5$67%
8#9:7%
)42%
5$67%
8#9:7%
.$/'"%
0)*23%
24Tuesday, 14 July 2009
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.
25Tuesday, 14 July 2009
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 DOTrue
False
26Tuesday, 14 July 2009
Examples
!"#$"%
&'(%
)'*"!+),)-./
012.3%
45"+6#$,)-./
012.73%789:;<%
2$=5%>#?@5%
27Tuesday, 14 July 2009
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 structureUNTIL
REPEAT
True
False
28Tuesday, 14 July 2009
Examples!"#$"%
&'(%
)'*"!+),)-./
012.3%
45"+6#$,)-./
012.73%
7889:;%
<#=>5%
2$?5%
29Tuesday, 14 July 2009
Iterationfor 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.
END?
BODY
YES
NO
START
30Tuesday, 14 July 2009
Iterationfor 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.
END?
BODY
YES
NO
START
31Tuesday, 14 July 2009
Examples!"#$"%
&'(%
)*+%
,$-'"%)%
).*/%
0$12%
3#452%
)*)67%
32Tuesday, 14 July 2009
Examples!"#$"%
&'(%
)*+%
,$-'"%)%
).*/%
0$12%
3#452%
)*)67%
33Tuesday, 14 July 2009
IterationWhich 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.
–For Example
• while the x key has not been pressed do
• repeat ... until the x key is pressed
34Tuesday, 14 July 2009
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 reflect 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.
35Tuesday, 14 July 2009
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
36Tuesday, 14 July 2009
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.
37Tuesday, 14 July 2009
Modular Programming
Each module can be considered as a separate subprogram.
Main Module
Module 2Module 3
Module 4
An example of modular programming.
38Tuesday, 14 July 2009
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.
39Tuesday, 14 July 2009
Modular Programming
Each module should be able to be verified by itself. When testing the program it is much easier to setup the test data and find the error in a small block of code (say 10 lines), than it is to setup the test data and find 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 verified then the whole program should work when put together.
40Tuesday, 14 July 2009
Modular Programming: Subroutines
Subroutines are modules. Subroutines are called to perform a specific 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 #’B’JSR PutChar
41Tuesday, 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.
42Tuesday, 14 July 2009
Modular Programming: Subroutines
Top-Level (Level 0)
Sub 1 (Part of Level 1)
Do something Jump to Subroutine (JSR) Sub1Do something
Subroutine: Sub1Do somethingDo something
43Tuesday, 14 July 2009
Modular Programming: Subroutines
Top-Level (Level 0)
Sub 1 (Part of Level 1)
Do something Jump to Subroutine (JSR) Sub1Do something
Subroutine: Sub1Do somethingDo something
43Tuesday, 14 July 2009
Modular Programming: Subroutines
Top-Level (Level 0)
Sub 1 (Part of Level 1)
Do something Jump to Subroutine (JSR) Sub1Do something
Subroutine: Sub1Do somethingDo something
43Tuesday, 14 July 2009
Modular Programming: Subroutines
Top-Level (Level 0)
Sub 1 (Part of Level 1)
Do something Jump to Subroutine (JSR) Sub1Do something
Subroutine: Sub1Do somethingDo something
43Tuesday, 14 July 2009
Modular Programming: Subroutines
Top-Level (Level 0)
Sub 1 (Part of Level 1)
Do something Jump to Subroutine (JSR) Sub1Do something
Subroutine: Sub1Do somethingDo something
43Tuesday, 14 July 2009
Modular Programming: Subroutines
Top-Level (Level 0)
Sub 1 (Part of Level 1)
Do something Jump to Subroutine (JSR) Sub1Do something
Subroutine: Sub1Do somethingDo something
43Tuesday, 14 July 2009
Modular Programming: Subroutines
Top-Level (Level 0)
Sub 1 (Part of Level 1)
Do something Jump to Subroutine (JSR) Sub1Do something
Subroutine: Sub1Do somethingDo something
43Tuesday, 14 July 2009
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 refinement.
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).
44Tuesday, 14 July 2009
Example
Party of the Century
Guests Venue Entertainment
How Many? Who Why? Music Alcohol Food
45Tuesday, 14 July 2009
Modular Flowchart
!"#$"%
&'(%
)#*+,-./0,
1%23)045%%
)#*+,-./0.1%
23)0%65%
&'(%
67.89%,7:%
46Tuesday, 14 July 2009
Important Techniques for developing Flowcharts• Use a standardized flowcharting template,
with clearly recognizable symbols. Follow ANSI recommendations for symbol use.
• Do not crowd or clutter the flowchart, ensure proper spacing between symbols.
• Number the pages of your flowchart sequentially. Specifically the title of program, the date and the author on each separate page.
47Tuesday, 14 July 2009
Important Techniques for developing Flowcharts• Chart the main line of data flow in the system or program first, then incorporate detail in later flowcharts.
• Write within symbols avoid using too many words. If necessary use the annotation symbol.
• Choose wording to suit the anticipated readers of the flowchart.
• Be legible, neatness counts.
• If flowchart becomes complex use connector symbols to reduce the number of flow lines.
48Tuesday, 14 July 2009
Important Techniques for developing Flowcharts
• Collect incoming and outgoing flow lines so that the number of lines entering or leaving a symbol are minimized.
• Use the flowchart as a guide when coding; change it when necessary to ensure the flowchart reflects the steps implemented in the code.
• Cross-reference portion of the flowchart to the source language code.
49Tuesday, 14 July 2009
Important Techniques for developing Flowcharts
• Be consistent with the level of detail shown in the flowchart. Do not chart every detail, but do not leave out important details.
• Put yourself in the position of the reader; try to anticipate the reader’s problems in understanding the flowchart.
50Tuesday, 14 July 2009
Need Further Assistance?
• Ask your Demonstrator,
• Post a question on the Forum,
• Email the Convener, or
• Make an appointment.
51Tuesday, 14 July 2009