0% found this document useful (0 votes)
0 views27 pages

Programming Lesson Notes (1)

This document provides lesson notes for Grade 11 ICT programming, focusing on breaking down problems into Input, Process, and Output, and understanding algorithms and control structures. It covers flowcharts, pseudocode, and introduces Pascal programming, including identifiers, reserved words, data types, variables, constants, and operators. The notes emphasize practical exercises to reinforce learning through practice problems and examples.

Uploaded by

sharadacompany
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views27 pages

Programming Lesson Notes (1)

This document provides lesson notes for Grade 11 ICT programming, focusing on breaking down problems into Input, Process, and Output, and understanding algorithms and control structures. It covers flowcharts, pseudocode, and introduces Pascal programming, including identifiers, reserved words, data types, variables, constants, and operators. The notes emphasize practical exercises to reinforce learning through practice problems and examples.

Uploaded by

sharadacompany
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ICT · Grade 11 · Programming — Lesson Notes

GRADE 11 · ICT · CHAPTER 1

Programming
Lesson Notes — from problems to Pascal code

INPUT — Raw → PROCESS — Step-by- → OUTPUT — The result


material step work

Name: ______________________________ Class: ____________ Date: ____________

Page 1 of 27
ICT · Grade 11 · Programming — Lesson Notes

What You Will Learn


By the end of this chapter you should be able to:
– Break a problem down into Input, Process and Output, and recognise that a problem may have more than one
valid solution.
– Explain and identify the three control structures used in every algorithm: Sequence, Selection and Repetition.
– Draw a flowchart and write a pseudocode for a given problem, and convert between the two.
– Read and write simple Pascal programs using variables, operators, IF/CASE, loops, arrays and sub-programs.
– Understand why programming languages exist and how they let us instruct a computer.

How to use these notes: Each section below ends with a short Practice box. Work through it before moving to the
next section — programming is learned by doing, not just reading.

SECTION 1.1

Analyzing a Problem
Breaking a problem into Input, Process and Output

Input → Process → Output


Whatever problem you are solving — by hand or with a computer — it can always be broken into three parts. The
Input is the raw material you start with. The Process is the ordered set of steps that turns that input into a result.
The Output is the result you end up with.

INPUT → PROCESS → OUTPUT

Example 1 — Posting a letter


Part Details

Input A sheet of paper, a pen, an envelope, a stamp and glue

Process 1. Write the letter 2. Fold it and put it in the envelope 3.


Seal the envelope 4. Write the address 5. Stick the stamp

Output A letter ready to be posted

Notice: Steps 4 and 5 can swap order without changing the result, but steps 1–3 must stay in that exact order.

Example 2 — Making a cup of tea


Part Details

Input Tea leaves, sugar, hot water

Process Put leaves in a strainer and pour hot water over them, add
sugar, stir, then taste — if it isn't sweet enough, add more
sugar and stir again

Output A cup of tea

Look closer: "Taste, and repeat if needed" is a Repetition hiding inside this problem — you will meet this control

Page 2 of 27
ICT · Grade 11 · Programming — Lesson Notes

Part Details

structure properly in section 1.2.

Alternative Solutions
Most problems can be solved in more than one way. Every possible way of solving a problem together makes up its
solution space. A good programmer compares the alternatives and picks the most efficient one — usually the one
with the fewest steps or calculations.

Example — Perimeter of a rectangle


Solution Formula Comment

1 P=l+w+l+w Uses only addition

2 P = l×2 + w×2 Uses multiplication and addition

3★ P = (l + w) × 2 Fewest operations — the most efficient


choice

Example — Pass or fail


Given a mark, decide whether a student has passed (mark ≥ 35) or failed. There are two equally valid ways to write
the condition:

Solution 1: Solution 2:
IF mark < 35 THEN IF mark >= 35 THEN
Result = Fail Result = Pass
ELSE ELSE
Result = Pass Result = Fail

