You are on page 1of 12

Programming

Theoretical

Nepal  Ammer   OL  computer  science  (Problem  solving  and  Programming)   225  


Pre-design stage (brain storming):
It is the design of breaking down a computer system into a set of sub-system, then
Top-down breaking each sub-system down into a set of smaller sub-systems, until each sub-system
design just performs a single action.
In order to show it in a diagrammatic form, we can draw structure diagram
Stepwise
It is the process of breaking down a system into smaller sub-systems.
refinement
• It is a set of programming statements/ instructions for a given task (for example: sort
some values in ascending order)
• It’s just a part of the whole system/program.
Sub-routine
• It’s written in a high-level programming language.
• Subroutines are sometimes called functions (must return a value) or procedures (don’t
have to return a value).
• It’s the diagram that shows all the detailed break-down/hierarchy of a system to sub-
systems, for example: the Alarm application for a smart phone

Structure
diagrams

Advantages of top-down designs and stepwise refinement:


1) Smaller programs (sub-programs) will be easier to manage, understand, test and debug
2) Development can be shared between a team of programmers (each person programming different
modules according to their individual strengths), thus reducing the development and testing time
3) Codes from developed modules can be re-used later on with other programs
Disadvantages of top-down designs and stepwise refinement:
1) We need to test each module separately then all the program
Ensure that all sub-programs are linked together
• It consists of some instructions (block of code/subroutine) that are ready to be used
• It’s already a given name, which could be called from another program.
Library routine
• They make writing and testing a program easier and faster, as this code is already
written, tested and debugged.

Program Algorithms:
 

It sets out the steps on a piece of paper to complete a given task in order to solve a problem. This
Algorithms
can be done either by flowchart or pseudo code.
Flowchart It is a graphical representation of the steps required for a task using standard shapes.
Pseudo It is a simple method of showing an algorithm, using English like words and mathematical operators
code that are set out to look like a high-level program notation.

Data Testing:
 

Nepal  Ammer   OL  computer  science  (Problem  solving  and  Programming)   226  


It shows the values of variables as you manually test your program (dry run), thus it
records the algorithm results; by recording the value of an item (variable) each time
Trace table
that it changes. A trace table is set up with a column for each variable and a
column for any output.
It is all the items of data required to work through a solution in order to determine
Test data whether a solution is working as it should or not. Always test the subsystems
separately before testing the whole system.
It is the data which lies within the acceptable range.
1) Normal The values accepted
Example: Mark is 70
2) Abnormal It is the data which lies outside the acceptable range. The values are
(erroneous) Example: Mark is -1, Two rejected
The largest and the smallest values of the acceptable range
3) Extreme The values accepted
of numbers (min/max). Example: Mark is 100,0
The min value and the value prior as well as the max value and
the value follows (one of them is accepted while the other is -1, 101 are rejected
4) Boundary rejected. Example: Mark is -1, 0 or 100, 101 [-1 and 101 are while 0 and 100 are
rejected boundaries, while 0 and 100 are accepted accepted.
boundaries]
 

Data Verification:
To minimise errors occurred while transferring data or transcripting data from one source to another.
This is usually carried out by the user/operators by one of the following methods:
1 Sight check (visual check/screen check):
It is a manual check completed by the user who is entering the data. When the data entry is
complete the data is displayed on the screen and the user is asked to confirm that it is correct
before continuing. The user either checks the data on the screen against a paper document
that is being used as an input form or confirms from their own knowledge if the data is about
them.
2 Double data entry check:
the same user (or 2 different users) enter the data twice to the computer. Both entries are
compared by the computer, if they are equal, so assumed to be entered correctly
3 Parity check
A parity bit is added to the bits/byte needed to be transmitted, to adjust the number of 1s on
the bytes, as before data is transferred, an agreement is made between sender and receiver
regarding which of the two types of parity are used, EVEN PARITY: have an even number of 1-
bits; ODD PARITY have an odd number of 1-bits.
4 Check sum
When a block of data is about to be transmitted, the checksum for the bytes is first of all
calculated. This value is then transmitted with the block of data. At the receiving end, the
checksum is recalculated from the block of data received. This calculated value is then
compared to the checksum transmitted. If they are the same value, then the data was
transmitted without any errors; if the values are different, then a request is sent for the data
to be retransmitted.

Nepal  Ammer   OL  computer  science  (Problem  solving  and  Programming)   227  


Data Validation:
Validation is the automated checking by a program that data is accurate, complete and reasonable
before it is accepted into a computer system. Different types of check may be used on the same piece
of data.When data is validated by a computer system, if the data is rejected a message should be
output explaining why the data was rejected and another opportunity given to enter the data.

