Unit 2 Programming Notes
Unit 2 Programming Notes
Develop code
Difference between algorithms and programs
Algorithm – is a step-by-step procedure for solving a problem (detailed design for a solution)
Programming – is a set of instructions for a computer to follow to perform a task (implementation of
that design)
Data types
Algorithms use variables (named memory locations) to store values, variables have a variety of uses:
When algorithms are converted into programs, the computer needs to be told what type of data is stored in each
variable.
In pseudocode don’t need to specific data type of variables but you do in high-programming languages like python.
Variable initialization
1. SET total TO 0
2. RECEIVE admissionCharge FROM (INTEGER) KEYBOARD
Total=0
Type coercion
X=1
Y = 2.25
Z = x+y
Print (z)
5/3= 1.66
Exponential = **
Sequence
Orderly, if:
To create a branch based on the outcome of a condition using: if, elif, else
Relational operators
Logical operators
Iteration/ loops
Loop in a loop
Random numbers
Making programs easy to read
Code readability
Strings
String indexing
first character is 0
length
len (variable)
String traversal
Using FOR loop, you can display letter by letter on separate lines.
Other ways to manipulate strings
Concatenation
+, 2 variables together
Comparing strings
Use ==
Data structures
Collection of items arrays [same datatype] or records [for 2 or more diff datatype]
► Data should be classified and organized into similar types to easily search and analyze. This is done by storing
together in data structures like records or arrays.
► There are many different data structures that can store multiple data items: strings, arrays and records.
► Pseudocode for array --? SET, e.g.: SET firstNames TO [‘Ashura’, ‘Bryn’, ‘Eloise’, ‘Mei’]
► Most are static arrays (fixed size and holds declared number of items which needs to be stated) e.g.: ,array
friends [5]
► to find length of myArray do len(myArray)
In python
► arrays not commonly used, instead use lists with comma to separate and inside square brackets.
► Lists are easier to use as they are dynamic – can add new elements as size not declared, e.g.: cars: [ ]
cars = [ ]
cars.append (‘Audi’)
print (cars)
[‘Audi’]
cars = [ ]
cars.append(‘Audi’)
cars.append(3)
print(cars)
[‘Audi’, 3]
max( )
x = max(5,2,11,10)
print(x)
you get 11
min( )
x = min(5,2,11,10)
print(x)
you get 2
slice( )
b = "Hello, World!"
print(b[2:5])
multidimensional arrays
e.g.:
print(results[index][0] + str(results[index][1]))
Records
Each element in a record is known as a field and is referenced using a field name.
All the values in a column have the same data type – learnerNum and ‘age’ are integers; firstName, lastName and ‘form’
are strings.
Input/ output
User input
A program can be made much more ‘user friendly’ by displaying helpful messages informing users of what they are
expected to enter and confirming successful input.
Validation
Invalid data can cause the program to behave unexpectedly or stop or output would be wrong --‘garbage in, garbage
out (GIGO) principle’
Validation can’t guarantee data entered is correct but can only make sure data is reasonable.
4 types of checks:
Range check
The Boolean variable is named validNum as a status flag – to initially set to False
Presence check
Length check
Normal data - This is data that is within the limits of what should be accepted by the program.
Boundary data - This is data that is at the outer limits of what should be accepted by the program. For example, if a
validation rule specifies that the range of acceptable values is >= 75 AND <= 100, then a value of 100 is at the upper limit
and a value of 75 at the lower.
Erroneous data – (not valid) This is data that should not be accepted by the program. For example, a value of 0 should
not be accepted by either of the validation rules given above.
To access a text file, you must first give it a file handle, e.g. myFile
The following program would create a text file called ‘names’ and add two items of data.
Writing to a file
Subprograms
Write it once, use it many times can reuse code def variable ( )
Functions
Local and global variables
Procedures
Unlike a function, a procedure does not return a value to the main program.
Arguments and parameters
Parameters – are the input value that you pass to the function
Functions and procedures are useful when using menus in a program. When a user selects a menu option, they can be
sent to a particular function or procedure.
The benefits of using subprograms
Repeated sections of code only need to be written once shortens development time means finished program will
occupy less memory space when it is run
Built-in functions
These are already written by python to make it easier for high-level programmers like print, count the number of
characters in a string and generate random numbers.
Logic errors
Ideally should be fixed at design stage, it occurs when the thinking behind an algorithm is incorrect so the output isnt
what is expected or intended.
Computer has no sense to find error program will run but the desired result won’t be achieved.
This is a logic error because it is an infinite loop as it will never reach 10.
Trace tables
Syntax errors
Misspelled
E,g: missing out closing brackets, missing out quotation marks, writing ‘prnit’ instead of ‘print’
Runtime errors
Most difficult to predict and spot as they occur during program execution
One of the most useful features is the debugger as it can flag up syntax errors.
The test plan
It is important to use ‘bottom up’ testing e.g.: test each subprogram as you develop it and then test the whole program
once it is finished
This is the completed test plan for the grade calculator program:
Evaluating programs
You need to identify strengths and weaknesses to identify techniques that work and aspects that can be improved.