✎ PRACTICE — 1.1
Analysing problems and comparing solutions
1. For the problem "sharing 100 toffees equally among 20 people", write down the Input, the Process and the
Output.
2. Identify the Input, Process and Output for the task of making a paper kite.
3. Write two different formulas (alternative solutions) that could calculate the area of a square, and say which
one is simplest.
4. A shop gives a 10% discount on bills over Rs. 2000. Write two alternative ways of expressing this rule using
words.
5. Explain, in your own words, why comparing alternative solutions is useful before you start programming.

Page 3 of 27
ICT · Grade 11 · Programming — Lesson Notes

SECTION 1.2

Algorithms & Control Structures


Turning a plan into an ordered set of steps

What is an Algorithm?
An algorithm is a step-by-step procedure for solving a problem. It is written in plain language, independent of any
computer or programming language, so that anyone reading it can follow exactly what to do and in what order.

Example — Algorithm to post a letter


1. Write the letter
2. Fold the letter
3. Insert the letter into the envelope
4. Write the address
5. Stick the stamp
6. Post the letter

Key idea: Steps 1–3 must be followed in strict order. Steps 4 and 5 may be swapped without affecting the outcome
— this is what we mean by a sequential vs. a flexible step.

The Three Control Structures


Every algorithm, no matter how complex, is built from just three basic building blocks:
SEQUENCE SELECTION REPETITION

Step 1 Condition? Do step

↓ ↓
YES NO
Step 2 Done?
Path A Path B
↓ NO → repeat "Do step"

↓ YES — continue
Step 3

1. Sequence
Steps are carried out one after another, from beginning to end, in a fixed order.
– Climbing a staircase, one step at a time.
– A student progressing from Grade 1 through to Grade 13.

2. Selection
A condition is tested, and exactly one of two possible actions is carried out depending on whether it is True or False.
– Passing a subject: IF mark ≥ 35 → Pass, ELSE → Fail.
– Buying a book: IF you have enough money → buy it, ELSE → you cannot.

3. Repetition
One or more steps are repeated until a condition is satisfied.
– Marking an attendance register: call each name and mark it, repeating until the last name is called.

Page 4 of 27
ICT · Grade 11 · Programming — Lesson Notes

– Counting the words in a paragraph: read a word and add 1 to the count, repeating until the paragraph ends.

✎ PRACTICE — 1.2
Identifying control structures
1. State which control structure (Sequence, Selection or Repetition) best describes: "Ask every student in the
class, one by one, whether they want rice or noodles for lunch."
2. Write a short algorithm (in numbered steps) to decide whether a person can vote, given that the voting age
is 18.
3. Write an algorithm using repetition to print the 5-times table from 5 to 50.
4. Give one real-life example (not from these notes) of each control structure: Sequence, Selection,
Repetition.
5. Explain why almost every useful computer program needs all three control structures, not just one.

Page 5 of 27
ICT · Grade 11 · Programming — Lesson Notes

SECTION 1.3

Flowcharts & Pseudocode


Two ways to picture the same algorithm

Flowchart Symbols
A flowchart presents an algorithm visually, using standard shapes to show what kind of action each step performs.

Symbol What it means

Start / End (rounded shape) Marks the beginning or end of the algorithm

Input / Output (slanted box) Data going into or out of the process

Process (rectangle) A calculation or action step

Decision (diamond) A Yes / No condition that branches the flow

Flow line (↓ arrow) Shows the direction the algorithm runs

Connector (○ circle) Links parts of a large flowchart

Sequence example — Area of a circle

Start


Every box here runs exactly once, from top to bottom,
Input radius with no decisions and no repeats — that makes this a
pure sequence.

Area = pi × r × r BEGIN
INPUT Radius
↓ Area = 22/7 * Radius * Radius
DISPLAY Area
Output area END.


End

Selection example — Odd or even number

Start
The diamond checks a single condition. Exactly one of
the two branches runs, depending on whether the
↓ remainder is 0.