Types of checks Description


Checks that the data entered is of a given data type, for example number of
1 Type check
brothers or sisters would be an integer (whole number).
checks that when a string of characters is entered it docs not contain any
Character invalid characters or symbols, for example a name would not contain
2
check characters such as %, and a telephone number would only contain digits or ,
or +
It checks either:
• That data contains an exact number of characters, for example that a
password must be exactly eight characters in length so that passwords
with seven or fewer characters or nine or more characters would be
3 Length check
rejected
• That the data entered is a reasonable number of characters, for example a
family name could be between two and 30 characters inclusive so that
names with one character or 31 or more characters would be rejected.
Check performed on numbers to make sure that a particular data item lies
4 Range check
within specified range. (Computer is given the min & max values)
Presence/
Checks to ensure that some data has been entered and the value has not
Lookup/
5 been left blank, for example an email address must be given for an online
existence
transaction.
check
Layout /Format A FORMAT CHECK Checks that the characters entered conform to a pre-
6
check defined pattern, for example: the cub number must be in the form CUB9999.
Consistency Checks if the fields correspond (tie up) with each other.
7
check
It is an extra digit appended to the code number, check digit is calculated
using the other digits and added to the end of the number.
It is re-calculated at a later stage to ensure t he validity of the data entry.
8 Check digit Method of calculating check digit, differs depending on the application

Check digits are used for barcodes, product codes, International Standard
Book Numbers (ISBN) and Vehicle Identification Numbers (VIN).
Uniqueness Checks that the data entered doesn't have any duplicates (for example:
9
check email address, account number, card number…etc)

Note:
In order to validate the data, we have to have clear set of rules that can be used (for example, fixed
length, fixed values..etc)

Nepal  Ammer   OL  computer  science  (Problem  solving  and  Programming)   228  


Data structures (Variable, constants, arrays):
Variable It’s the named data store that contains a value that may change during running the program
(example: loop counter).
Reason of using: used in calculations and loops
Constant It’s the named data store that contains a value that will not change during running the program
(example: NormalTemperature=37.0).

Reason of using: Used in calculations, also it’s defined only once, and referenced many times in
a program, thus, if you want to change the value of this constant it will be changed ONCE only
Array/list It is a container which can hold a fix number of items and these items should be of the same
type. Each array use the following terms:
• Element: Each item stored in an array is called an element.
• Index: Each location of an element in an array has a numerical index, which is used to
identify the element.
In order to repeat the same task many times for all the items in a list, each item needs to be
referred to in the same way using the index number for each element of the one -dimensional
array (for example: for i = 1 to 50, input name[i], next i)

Reason of using arrays:


1) To simplify the program
2) Make the program shorter
3) To store all details using one name and different indexes (as each index uniquely
identifies just one item across the array)

Note: A statement should not refer to a group of array elements individually. For example, the
following construction should not be used.
StudentNames [1 TO 30] ß ""
Instead, an appropriate loop structure is used to assign the elements individually. For example:
FOR Index ß 1 TO 30
StudentNames[Index] ß""
NEXT Index

In order to make programs understandable to others, variables/constants/arrays/other identifiers


should be given a meaningful name

Nepal  Ammer   OL  computer  science  (Problem  solving  and  Programming)   229  


Data types:
Description Literals (how it’s written)
Integer Positive or negative whole number that Written as normal in the denary system,
can be used in mathematical calculations e.g. 5, -3
(Example: NumberOfSiblings = 3)
Real/float Positive or negative number with Always written with at least one digit on
fractional part that can be used in either side of the decimal point,
mathematical calculations (Example: zeros being added if necessary,
Weight = 65.7) e.g. 4.7, 0.3, -4.0, 0.0
Char A single character (Example: gender= ‘F’) Char: A single character delimited by
single quotes, e.g. ‘x’, ‘C’,’@’
String It’s several characters in length. strings String: Delimited by double quotes. A
vary in length and may even have no string may contain no characters (i.e. the
characters (empty string). These empty string) e.g. "This is a string", ""
characters can be letters and/or digits
and/or printable symbols. (Example:
telephoneNumber = “01227713371”,
Name= “Nepal”)
Boolean It is the variable that can have only two Boolean: TRUE, FALSE
values true or false (example: Won)

Storing data with correct data type is very important, in order to have an effective manipulation (for
example: numbers in calculations, characters in concatenation), also in some case when choosing the
correct type is like automatic validation (char one digit, Boolean two values only..etc)

Nepal  Ammer   OL  computer  science  (Problem  solving  and  Programming)   230  


