Professional Documents
Culture Documents
IT NOTES - Problem and Program Design
IT NOTES - Problem and Program Design
Content Definition of the problem; propose and evaluate solutions; Determination of the
most efficient solution; develop and represent algorithm; test and validate the
solution; decompose a problem into input, processing, output and storage.
Inputs can be identified by the keywords that precede them. These are: GIVEN, ENTER, READ,
or ACCEPT.
Outputs can be identified by the keywords: PRINT, DISPLAY, FIND, PRODUCE, or OUTPUT.
Processing can be determined by asking;
“What do I have to do with the inputs in order to produce the desired output?” The
actions/tasks determined must be listed in a logical sequential order.
Example:
Given two numbers find and print their product.
Defining diagram:
Variables
In processing data, values that are manipulated need to be stored in a memory location.
Because of the large number of storage location in memory, we need to have an identifier to
label each location. Depending on if the value changes during the execution of the instructions,
it is called a variable. If the value does not change it is called a constant.
Each piece of data stored in memory is of a specific data type. In programming there are five (5)
basic data types.
Initial solution:
Start
Get first number, call it num1
Get second number, call it num2
Get third number, call it num3
Add num1 to num2 to num3, storing in sum
Divide sum by 3, storing in average
Print average
Stop
N.B. One more step and a variable is removed but the logic is maintained. The solution is more
efficient since less memory is required, less CPU time is required and the program executes
faster since there are fewer instructions to be executed.
N.B. This solution is more general. It can easily be adapted to find the average of any amount of
numbers by changing the value of N. It utilizes less memory since a single variable is used to
hold every number that will be entered.
Initializing variables
In the last solution given above, the variable sum was given the value 0. This is referred to as
initialization. This means giving the variable an initial or starting value. Sometimes it is
necessary to give variables a starting value. This ensures that any data previously stored in that
memory location, from a previous operation, is erased and not used mistakenly in a new
operation.
If a variable appears on the right hand side of an assignment statement before a value
is assigned to it, then it must be assigned an initial value before the assignment
statement is executed.
After evaluating and refining the solution, the next step is to choose the most efficient solution.
Use the following attributes to determine the most efficient solution:
1. It should be maintainable (i.e. easy to read and upgrade, if necessary).
2. Should use memory efficiently
3. Should be robust (be able to check for invalid input data)
E.g.
Start {terminator}
sum = 0
count = 0 {Declaration}
Repeat
Read num
count = count + 1
sum = sum + num {Body}
Until count > 10
Print sum
Stop {terminator}
Control Structures
In programming, control structures are used to represent logic. There are different types of
control structures: sequential, selection and loop (repetition). Just like a program, the body of
an algorithm is made up of various control structures.
N.B. If the decision consists of more than one instruction statement. These statements must be
enclosed in a BEGIN and an END e.g.
IF (A > B) THEN
BEGIN
C=A+B
Print C
END
ENDIF
age >= 50
= Equal to
Boolean Operators
These are also called logical operators. They are used when a selection is based upon one or
more decisions being TRUE or FALSE. The decisions are combined using Boolean operators: OR,
AND and NOT.
If the AND operator is used both conditions must be met, in order for the total expression to be
TRUE or FALSE.
If the OR operator is used, either conditions must be met, in order for the total expression to be
TRUE or FALSE.
To determine the outcome of Boolean operations, truth tables can be used. A truth table lists
all possible combinations of operands, and, for each combination, gives the value of the
expression.
E.g. A AND B
There are two operands, A and B. Since each operand can have up to two values, there are four
combinations of A and B:
A B
1. False False
2. False True
3. True True
4. True False
A B A AND B
A B A OR B
The truth table for NOT is different because it takes only one operand and has only two
combinations:
A NOT A
False True
True False
Activity:
REPEAT
Print “I love programming”
count = count + 1
UNTIL count > 10
For each loop example given above, the number of times the instructions have to be repeated
is known. As such, it is necessary to keep track of how many times the instructions are
repeated. This is done by counting or iteration, which involves increasing the value of a counter
variable by a fixed number every time the instructions are repeated. This counter can be part of
a condition as in the FOR loop.
Or within the body of the loop e.g. in WHILE loop and REPEAT UNTIL loop.
In both cases, the instructions stop repeating when the value of the counter becomes equal to a
certain value. N.B. Counter variables in the WHILE and REPEAT_UNTIL loops MUST be
initialized before the counter variable is incremented (increased).
When the number of times the instructions has to be repeated is NOT known, only the WHILE
loop or REPEAT_UNTIL loop can be used. These loops can allow instructions to be repeated until
a terminating value or sentinel value is inputted. The sentinel value causes the loop to stop. It
is a dummy value that signals the end of the data to be entered for processing.
In this construct the counter variable is initialized when the loop is first executed and is
increased by a fixed value each time the set of instructions is executed. The syntax for the FOR
loop can be either of the following depending on the problem given:
In this construct, the condition is tested at the start of the loop. If it is TRUE, the instruction
within the WHILE and ENDWHILE are executed until the condition becomes FALSE and the loop
is exited. Statements before and after the loop are carried out once. If the condition in the
WHILE loop is FALSE, the computer skips the instructions within the loop and continues with
statements after the loop.
In this construct, the condition is tested at the end of the loop. The instructions within the
REPEAT and UNTIL are executed until the condition becomes TRUE and the loop is exited. The
instructions within the REPEAT and UNTIL are always executed at least once. Just like the WHILE
loop, statements before and after the loop are carried out once. If after the first execution of
instructions within the REPEAT_UNTIL loop, the condition is FALSE, the computer exits the loop
and continues with statements after the loop. N.B. The REPEAT_UNTIL loop does not need the
BEGIN and END to enclose multiple statements.
FLOWCHARTS
A flowchart represents the steps in an algorithm using geometric symbols and arrows. The symbols
contain the instructions and the arrows show the order in which the instructions should be executed to
solve the problem.
FLOWCHART SYMBOLS
Start Stop
Decision
Terminators or terminals Connector
Input or Process
Output
1. The main symbols used in a flowchart are the Decision, Process and Terminal symbols.
4. Processes have only one entry point and one exit point.
5. Decisions have only one entry point, one TRUE exit point and one FALSE exit point.
6. The REPEAT loop has a process before the decision, since it always executes the process at least
once.
8. Use arrow-heads on connectors where the direction of flow may not be obvious.
Examples of a flowchart:
Start
avgMark = totalMark/noOfMarks
Display avgMark
Stop
Stop
Problem 2: Ask the user to enter two numbers then display the greater number.
Start
Yes
Display num 2
num1 > num2?
No
Display num 1
Stop
Problem 3: Print the number 1 to 5.
Start
num = 1
Yes
Display num
num < 5?
No
Stop
Problem 4: Read the marks of students terminated by 999. Find and print the highest mark.
Start
Highest = 0
Read mark
No
Read mark
mark <> 999?
Yes
No
mark >
highest?
Yes
Highest = mark
Print Highest
Stop
Problem 5: Print the numbers 1 to 5.
Start
number = 1
Display number
number = number + 1
No
Stop
Objective 2.8 Test algorithms for correctness
This is also called desk-checking. It allows the user to detect any logic errors by
tracing through the program. Dry-running involves executing the program
manually by using input values for variables and recording what takes place after
each instruction is executed. This method uses a trace table which is completed
upon manual execution of the program to record and determine what the
program is doing. To create a trace table, you must record all variables found in
the program as headings, as well as the heading ‘OUTPUT’ to represent what is
printed.
count = 0
WHILE count <= 10 DO
count = count + 2
PRINT count
ENDWHILE
Count OUTPUT
0 Count is set to 0; count < 10
2 2 2 is added to count and 2 is printed; count <10
4 4 2 is added to count and 4 is printed; count <10
6 6 2 is added to count and 6 is printed; count <10
8 8 2 is added to count and 8 is printed; count <10
10 10 2 is added to count and 10 is printed;
12 12 2 is added to count and 12 is printed; count not less than 10
so loop is exited.
Top-down Design
Print wages
This technique is called top-down design because the division of problems starts
at the top level and you work your way down.
The process of breaking down the problem into sub-problems and the sub-
problems into tasks and the tasks into actions is referred to as stepwise
refinement.