Chapter 4 - Standard Algorithms
Chapter 4 - Standard Algorithms
Chapter 4 - Standard Algorithms
- A set of instructions describing the steps followed in performing a specific task, for example, calculating
change.
- They are a sequence of instructions for solving a problem.
- Algorithms can be illustrated using the following:
Descriptions, Flowcharts, Pseudocodes, Structure diagrams
a. Descriptions:
- These are general statements that are followed in order to complete a specific task.
- They are not governed by any programming language. An example is as follows:
Enter temperature in oC
Store the value in box C
Calculate the equivalent temperature in oF
Store the value in box F
Print the value of box C and F
End the program.
b. Pseudocodes:
- These are English-like statements, closer to programming language that indicates steps followed in
performing a specific task.
- They are means of expressing algorithms without worrying about the syntax of the programming
language.
- There are no strict rules on how pseudocode statements should be written.
- Indentations are very important in writing pseudocodes since they clearly indicate the extent of loops
and conditional statements.
- They are however independent of any programming language.
- An example is as follows:
Enter centigrade temperature, C
If C = 0, then stop.
Set F to 32 + (9C/5)
Print C and F
End
A B C
The above 3 Pseudocodes produces the same result.
Cascaded/Nested If Statements
This is whereby if statements are found inside other if statements (nested Ifs) as shown below:
Start
Enter “First Number”, A
Enter “Second Number”, B
Enter “Third Number”, C
If A>B Then
If B>C Then
Print “A is the biggest Number”
End If
End If
End.
CASE Statement: This is an alternative to the IF...THEN...ELSE statement and is shorter. For example:
Enter first Number, A
Enter second number, B
Enter operand (+, -, * /)
CASE operand of:
“+”: C = A + B
“-”: C = A-B
“*”: C = A*B
“/”: C = A/B
ENDCASE
Print C
END
a. For...Next Loop: A looping structure that repeatedly executes the loop body for a specified number of
times. The syntax of the For...Next loop is as follows:
A group of statements between the looping structures is called the loop body and is the one that is
repeatedly executed.
The For...Next loop is appropriate when the number of repetitions is known well in advance, e.g. five
times. An example of a program that uses the For...Next loop is as follows:
Sum, Average = 0
FOR I = 1 to 5 DO
Enter Number
Sum = Sum + number
NEXT I
Average = Sum/5
Display Sum, Average
End
b. Repeat...Until Structure: This is a looping structure that repeatedly executes the loop body when the
condition set is FALSE until it becomes TRUE. The number of repetitions may not be known in advance
and the loop body is executed at least once. The syntax is as follows:
Repeat
Statement 1
Statement 2 loop body
................
Until {Condition}
For example
Sum, Average, Count = 0
Repeat
Enter Number (999 to end)
Sum = Sum + Number
Count = count + 1
Until Number = 999
Average = Sum / count
Print Sum, count, Average
End
In the above program:
- Count records the number of times the loop body executes.
- 999 is used to stop further data entry through the keyboard and thereby ending the loop. Such a
value that stops further data entry through the keyboard thereby terminating a loop is called a
Rogue value or sentinel.
WHILE {condition}
Statement 1
Statement 2 loop body
................
ENDWHILE
c. Flowcharts
It is a diagram used to give details on how programs and procedures are executed. Flowcharts are drawn
using specific symbols, each with its own meaning, as given below:
Symbol Explanation
Process Symbol - Indicates where some form of processing occur
Flowchart
Start
Enter number, A
Enter number, B
Sum = A + B
Display Sum
Stop
d. Structure Diagrams/Structure Charts: These are diagrams that show relationships between different
modules, thereby giving the structure of a program. They also illustrate the top-down approach to
programming. It is useful as a documentation of a complex program once it is completed. It resembles a
family tree as given below.
Start
Sum, Product = 0
Enter First Number, A
Enter Second Number, B
Sum = A + B
Product = A * B
Display Sum, Product
End
- The structure diagram above indicates five sub-programs of the program Process Numbers, namely
Initialise, Accept Numbers, Process Numbers, Display Results and Exit.
- The module Process Numbers has its own sub-programs, which are Add Numbers and Multiply
Numbers.
- Modules are appropriate for very large programs.
- If the module is repeatedly executed (loop), then an asterisk (*) must be placed at the top right
corner of the module (inside).
- All the boxes at the same level indicate selection.
- Boxes below others indicate sequence.
- The program can be written as a continuous single program as indicated on the right side of the
diagram.
Recursion
A recursive function or procedure occurs when the procedure calls itself other than calling another
procedure.
Recursion can be used when finding factorial of a number. For example
Function Factorial (n)
If n=1 then
Return 1
Else
Return n * Factorial (n-1)
End if
End Function
A recursive structure has a condition.
- It is also an error generated by entering the wrong data type during program execution, for example,
entering a text value where a numeric value is needed.
2. Debugging tools.
These are part of the software which help the user to identify where the errors are. The techniques available
include:
a) Cross-referencing.
This software checks the program that has been written and finds places where particular variables have
been used. This lets the programmer check to make sure that the same variable has not been used twice for
different things.
b) Traces.
A trace is where the program is run and the values of all the relevant variables are printed out, as are the
individual instructions, as each instruction is executed. In this way, the values can be checked to see where
they suddenly change or take on an unexpected value.
Test strategies are important to establish before the start of testing to ensure that all the elements of a
solution are tested, and that unnecessary duplication of tests is avoided.
Using VB 6.0, if you reach a point in your code that calls another procedure (a function, subroutine, or the
script associated with an object or applet), you can enter (step into) the procedure or run (step over) it and
stop at the next line. At any point, you can jump to the end (step out) of the current procedure and carry on
with the rest of the application.
Break points can be set within program code so that the program stops temporarily to check that it is
operating correctly to that point.
Step Into: Traces through each line of code and steps into procedures. This allows you to view the effect of
each statement on variables.
Step Over: Executes each procedure as if it were a single statement. Use this instead of Step Into to step
across procedure calls rather than into the called procedure.
DATA TESTING
After a program has been coded, it must be tested with different data types to determine if intended results
are produced. The types of test data that can be used include:
i. Extreme Data: Refers to the minimum and the maximum values in a given range. For example, a
computer program requires the user to enter any number from (between) 1 to 20. 1 and 20 are extreme data
and the computer must accept these. Thus extreme data is accepted by the computer.
ii. Standard (normal) Data: This refers to data that lies within (in-between) a given range. In our example
above, the numbers from 2 to 19 are standard data and are accepted by the computer.
iii. Abnormal Data: This refers to data outside a given range. As to our example above, the number 0, -1, -
50 and all number from 21 and above are abnormal data.
iv. Valid data: refers to data of the correct data type. Invalid data is data of the wrong data type. Thus if
the user enter the value “Terrence” instead of a number, this is referred to as a wrong (invalid) data type.
Only numbers are needed, not text.
PROGRAM TESTING
Can be done using the following testing methods:
Unit testing, Integration Testing, User acceptance testing, black box testing, white box testing, bottom-up
testing, top-down testing, etc.