Programming techniques:
Sequence Statements will run/executed one after another in order/sequence, so that the order of
the statements is very important (we have to pass through them all)
Assignment The identifier on the left of the ß is assigned a value/expression on the right. The
statements expression on the right may be a single value or several values combined with
mathematical operators which are +, -, *, /, ^, ( )
<identifier> ← <value>
Input statements often provide values for assignment statements. Output statements
often use the results from assignment statements
(Example: Y ß X+1 chosen ß False Gender = ‘M’ )
Selection/ • It decides which action/statement should be taken (by an algorithm) according to
conditional the values of the variables (criteria), for Example: selecting the largest value or the
statement smallest value, selecting items over a certain price, selecting everyone who is female.
• There are two types of conditional statement as shown below with an example of
each.
CASE …OF … OTHERWISE
IF ... THEN ... ELSE ... ENDIF
…ENDCASE
It can be used to check complex conditions, check It allows one out of several
range of values, used with condition that can be branches of code to be executed,
either true or false. depending on the value of a
variable, as it checks/chooses
Note: Nested IF is when the second IF statement from discrete/separate specific
is part of the ELSE path of the first IF values, thus it makes the code
statement. simpler and more understandable
For a CASE condition the value of
For an IF condition the THEN path is followed if the
the variable decides the path to be
condition is true and the ELSE path is followed if
taken. Several values are usually
the condition is false. There may or may not be an
specified. OTHERWISE is the path
ELSE path. The end of the statement is shown by
taken for all other values. The end
ENDIF. A condition can be set up in different ways:
of the statement is shown by
ENDCASE.
• Using a Boolean variable that can have the value
TRUE or FALSE. For example
For example
IF Found
THEN PRINT "Your search was successful"
CASE Grade OF
ELSE PRINT "Your search was unsuccessful"
“A” : PRINT “Excellent”
ENDIF
“B” : PRINT “Good”
“C” : PRINT “Average”
• Using comparison operators. For example
OTHERWISE PRINT
IF ((Height > 1) OR (Weight > 20) OR (Age > 5)) AND
"Improvement is needed"
(Age < 70)
ENDCASE
THEN PRINT "You can ride"
ELSE PRINT "Too small, too young or too old"
Note: you don't have to write the
ENDIF
otherwise line
Totalling It is used with repetition with the total updated every time the loop is repeated.
Example: Receipt_Total ß Receipt_Total + Item_Price
Counting Used to count how many item in a list/that matches certain criteria. It is used with
repetition with the counter increased/decreased by 1 every time the loop is repeated.
Example1 (Count up): NumberOfitems ß NumberOfitems + 1 (count sold items)

2 (count number in stock)


Example (count down): NumberInStock ß NumberInStock – 1
Repetition/ When some actions performed as part of an algorithm need repeating, this is called

Nepal  Ammer   OL  computer  science  (Problem  solving  and  Programming)   231  


Iteration ‘iteration’. Most programming languages support three types of loop:
• A fixed number of repetitions (for i= 1 to 20 ------- Next i)
• An unknown number of repetitions with at least one repetition, as the condition is
tested at the end of the loop (Repeat ----- until x>1)
• An unknown number of repetitions, which may not be completed at all, as the
condition is tested at the beginning of the loop (While x>y do---End while)
all loops are controlled by a control variable (i) which has to be initialised, updated and
tested.
There are three different types of loop structure:

FOR ...TO ... NEXT REPEAT ... UNTIL WHILE...DO...ENDWHILE


Count controlled Post condition Pre condition
A variable is set up with a start The statements in The condition is tested
value and an end value and then the loop will be before the statements, and
incremented in steps of one until executed at least the statements will only be
the end value is reached and the once. The condition is executed if the condition
iteration finishes. Thus, the tested after the evaluates to TRUE. After
statements between for and next statements are the statements have been
will be repeated for certain executed and if it executed the condition is
number of times.
evaluates to TRUE tested again. The loop
the loop terminates, terminates when the
Note:
otherwise the condition evaluates to
for i ß 0 to 20 step 2
statements are FALSE.
------
executed again. The statements will not be
Next i
executed if, on the first
(in this case the identifier i is
incremented by 2 steps not just test, the condition
one) evaluates to FALSE.
Reason for choice: Reason for choice: Reason for choice:
• Known number of iterations. • We need to enter • It’s required to terminate
• Inputting/printing/reading the loop once then the loop if a certain
values into arrays carry out the test. condition happen
• Unknown number of • The test is performed
iterations before entering the loop
• Known termination • May never run if the test
condition was false
Unknown number of
iterations
… Examples …
FOR Counter ← 1 to 10 Total ← 0 Total ← 0
PRINT “Enter Name of Mark ← 0 PRINT “Enter value for
Student” REPEAT mark, -1 to finish”
INPUT StudentName Total ← Total + INPUT Mark
[Counter] Mark WHILE Mark <> -1 Do
NEXT Counter PRINT “Enter value Total ← Total + Mark
for mark, -1 to PRINT “Enter value for
finish” mark, -1 to finish”
INPUT Mark INPUT Mark
UNTIL Mark = -1 ENDWHILE

