Summary notes – SDD – National 5

advertisement
National 5 Computing Science
Software Design and Development
Summary Notes
Design notations
A design notation is the method we use to write down our program design.




Pseudocode is written using English words and is easily changed, line by line, into
the chosen programming language (see all of the examples in this booklet).
A structure diagram gives a visual representation of how the program is broken
down into smaller problems.
A flowchart gives a visual representation of the sequence of processes in the
program.
Programming - Data structures and types
Two data structures are available for storing information:
 a variable is used to store a single item of data
 an array (aka 1D array) is used to store a list of data items.
Some data types which can be used are:
 string (for text)
 character (for a single character)
 integer (for whole numbers)
 real (for non-whole numbers)
 Boolean (for True/False results).
VB examples
Dim name As String
Dim age As Integer
Dim names (1 To 30) As String
Dim prices (1 To 100) As Single
**********************************************************************
Programming - Assigning values to variables
This means “putting data into a variable”.
Pseudocode examples
SET age TO 21
SET name TO “Sherlock”
VB examples
age = 21
name = “Sherlock”
**********************************************************************
Programming - Arithmetic operations
Arithmetic operations include +, -, *, /, ^ (to the power of) and mod (the remainder of
a division).
Pseudocode examples
SET answer TO 3 + 4 ^ 2
SET remainder to 17 mod 5
VB examples
answer = 3 + 4 ^ 2
remainder = 17 mod 5
**********************************************************************
Programming - Concatenation
Concatenation is the process of joining strings, variables and arrays together.
Pseudocode examples
SEND “Hello “ & name TO DISPLAY
VB examples
MsgBox (“Hello “ & name)
**********************************************************************
Programming - Pre-defined functions
Pre-defined functions are built in to the programming environment and perform useful
calculations. Some predefined functions include: Int, Rnd, Sin, Cos, Len, Left.
VB examples
Number = Int (Rnd * 10) + 1
Initial = Left (forename, 1)
Programming - Conditional statements
Conditional statements use the IF…THEN…ELSE structure.
They are used to select particular lines of code to be carried out.
Pseudocode examples
IF age >= 17 THEN
SEND “You can learn to drive” TO DISPLAY
ELSE
SEND “You are not qualified” TO DISPLAY
END IF
**********************************
IF name ≠”Moriarty” THEN
SEND “Welcome” TO DISPLAY
END IF
VB examples
If age >= 17 Then
MsgBox (“You can learn to drive”)
Else
MsgBox (“You are not qualified”)
End If
**********************************
If name <> “Moriarty” Then
MsgBox (“Welcome”)
End If
**********************************************************************
Programming - Logical operators
Logical operators – AND, OR, NOT – can be used to create complex conditions.
Pseudocode examples
IF score>100 AND score<500 THEN
**********************************
WHILE age<21 OR name<>”Watson” DO
VB examples
If score>100 And score<500 Then
**********************************
Do While age<21 Or name<>”Watson”
**********************************************************************
Programming - Fixed loops
A fixed loop repeats a section of code a set number of times.
Pseudocode examples
REPEAT 10 TIMES
SEND name TO DISPLAY
END REPEAT
**********************************
FOR loop FROM 1 TO 20 DO
SEND loop TO DISPLAY
END FOR
VB examples
For counter = 1 To 10
List1.AddItem name
Next counter
**********************************
For loop = 1 T0 20
List1.AddItem loop
Next loop
**********************************************************************
Programming - Conditional loops
A conditional loop repeats a section of code either WHILE a condition is met or UNTIL a
condition is met.
Pseudocode examples
REPEAT
RECEIVE response FROM KEYBOARD
UNTIL response = “No”
**********************************
WHILE response ≠ “No” DO
RECEIVE response FROM KEYBOARD
END WHILE
VB examples
Do
response = InputBox (“Continue?”)
Loop Until response = “No”
**********************************
Do While response <> “No”
response = InputBox (“Continue?”)
Loop
Errors
There are 3 main types of programming error.
 Syntax – incorrect use of the programming language.
e.g. Typing Nxt counter instead of Next counter
 Execution – errors while the program is running, usually causing an error message.
e.g. Type mismatch error if the program tries to store text in an integer variable.
 Logic – no syntax or execution error but program doesn’t produce correct results.
e.g. The program should only allow input of numbers from 1 to 10, but it allows any
number to be input.
**********************************************************************
Testing
Software is tested methodically, following a test plan, to make sure it is free from
errors. A good test plan should include the following types of test data:
 Normal
– acceptable test data.
 Extreme
– acceptable test data but on the limits of what is acceptable.
 Exceptional – unacceptable test data.
Example
A program asks the user to input a number from 1 to 10.
Normal test data – 4, 5, 6
Extreme test data – 1, 10
Exceptional test data – 233, -15, A, %
**********************************************************************
Readability
How easily the program can be understood by another programmer.
Readability can be improved by adding:
 internal commentary – comments in the program code to explain what it is doing.
 meaningful variable names – using sensible names for variables, e.g. score rather