Input number N BEGIN


READ number as N
↓ remainder = N MOD 2
IF remainder = 0 THEN
remainder = N MOD 2
DISPLAY "Even number"
↓ ELSE
DISPLAY "Odd number"
remainder = 0 ? ENDIF

Page 6 of 27
ICT · Grade 11 · Programming — Lesson Notes

YES NO
END.
"Even number" "Odd number"

Repetition example — Total of several numbers

Start


Total = 0 While the answer to "More numbers?" is YES, the flow
loops back and repeats the same two steps. It only
↓ continues onward once the condition becomes NO.
Get number N
BEGIN
↓ Total = 0
REPEAT
Total = Total + N READ Number as N
Total = Total + N
↓ UNTIL numbers are over
DISPLAY Total
More numbers?
END.
YES → repeat from "Get number N"

↓ NO

Output Total

Page 7 of 27
ICT · Grade 11 · Programming — Lesson Notes

Pseudocode Keywords
Pseudocode expresses an algorithm in plain English, independent of any specific programming language, which
makes it easy to translate later into Pascal, Python, or any other language.

Keyword(s) Meaning

BEGIN / END Marks the start / end of the algorithm

INPUT, READ, GET Take in a value

OUTPUT, DISPLAY, SHOW Show a result

PROCESS, CALCULATE Perform a calculation

IF … THEN … ELSE … ENDIF Selection

FOR … DO Counted repetition

WHILE … ENDWHILE / REPEAT … UNTIL Conditional repetition

Worked pseudocode — Total & average of 10 numbers

BEGIN
Total = 0
Average = 0
n = 1
WHILE n <= 10
READ Number
Total = Total + Number
n = n + 1
ENDWHILE
Average = Total / (n - 1)
DISPLAY Total, Average
END.

Trace through what happens to each variable as the loop runs:

n Number read Total

1 12 12

2 15 27

… … …

10 — final sum

11 (loop stops) — Average = Total / 10

Converting between flowchart and pseudocode — Making tea

BEGIN
Put tea bag in cup
WHILE (not water boiled)
Boil water
ENDWHILE
Pour water in cup
WHILE (sugar needed)
Add sugar
Stir tea

Page 8 of 27
ICT · Grade 11 · Programming — Lesson Notes

ENDWHILE
END.

Conversion rule: Every diamond with a loop-back becomes a WHILE…ENDWHILE. Every plain rectangle becomes one
pseudocode instruction line.

Page 9 of 27
ICT · Grade 11 · Programming — Lesson Notes

✎ PRACTICE — 1.3
Flowcharts and pseudocode — 20 questions, easiest to hardest

Level 1 — Warm-up (pure sequence)


1. Draw a flowchart that inputs a number and simply displays it back.
2. Write pseudocode to input two numbers and display their sum.
3. Draw a flowchart to find the area of a triangle, given its base and height (Area = 0.5 × base × height).
4. Write pseudocode to convert a temperature from Celsius to Fahrenheit (F = C × 9/5 + 32).
5. Draw a flowchart that inputs a person's name and age, then displays a greeting message using both.
Level 2 — One control structure (a single IF or a single loop)
6. Draw a flowchart to check whether a number entered is positive or negative.
7. Write pseudocode to decide whether a student has passed or failed, given a pass mark of 40.
8. Draw a flowchart to print the numbers from 1 to 20 using repetition.
9. Write pseudocode (using FOR) to display the square of every number from 1 to 10.
10. Draw a flowchart to find the larger of two numbers entered by the user.
Level 3 — Combining structures (loops + decisions together)
11. Write pseudocode to find the total and average of 20 numbers entered by the user.
12. Draw a flowchart to find the largest of three numbers.
13. Write pseudocode that counts how many of 15 numbers entered are positive, how many are negative, and
how many are zero.
14. Draw a flowchart to assign a grade (A/B/C/S/F) to a student based on their mark, using nested selection.
15. Convert this into pseudocode: a flowchart where a user enters an amount to withdraw from an ATM; if
the amount is more than the balance, display "Insufficient funds", otherwise subtract it from the balance
and display the new balance.
Level 4 — Challenge (classic algorithmic problems)
16. Write pseudocode to calculate the factorial of a number N (e.g. 5! = 5×4×3×2×1) using repetition.
17. Draw a flowchart to determine whether a number is prime (hint: check whether it divides evenly by any
number from 2 up to N−1).
18. Write pseudocode to print the first N terms of the Fibonacci sequence (each term is the sum of the two
before it: 0, 1, 1, 2, 3, 5, 8 …).
19. Draw a flowchart for a simplified ATM: the user enters a PIN (allow up to 3 attempts before locking the
card), then enters a withdrawal amount, which is only approved if it does not exceed the balance.
20. Write pseudocode to sort three numbers into ascending order, without using an array — only comparisons
and swaps.

