Programming Soluions To Syllabus
Programming Soluions To Syllabus
Inputs can be identified by the keywords that precede them. These are:
GIVEN, ENTER, READ, or ACCEPT.
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.
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.
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
Relational Meaning
Operators
= 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.
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
Fals True
e
Tru False
e
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.
REPEAT
Print I love programming
count = count + 1
UNTIL count >= 10
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
Connector
Terminators or terminals
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.
Start
Enter the
marks
avgMark = totalMark/noOfMarks
Display
avgMark
Stop
Stop
Problem 2: Ask the user to enter two numbers then display the greater
number.
Start
Enter num 1,
num 2
Yes
Display num
num1 > 2
num2?
N
o
Display num
1
Stop
Problem 3: Print the number 1 to 5.
Start
num = 1
Yes
Display num
num < 5?
N
o
Stop
Problem 4: Read the marks of students terminated by 999. Find and print the
highest mark.
Flowchart diagram showing WHILE loop
Start
Highest = 0
Read mark
N
o
Read
mark <>
mark
999?
Yes
N
mark >
o
highest?
Yes
Highest = mark
Print
Problem 5: Print the numbers 1 to 5.
Highest
Flowchart diagram showing REPEAT loop
Stop
Start
number = 1
Display number
number = number + 1
number Yes
> 5?
No
Stop
count = 0
WHILE count <= 10 DO
count = count + 2
PRINT count
ENDWHILE
Cou OUTP
nt UT
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.
Objective 2.9 Use the top-down design approach to
problem solving.
Top-down Design
Print
wages