than s.
 indentation – using the tab key to help show where control structures start and
finish.
 Blank lines – to help separate sections of code.
**********************************************************************
Input validation
 A standard algorithm used to ensure that the user inputs acceptable data.
 Input validation always uses a conditional loop.
Below are some examples to ensure the user inputs a number between 1 and 10.
Basic algorithm 1
REPEAT
RECEIVE number FROM KEYBOARD
UNTIL number >=1 AND number <=10
Basic algorithm 2
RECEIVE number FROM KEYBOARD
WHILE number <1 OR number >10 DO
RECEIVE number FROM KEYBOARD
END WHILE
Improved algorithm 1
REPEAT
RECEIVE number FROM KEYBOARD
IF number <1 OR number >10 THEN
SEND “Please re-enter” TO DISPLAY
END IF
UNTIL number >=1 AND number <=10
Improved algorithm 2
RECEIVE number FROM KEYBOARD
WHILE number <1 OR number >10 DO
SEND “Please re-enter” TO DISPLAY
RECEIVE number FROM KEYBOARD
END WHILE
Machine code
The computer’s own language is called machine code and is made up of binary
instructions.
**********************************************************************
High Level Languages
 High level programming languages (e.g. Visual Basic) make it easier for humans to
program computers, rather than using machine code.
 They use a lot of English keywords and have built-in functions which would require a
lot of machine code programming.
**********************************************************************
Translation
A computer only understands machine code, so all high level languages must be
translated before they can run. There are two types of translator:
 Interpreter – translates and executes the program one line at a time. If an error is
found the interpreter will stop running the program so the error can be fixed. The
interpreter must be running while the program is running.
 Compiler – translates the program in one go and saves it as a fast running,
standalone, machine code version which can then be run. If errors need to be fixed,
the program will need to be recompiled. The compiler is not needed for the program
to run.
**********************************************************************
Units of storage
The following units are used when referring to file sizes or storage capacity.
 Bit
– a single binary digit, 1 or 0.
 Byte
– 8 bits, e.g. 10010001
 Kilobyte (KB)
– 1024 bytes
 Megabyte (MB)
– 1024 KB
 Gigabyte (GB)
– 1024 MB
 Terabyte (TB)
– 1024 GB
 Petabyte (PB)
– 1024 TB
**********************************************************************
Binary – integers
 Every type of data in a computer system is stored using the binary number system,
where 1 is represented by ON and 0 is represented by OFF.
 Integers (whole numbers) are represented as shown below.
Examples
128
64
0
1
1
1
1
0
32
0
1
0
16
1
1
0
8
1
1
0
4
0
1
0
2
1
1
1
1
1
1
0
=
=
=
91
255
130
**********************************************************************
Binary – real numbers
Real numbers (numbers with a fractional part) are represented using the floating point
system, which stores the mantissa and the exponent.
Example
In the number 2.037845 x 1017, the mantissa is 2.037845 and the exponent is 17.
Binary – characters
 Text characters are converted into binary using the ASCII code, where each
character is represented by an 8 bit binary number.
 Another code which can be used is Unicode, where each character is represented by
a 16 bit binary number.
**********************************************************************
Graphics – bitmapped and vector
 Bitmapped graphics store the colour of each pixel as a binary number. Paint.Net is a
bitmap graphics package.
 Vector graphics store the attributes of every object in the image (e.g. circle: centre
x, centre y, radius, fill colour, line thickness, etc). Inkscape and SMART Notebook
store graphics using the vector method.
Bitmapped
Manipulate at pixel level.
Typically a larger file size. File size is
affected by resolution of image.
Becomes pixellated (blocky) when
enlarged.
Ideal for photos and realistic images.
Vector
Manipulate at object level.
Typically a small file size, although it will
increase as more objects are added to the
image.
Can be enlarged without affecting quality
(it is resolution independent).
Ideal for simple logos on websites, etc.
**********************************************************************
Computer architecture - Processor
The CPU (Central Processing Unit) is the main processor in a computer.
It has 3 main components:
 Control Unit – manages the execution of program instructions and fetching from
memory.
 ALU (Arithmetic and Logic Unit) – performs arithmetical operations (add, subtract,
multiply, divide) and logical decisions (AND, OR, NOT).
 Registers – Temporary storage locations inside the processor.
**********************************************************************
Computer architecture - Memory
Memory is used to store programs while they are running. It is organised into millions of
individual storage locations, each with a unique binary address.
 RAM (Random Access Memory) – can be written to, stores program which are in use,
loses its contents when power is switched off.
 ROM (Read Only Memory) – can’t be written to, retains contents even when power is
switched off.
**********************************************************************
Computer architecture - Buses
Buses are sets of wires which connect the processor and memory.
 Data bus – caries binary data to and from memory.
 Address bus – carries the memory location which data is to be written to or read
from.
**********************************************************************
Computer architecture - Interfaces
An interface is a hardware device which compensates for differences in the way
peripherals (devices connected to the computer) and the CPU operate, and allows them
to communicate with one another.
Download