Page 10 of 27
ICT · Grade 11 · Programming — Lesson Notes

SECTION 1.4

Pascal Programming
Turning pseudocode into a real, running program

1.4.1 Identifiers
An identifier is the name you give to a variable, constant or program.
– Must start with a letter (A–Z, a–z)
– After the first letter, only letters, digits (0–9) and underscore ( _ ) are allowed
– No spaces or special symbols ( ! @ # $ % etc.)
– Not case sensitive — Art, art and ART are treated as the same identifier
– Cannot be a reserved word (e.g. BEGIN, END, IF)
Valid identifiers Invalid identifiers

Sum, Total_Nos, FirstName $75 (starts with a symbol)

Student_Name, Num1 Average Marks (contains a space)

avg, Last_Name 9A (starts with a digit)

1.4.2 Reserved Words


Reserved words already have a fixed meaning in Pascal, so they can never be used as identifiers. Some common
ones:

begin end if then

else for while do

repeat until var const

function procedure case of

array program div mod

1.4.3 Standard Data Types


Data type Stores Example

integer Whole numbers (+ / −) 0, 46, -12

real Decimal numbers (+ / −) 0.0, 25.68

boolean True or False True

char One keyboard character 'k', '#', '7'

string A sequence of characters 'ICT', 'Sri Lanka'

Important: Values of char and string type are always written inside single quotation marks, e.g. 'k' or 'ICT'.

1.4.4 Variables & Constants


A variable is declared with var, and its value can A constant is declared with const, and its value never
Page 11 of 27
ICT · Grade 11 · Programming — Lesson Notes

change while the program runs.


changes once set.

var count : integer;


const max = 100;
var a, b : real;
const pie = 22/7;
var Name, school : string;

1.4.5 Operators
Arithmetic operators
Operator Meaning Example Result

+ Addition 6+3 9

- Subtraction 7-5 2

* Multiplication 2*5 10

/ Division 10 / 4 2.50

DIV Whole-number division 20 DIV 6 3

MOD Remainder after division 20 MOD 6 2

DIV & MOD: 20 ÷ 6 goes in 3 whole times with 2 left over, so 20 DIV 6 = 3 and 20 MOD 6 = 2.

Comparison operators
Comparisons always produce a Boolean result — True or False.

Operator Meaning Example Result

> Greater than 7>3 True

>= Greater than or equal 8 >= 8 True

< Less than 3<2 False

<= Less than or equal 4 <= 6 True

= Equal 3=1 False

<> Not equal 2 <> 5 True

Logical operators — AND, OR, NOT


A B A AND B A OR B

False False False False

False True False True

True False False True

True True True True


NOT simply flips a value: NOT(True) = False, and NOT(False) = True.

Operator precedence (highest to lowest)


Priority Operators

1 (highest) NOT

Page 12 of 27
ICT · Grade 11 · Programming — Lesson Notes

Priority Operators

2 * / DIV MOD AND

3 + - OR

4 (lowest) = <> < <= > >=

✎ PRACTICE — 1.4 (a) — Identifiers, data types & operators


1. State whether each is a valid Pascal identifier: Marks2, 2ndMark, Student Age, total_sum.
2. Which data type would you use to store: (a) a phone number's area code (b) a person's average mark (c)
whether a student has passed (d) a student's initial?
3. Evaluate: 5 + 14 MOD 4
4. Evaluate: NOT(8 MOD 2 > 5)
5. Evaluate: (Height > 150) AND (Age < 15), given Height = 160 and Age = 16.

Page 13 of 27
ICT · Grade 11 · Programming — Lesson Notes

Anatomy of a Pascal Program

program addNum(input, output); { name of the program }


var num1, num2, total : integer;
avg : real; { declaring the variables }
begin { start of the main program }
writeln('Enter First Number'); read(num1);
writeln('Enter Second Number'); read(num2);
total := num1 + num2;
avg := total / 2;
writeln('Total is ', total);
writeln('Average is ', avg);
end. { end of the main program }

– read( ) / readln( ) bring a value IN from the keyboard.


– write( ) / writeln( ) send a value OUT to the screen.
– Every statement ends with a semicolon ( ; ).
– := is the assignment operator — it stores a value into a variable.

1.4.6 Selection in Pascal


IF … THEN … ELSE … ENDIF

program LargeNo(input,output);
var N1, N2, Large : integer;
begin
writeln('Enter Two Numbers'); read(N1, N2);
if N1 > N2 then
Large := N1
else
Large := N2;
writeln('Large Number is ', Large);
end.

Nested IF — Grading a mark

if M >= 75 then
Grade := 'A'
else
if M >= 65 then
Grade := 'B'
else
if M >= 50 then
Grade := 'C'
else
if M >= 35 then
Grade := 'S'
else
Grade := 'F';

Why "nested"?: Each ELSE contains another whole IF statement inside it. Only one branch ever runs, chosen from
the top down.

Page 14 of 27
ICT · Grade 11 · Programming — Lesson Notes

CASE — a cleaner way to grade marks

program FindGrade(input,output);
var Marks : integer; Grade : char;
begin
writeln('Enter Marks'); read(Marks);
case Marks of
0..34 : Grade := 'W';
35..49 : Grade := 'S';
50..64 : Grade := 'C';
65..74 : Grade := 'B';
75..100 : Grade := 'A';
else writeln('Invalid Marks');
end;
writeln('Grade is ', Grade);
end.

✎ PRACTICE — 1.4 (b) — Selection


1. A student scores 58 marks. Using the CASE statement above, what grade would they get?
2. Rewrite the nested-IF grading example as a flowchart (use the diamond symbol for each condition).
3. Write an IF…THEN…ELSE statement that prints 'Even' or 'Odd' for a number N.
4. What is the output of: if (10 > 5) AND (3 > 8) then writeln('Yes') else writeln('No'); ?

Page 15 of 27
ICT · Grade 11 · Programming — Lesson Notes

1.4.7 Repetition in Pascal


FOR … TO / DOWNTO … DO — when the number of repeats is known

for count := 1 to 10 do for count := 10 downto 1 do


writeln(count); writeln(count);
{ prints 1,2,3 ... 10 } { prints 10,9,8 ... 1 }

WHILE … DO vs. REPEAT … UNTIL — when repeats are not known in advance
WHILE … DO REPEAT … UNTIL

Condition checked BEFORE the loop body runs Condition checked AFTER the loop body runs

If false at the start, the body never runs The body always runs at least once

Stops when the condition becomes FALSE Stops when the condition becomes TRUE

number := 1; count := 0;
while number <= 10 do repeat
number := number + 1; writeln('Pascal');
count := count + 1;
until count > 5;

✎ PRACTICE — 1.4 (c) — Repetition


1. How many times does the loop body run? for i := 1 to 8 do writeln(i);
2. Trace this: count := 0; repeat count := count + 5; until count >= 20; — what is the final value of count, and
how many times did the loop run?
3. Rewrite this WHILE loop as a REPEAT…UNTIL loop: n := 1; while n <= 5 do begin writeln(n); n := n + 1; end;
4. Write a FOR loop that prints every even number from 2 to 20.

1.4.8 Nested Control Structures


Control structures can be placed inside one another — a selection inside a repetition, or a repetition inside a
selection.

Repetition inside Selection

if cho = 'A' then


for num := 1 to 6 do
writeln(num); { ascending }
if cho = 'D' then
for num := 6 downto 1 do
writeln(num); { descending }

Selection inside Repetition

for count := 1 to 10 do
begin
read(num);
if num mod 2 = 0 then
e_count := e_count + 1

Page 16 of 27
ICT · Grade 11 · Programming — Lesson Notes

else
o_count := o_count + 1;
end;

Page 17 of 27
ICT · Grade 11 · Programming — Lesson Notes

1.4.9 Arrays
An array stores many values of the same data type under a single variable name, instead of needing a separate
identifier for each value.

45 75 36 81 60
num[0] num[1] num[2] num[3] num[4]

var marks : array[0..9] of integer; { holds 10 whole numbers, indexed 0 to 9 }

Worked example — Highest mark & average of 35 students

program ictMarks(input,output);
var marks : array[0..34] of integer;
i, tot, max : integer; avg : real;
begin
for i := 0 to 34 do
begin
read(marks[i]);
tot := tot + marks[i];
end;
avg := tot / 35;
max := marks[0];
for i := 1 to 34 do
if marks[i] > max then max := marks[i];
writeln('Maximum marks = ', max);
writeln('Average marks = ', avg);
end.

1.4.10 Sub-programs — Functions & Procedures


As a program grows, splitting it into smaller reusable pieces — sub-programs — makes it easier to read, test and
maintain.
PROCEDURE FUNCTION

Input Input

↓ ↓
PROCEDURE FUNCTION
No value returned

Returns a value

Procedure Function

Performs a task but does NOT return a value Performs a task and DOES return a value to the caller

procedure calculateArea(var radius:real); function calculateArea(var radius:real):real;

function processArea(var radius:real):real;


var area:real;

Page 18 of 27
ICT · Grade 11 · Programming — Lesson Notes

begin
area := pi * radius * radius;
processArea := area; { the function's own name carries the return value }
end;

✎ PRACTICE — 1.4 (d) — Arrays & sub-programs


1. Declare an array called ages that can store the ages (whole numbers) of 25 students.
2. Write a FOR loop that prints every element of an array called scores with indices 0 to 9.
3. A sub-program calculates and prints the circumference of a circle, without sending any value back. Is it a
Function or a Procedure?
4. Write a Function called cube that accepts one integer and returns its cube (x × x × x).

Page 19 of 27
ICT · Grade 11 · Programming — Lesson Notes

SECTION 1.5

Evolution of Programming Languages


Why we need a language between people and machines

1.5.1 Why We Need a Programming Language


A program is an ordered sequence of instructions that makes a computer perform a task. Computers only
understand patterns of 1s and 0s (machine code) — a programming language, like Pascal, is the bridge that lets us
express an algorithm in a form close to human language, which is then translated for the machine to carry out.

✎ PRACTICE — 1.5
1. In your own words, explain why writing directly in 1s and 0s would be impractical for most programmers.
2. Name one advantage of using pseudocode before writing code in any specific programming language.

Page 20 of 27
ICT · Grade 11 · Programming — Lesson Notes

Worked Examples — From Problem to Program


These three examples walk through the whole process, start to finish: understanding the problem, analysing its
Input/Process/Output, writing the algorithm, then the pseudocode, and finally the Pascal program. Use them as a
model for your own Practice answers.

WORKED EXAMPLE 1: Pass or Fail classifier

The problem
A teacher enters a student's ICT mark. The program should display "Pass" if the mark is 35 or above, and "Fail"
otherwise.
Input · Process · Output

Input Process Output

The student's mark (M) Compare M with 35 "Pass" or "Fail"

Algorithm
1. Get the mark, M
2. If M is 35 or more, the result is Pass
3. Otherwise, the result is Fail
4. Show the result
Pseudocode

BEGIN
READ mark as M
IF M >= 35 THEN
DISPLAY "Pass"
ELSE
DISPLAY "Fail"
ENDIF
END.

Pascal code

program passFail(input,output);
var M : integer;
begin
writeln('Enter Mark');
read(M);
if M >= 35 then
writeln('Pass')
else
writeln('Fail');
end.

Sample run
Input: M = 42 → Output: Pass

WORKED EXAMPLE 2: Sum and average of numbers entered by the user

Page 21 of 27
ICT · Grade 11 · Programming — Lesson Notes

The problem
A user will enter exactly 5 numbers, one at a time. The program should calculate and display their total and
their average.
Input · Process · Output

Input Process Output

5 numbers, one at a time Add each number to a running Total; Total and Average
after all 5, divide by 5

Algorithm
1. Set Total to 0 and count to 1
2. While count is 5 or less: read a Number, add it to Total, add 1 to count
3. Once the loop ends, calculate Average = Total / 5
4. Show Total and Average
Pseudocode

BEGIN
Total = 0
count = 1
WHILE count <= 5
READ Number
Total = Total + Number
count = count + 1
ENDWHILE
Average = Total / 5
DISPLAY Total, Average
END.

Pascal code

program sumAvg(input,output);
var total, num, count : integer;
avg : real;
begin
total := 0;
for count := 1 to 5 do
begin
writeln('Enter a number');
read(num);
total := total + num;
end;
avg := total / 5;
writeln('Total = ', total);
writeln('Average = ', avg);
end.

Sample run
Input: 10, 20, 15, 25, 30 → Output: Total = 100, Average = 20.00

WORKED EXAMPLE 3: Highest mark from a class, using an array and a function

Page 22 of 27
ICT · Grade 11 · Programming — Lesson Notes

The problem
A class of 8 students sit an ICT test. Store all 8 marks in an array, then use a function to find and return the
highest mark.
Input · Process · Output

Input Process Output

8 marks, stored in an array Scan every element, keeping track of The highest mark in the class
the largest seen so far

Algorithm
1. Read 8 marks into an array called marks
2. Assume the first element is the largest so far
3. Compare every other element against the current largest; update it whenever a bigger mark is found
4. Once all elements are checked, the current largest is the answer
5. Show the highest mark
Pseudocode

BEGIN
FOR i = 0 TO 7
READ marks[i]
ENDFOR
max = marks[0]
FOR i = 1 TO 7
IF marks[i] > max THEN
max = marks[i]
ENDIF
ENDFOR
DISPLAY max
END.

Pascal code (using a function)

program highestMark(input,output);
var marks : array[0..7] of integer;
i : integer;

function findMax(var m: array[0..7] of integer): integer;


var j, big : integer;
begin
big := m[0];
for j := 1 to 7 do
if m[j] > big then big := m[j];
findMax := big;
end;

begin
for i := 0 to 7 do
begin
writeln('Enter mark ', i+1);
read(marks[i]);
end;

Page 23 of 27
ICT · Grade 11 · Programming — Lesson Notes

writeln('Highest mark = ', findMax(marks));


end.

Sample run
Input: 65, 78, 92, 54, 88, 71, 60, 83 → Output: Highest mark = 92

Page 24 of 27
ICT · Grade 11 · Programming — Lesson Notes

Quick Revision — Key Terms


A one-page summary to help you remember the core vocabulary of this chapter before a test.

Term In one line

Input / Process / Output The raw material, the ordered steps, and the result of
solving a problem

Algorithm A step-by-step procedure for solving a problem,


independent of any language

Sequence Steps carried out one after another in a fixed order

Selection A condition chooses one of two possible paths (IF … THEN …


ELSE)

Repetition One or more steps repeat until a condition is satisfied

Flowchart A diagram that represents an algorithm using standard


shapes

Pseudocode An algorithm written in plain English, independent of a real


language

Identifier A name given to a variable, constant or program

Variable An identifier whose value can change while the program


runs

Constant An identifier whose value stays fixed throughout the


program

DIV / MOD Whole-number division / the remainder left after division

Boolean A data type that only holds True or False

Array A single variable name that stores many values of the same
type

Procedure A sub-program that does NOT return a value

Function A sub-program that DOES return a value to the caller

Study tip: Cover the right-hand column with a sheet of paper and try to explain each term in your own words — if
you can, you know it.

Page 25 of 27
ICT · Grade 11 · Programming — Lesson Notes

Answers to Selected Practice Questions


Short, factual questions are answered below so you can check your own work. Open-ended tasks (drawing a
flowchart, writing your own example) are best checked with your teacher.

Practice 1.1
– Input: 100 toffees & 20 people. Process: repeatedly give 5 toffees to each person until none remain. Output:
each person holding 5 toffees.
– 3rd formula, (l + w) × 2, is simplest — the fewest operations.

Practice 1.2
– Repetition — the same question is repeated for every student in the class.

Practice 1.3 (20 questions)


Questions that ask you to draw a flowchart, or to write your own original example, are best checked with your
teacher. Reference pseudocode is given below for the more advanced Level 4 problems, so you can check your
logic.
– Q7 (Pass/Fail): BEGIN → READ mark → IF mark >= 40 THEN DISPLAY "Pass" ELSE DISPLAY "Fail" → ENDIF →
END.
– Q11 (Total & average of 20 numbers): same pattern as the worked pseudocode on page 8, but WHILE n <= 20
and Average = Total / 20.

Q16 — Factorial of N

BEGIN
READ N
fact = 1
count = 1
WHILE count <= N
fact = fact * count
count = count + 1
ENDWHILE
DISPLAY fact
END.

Q18 — First N terms of the Fibonacci sequence

BEGIN
READ N
first = 0
second = 1
count = 1
WHILE count <= N
DISPLAY first
next = first + second
first = second
second = next
count = count + 1
ENDWHILE
END.

Page 26 of 27
ICT · Grade 11 · Programming — Lesson Notes

Q20 — Sort three numbers ascending (no array)

BEGIN
READ a, b, c
IF a > b THEN swap(a, b)
IF b > c THEN swap(b, c)
IF a > b THEN swap(a, b)
DISPLAY a, b, c
END.

Tip: For Q17 (prime check) and Q19 (ATM with PIN attempts), sketch the flowchart yourself using the decision-
diamond pattern from section 1.3, then compare with a classmate or your teacher.

Practice 1.4 (a)


– Marks2 — valid. 2ndMark — invalid (starts with a digit). Student Age — invalid (space). total_sum — valid.
– (a) integer (b) real (c) boolean (d) char
– 5 + 14 MOD 4 = 5 + 2 = 7
– NOT(8 MOD 2 > 5) = NOT(0 > 5) = NOT(False) = True
– (160 > 150) AND (16 < 15) = True AND False = False

Practice 1.4 (b)


– 58 falls in 50..64, so Grade = 'C'.
– (10 > 5) AND (3 > 8) = True AND False = False, so it prints 'No'.

Practice 1.4 (c)


– for i := 1 to 8 do runs 8 times (i = 1,2,...,8).
– count starts at 0, adds 5 each time: 5,10,15,20 — loop stops once count ≥ 20, so final value is 20, after 4 runs.

Practice 1.4 (d)


– var ages : array[0..24] of integer;
– A procedure that only prints a circumference and returns nothing is a Procedure.

Page 27 of 27

You might also like