Input It is used to enter data to the program


statement Example: INPUT Mark

Nepal  Ammer   OL  computer  science  (Problem  solving  and  Programming)   232  


Output It is used to output/print data out of the program
statement Example: print “the highest mark is”, max
Comment To add some notes in your pseudo code that will not be executed, they are preceded by two
statement forward slashes // and continues until the end of the line, thus each line is preceded by // in
multi-line comments

Operators:

Comparision/logical/relational operators Mathematical operators


Operator Comparison Operator Comparison + addition (and concatenate text)
> greater than AND both - subtraction
< less than OR either * multiplication
= equal NOT not / division
>= greater than or equal () group ^ Power
<= less than or equal () Grouping
<> not equal

Note:
Care should be taken with the division operation: the resulting value should be of data type REAL, even if the
operands are integers.

The integer division operators MOD and DIV can be used. However, their use should be explained explicitly and
not assumed.

Library routine (functions) Description


Returns the remainder
MOD
MOD(32, 10) = 2
Returns the Integer division
DIV
DIV(32, 10) = 3
Shows the integer part of a number
INT
Int (2.9) = 2.0
Shows random real/float numbers between 0 and 1
RND ( )
RND () = 0.1
Shows random Integers between x and y
Randombetween (x, y)
Randombetween (1,10) = 2

Nepal  Ammer   OL  computer  science  (Problem  solving  and  Programming)   233  


Stages in producing an algorithm
1. Make sure that the problem is clearly specified.
2. Break the problem down into sub-problems; if it is complex, you may want to consider writing an
algorithm for each sub-problem. Most problems, even the simplest ones can be divided into:
• Setup (preparing the data table that stores the variable, constants, arrays and other
identifiers names, data type and description)
• Input
• Processing
• Output of results.
3. Decide on
• How any data is to be obtained and stored?
• What is going to happen to the data?
• How any results are going to be displayed?
4. Decide on how you are going to construct your algorithm, using a flowchart or pseudo code?
5. Construct your algorithm, making sure that it can be easily read and understood by someone
else. This involves setting it out clearly and using meaningful names for any data stores.
6. Use several sets of test data (normal, abnormal and boundary) and trace tables to find any
errors.
7. If any errors are found, repeat the process until you think that your algorithm works perfectly.

The effectiveness of a solution


There are many different solutions to the same problem. In order to consider the effectiveness of a
given solution ask the following questions.
1. Does the solution work for all sets of data?
2. Does the solution have any unnecessary processes that are never used?
3. Are any actions repeated more often than necessary?
4. Can the solution be simplified and still work as well?

Nepal  Ammer   OL  computer  science  (Problem  solving  and  Programming)   234  


Algorithms concepts
Flowcharts symbols and their meaning:

Start   End   Start and End of the program

X  ç  0
Y ç 100 Processing
(Calculations or initializing/assigning)
Z ç 100 X  ç  X  +  1

Read/Write
Input  X   Output  Y   (Input from a keyboard or
output on the screen)

Decision/if

Flow lines/ direction

Nepal  Ammer   OL  computer  science  (Problem  solving  and  Programming)   235  


Using pre-release material
Here is a checklist of useful things to do.
1. Read through the pre-release material several times.
2. For each task, write an algorithm using both pseudo code and a flowchart to show what is required.
3. Choose sets of test data that you will need to use, and work out the expected results. Remember
to use normal, boundary and erroneous/abnormal data. Be able to give reasons for your choice of
test data.
4. Complete trace tables to test your pseudo code and flowcharts. This will enable you to ensure that
both the pseudo code and the flowcharts work properly.
5. Decide which works best for each task, pseudo code or a flowchart, and why.
6. Before starting to write your program for each task:
• Decide the variables, including any arrays, and constants you will need
• Decide the data types required for these
• Decide the meaningful names you will use
• Be able to explain your decisions.
7. If you are asked to repeat the same thing many times, for example adding up totals, complete the
task for one and check it works before repeating it many times.
8. Write and test each task. You can use the same test data as you used for your pseudo code and
flowcharts.

Nepal  Ammer   OL  computer  science  (Problem  solving  and  Programming)   236  

You might also like