Sie sind auf Seite 1von 45

2.

1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR


2.1.2 PSEUDOCODE AND FLOWCHARTS

Algorithm
An algorithm is a precise, step-by-step set of instructions for solving a task. An algorithm does not
solve a task; it gives you a series of steps that, if executed correctly, will result in a solution to a task.

Algorithm is written in two ways:


 Pseudocode
 Flowchart

R
 Pseudocode: Pseudocode is a simple way of writing programming code in English. Pseudocode
is not actual programming language. It uses short phrases to write code for programs before
you actually create it in a specific language.

KU
 Flowchart: A flowchart shows diagrammatically the steps required for a task (sub-system) and
the order that they are to be performed. These steps together with the order are called an
ALGORITHM. Flowcharts are an effective way to communicate the algorithm that shows how a
system or sub-system works.

A
OR

A block diagrammatic representation of a solution or flow of program is called a flowchart.


TH
 Understand and use pseudocode for assignment, using ←

 is used as an assignment symbol in pseudocodes.

Examples of pseudocode assignments:

ASSIGNMENT OF VALUE IN VARIABLE DESCRIPTION


Cost ← 10 Cost has the value 10
ED

Price ← Cost * 2 Price has the value 20


Tax ← Price * 0.12 Tax has the value 2.4
SellingPrice ← Price + Tax SellingPrice has the value 22.4
Gender ←"M" Gender has the value M
Chosen ← False Chosen has the value False
HM

Phrases/words used in Pseudocode:

WORD/PHRASE DESCRIPTION
SET To set/store values/variables – initialization/declaration
READ/INPUT To input/get data from user/keyboard
PRINT/OUPUT To output/return data/value on screen
END To show the end of algorithm
IF … THEN … ELSE … ENDIF Selection – to check some condition
CASE … OF … OTHERWISE … Selection – to match multiple values/variables/conditions
A

ENDCASE
FOR….TO….NEXT Loop – (Unconditional) to repeat some task(s) a specific
number of times.
WHILE…DO…ENDWHILE Loop – (Conditional) to repeat some task(s) till a condition
remains TRUE
REPEAT…UNTIL Loop – (Conditional) to repeat some task(s) till a condition
remains FALSE

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 1
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

 Understand and use pseudocode, using the following conditional statements:


IF … THEN … ELSE … ENDIF
CASE … OF … OTHERWISE … ENDCASE

Selection is the second technique of Algorithm.

Selection is written using two structures:

R
 IF … THEN … ELSE … ENDIF
 CASE … OF … OTHERWISE … ENDCASE

KU
IF … THEN … ELSE … ENDIF
It is used to check only one condition and act accordingly.

Syntax
IF <condition> THEN
<statement if condition is TRUE>
ELSE

A
<statement if condition is FALSE>
ENDIF

Example: Algorithm to check whether the input value is greater than100 and output the message
accordingly.
TH
SET number to 0
INPUT number
IF number > 100 THEN
OUTPUT “The entered number is greater than 100”
ELSE
OUTPUT “The entered number is smaller than 100”
ENDIF
ED

END

Visual Basic 2010 (Console Mode) Program:


HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 2
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Result of Program:

R
KU
IF…THEN…ELSEIF…ELSE…ENDIF
This is used to check multiple conditions.

 Each ELSEIF works like IF, but must be used within the main structure of IF…ENDIF.
 There is no limit of ELSEIFs
 If any of the ELSEIF’s condition is met, the rest of the ELSEIFs are not checked.

A
Syntax
IF <condition> THEN
<statement if condition is TRUE>
ELSEIF <condition> THEN
TH
<statement if condition is TRUE>
ELSEIF <condition> THEN
<statement if condition is TRUE>
ELSEIF <condition> THEN
<statement if condition is TRUE>
ELSEIF <condition> THEN
<statement if condition is TRUE>
ED

.
.
.
ELSE
<statement if condition is FALSE>
HM

ENDIF

Example: Algorithm to generate a marks sheet and check the grading scheme based on the
following criteria:

Grades Percentage range


A+ 90 +
A 80 – 89.99
B 70 – 79.99
C 60 – 69.99
A

D 50 – 59.99
U < 50 (less than 50)

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 3
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Task: Write down an algorithm to generate a marks sheet.


Input 5 subjects marks (maximum mark for each subject is 75)
Output the Total, Percentage and Grade.

Grading Scheme is as follows:


A+ 90+

R
A 80 – 89.99
B 70 – 79.99
C 60 – 69.99

KU
D 50 – 59.99
U less than 50

Note: You must use meaningful variable names

SET eng, comp, isl, math, phy, chem, tot, per to 0


SET gr to “ ”

A
INPUT eng, comp, isl, math, phy, chem
Tot = eng + comp + isl + math + phy + chem
Per = (Tot/375)* 100

IF per >= 90THEN


TH
Gr = “A+”
ELSEIF per >= 80 AND per < 90 THEN
Gr = “A”
ELSEIF per >= 70 AND per < 80 THEN
Gr = “B”
ELSEIF per >= 60 AND per < 70 THEN
Gr = “C”
ED

ELSEIF per >= 50 AND per < 60 THEN


Gr = “D”
ELSE
Gr = “U”
ENDIF
HM

OUTPUT tot, per, gr


END
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 4
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Visual Basic 2010 (Console Mode) Program:

R
KU
A
TH
ED
HM

Result of Program:
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 5
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

CASE … OF … OTHERWISE … ENDCASE


CASE is used to match multiple options in one condition.

Syntax
CASE <identifier (variable) OF
<constant>,<constant>,<constant> : <statement>
<constant>,<constant>,<constant> : <statement>

R
<constant>,<constant>,<constant> : <statement>
<constant>,<constant>,<constant> : <statement>
OTHERWISE

KU
<constant> : <statement if identifier/variable is not matched>
ENDCASE

EXAMPLE (NOV 2003 – Q11): The following algorithm inputs air speeds (which must be in multiples of
100) and outputs a suitable message.

A
1. input a speed
2. whole = speed/100
3. case whole of
4. 0,1,2 : result = slow
5. 3, 4, 5, 6 : result = normal
TH
6. 7, 8, 9 : result = high
7. otherwise whole = -1
8. endcase
9. if whole = -1 then
10. output “abnormal reading”
11. else output result, “speed”
ED

(a) Dry run the above algorithm for the following Input data and complete the Output column in

INPUT OUTPUT

150 abnormal reading


HM

400 normal speed

800 high speed

(b) State what would happen if line 2 had been missed out of the algorithm.
 Variable whole would not be defined,
 algorithm would fail/crash, etc.
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 6
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Visual Basic 2010 (Console Mode) Program:

R
KU
A
TH
Result of Program:
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 7
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Example 2: The following example uses a Select Case construction to write a line corresponding to
the value of the variable number.

Visual Basic 2010 (Console Mode) Program:

R
KU
A
TH
Result of Program
ED

More on: http://checktechno.blogspot.com/2012/11/how-to-use-switch-case-in-visual-basic.html


HM

 Understand and use pseudocode, using the following loop structures:


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

Loop: Repetition of task is called loop/iteration.

There are two basic types of loops:


A

 Unconditional Loop (FOR – TO – NEXT)


 Conditional Loop (WHILE – DO – ENDWHILE) and (REPEAT – UNTIL)

Unconditional Loop: Unconditional loop repeats the tasks unconditionally the specified number of
times.

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 8
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

FOR … TO … NEXT

Syntax
FOR <identifier(variable> = <number> TO <number>
<statement>
<statement>
<statement>

R
NEXT

Example:

KU
Algorithm to output name 10 times. Algorithm to output numbers from 1- 10.
SET a to 0 SET a to 0
FOR a = 1 TO 10 FOR a = 1 TO 10
OUTPUT “Ahmed Thakur” OUTPUT a
NEXT NEXT
END END

A
Program TH
ED

Result
HM
A

Example 2: Algorithm to output sum of first 10 natural numbers.


SET num, sum to 0
FOR num = 1 To 10
sum = sum + num
NEXT
OUTPUT sum
END

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 9
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Example 3: (November 2010, Paper 12 – Q9)

Task: The following algorithm inputs 20 numbers and outputs how many numbers were positive
(> 0) and how many numbers were negative (< 0).

Algorithm (Corrected):
1 negative = 0

R
2 positive = 0
3 FOR count = 1 TO 20
4 input number

KU
5 if number < 0 then negative = negative + 1
6 if number > 0 then positive = positive + 1
8 NEXT count
9 print negative, positive

A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 10
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

WHILE … DO … ENDWHILE
 It is a conditional loop.
 Loop continues till the condition remains TRUE.
 Condition is check first and the process comes next to it.

Syntax
WHILE <condition> DO

R
<statement>
<statement>
<statement>

KU
<identifier> = <identifier> + <number>
ENDWHILE

Example:
Algorithm to output name 10 times. Algorithm to output numbers from 1- 10.

A
SET a to 0 SET a to 0
WHILE a < 10 DO WHILE a < 10 DO
a = a + 1 a = a + 1
Output “Ahmed Thakur” OUTPUT a
ENDWHILE
TH ENDWHILE
END END

Program
ED
HM

Result
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 11
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Example: June 2015, Paper 22 – Q2

Program code that inputs 30 positive numbers and then output the largest number input.

Algorithm (Corrected):
1 Large = 0
2 Counter = 0

R
3 WHILE Counter < 30
4 DO
5 INPUT Num

KU
6 IF Num > Large THEN Large = Num
7 Counter = Counter + 1
8 ENDWHILE
9 PRINT Large

Exercise:

A
June 2015, Paper 22 – Q2
June 2005 – Q13 (b)
November 2002 – Q13
November 2006 – Q9
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 12
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

REPEAT… UNTIL
 It is a conditional loop.
 Loop continues till the condition remains FALSE.
 The process comes first, condition is checked next.

Syntax
REPEAT

R
<statement>
<statement>
<statement>

KU
<identifier> = <identifier> + <number>
UNTIL <condition>

Example:
Algorithm to output name 10 times. Algorithm to output numbers from 1- 10.
SET a to 0 SET a to 0

A
REPAT REPAT
a = a + 1 a = a + 1
Output “Ahmed Thakur” Output a
UNTIL a = 10 UNTIL a = 10
END
TH END

Program
ED
HM

Result
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 13
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Example: June 2013, Paper 13 – Q10


(a) The following pseudocode was written to input 1000 dates.

Algorithm
1 count = 1
2 repeat
3 input day, month, year

R
4 count = count + 1
5 until count = 1000

KU
(i) Describe why the loop only inputs 999 dates instead of 1000.
 value of count starts at 1 so only 999 iterations
 value of count reaches 1000, but before 1000th input

(ii) What needs to be changed or added to the above code to make sure 1000 dates are input?
 line 1 should read count = 0

A
 line 5 should read count = 1001 (or count >1000)
 change to appropriate loop structure

Corrected Algorithm
1 count = 0
2 repeat
TH
3 input day, month, year
4 count = count + 1
5 until count = 1000

Exercise:
June 2011, Paper 12 – Q7
June 2015, Paper 21 – Q2
ED

June 2005 – Q13 (a)


2015 Specimen Paper 2 – Q2

 Understand and use pseudocode, using the following commands and statements:
INPUT and OUTPUT (e.g. READ and PRINT)
HM

totalling (e.g. Sum ← Sum + Number)


counting (e.g. Count ← Count + 1)

INPUT and OUTPUT (e.g. READ and PRINT)

INPUT/READ
This command is used in pseudocode to accept/collect/get data value from user/keyboard entry.

Algorithm: To take two numbers from user and output the sum.
SET num1, num2, tot to 0
A

INPUT num1
INPUT num2
tot = num1 + num2
OUTPUT tot
END

OUTPUT/PRINT
This command is used in pseudocode to output/display any value or string on screen.

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 14
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Algorithm: To change the magnitude (sign) of a user entered number and output it.
SET num,chng_mag to 0
INPUT num
Chng_mag = num x -1
OUTPUT chng_mag
END

R
Further techniques used in Algorithm:
 Counting
 Totaling

KU
 Highest(Greatest) / Lowest(Smallest)

COUNTING
Counting in 1s is quite simple; use of the statement count = count + 1 will enable counting to
be done (e.g. in controlling a repeat loop). The statement literally means: the (new) count =
the (old) count + 1

A
It is possible to count in any increments just by altering the numerical value in the statement (e.g.
count = count – 1 counts backwards)

Exercise:
TH
June 2014, Paper 32 – Q1 (i)
June 2013, Paper 12 – Q17
June 2012, Paper 12 – Q15
November 2011, Paper 13 – Q16
June 2011, Paper 12 – Q17
November 2010, Paper 13 – Q17 (b)
November 2010, Paper 12 – Q9
June 2009 – Q18
ED

June 2005 – Q17


June 2003 – Q17
June 2002 – Q16

TOTALING
HM

To add up a series numbers the following type of statement should be used:


total = total + number
This literally means (new) total = (old) total + value of number

Example: Algorithm to count total number of positive and negative entries in 100 inputs.

Algorithm:
SET num, pos, neg to 0
FOR a = 1 TO 100
A

INPUT num
IF num >= 0 THEN
pos = pos + 1
ELSE
neg = neg + 1
ENDIF
OUPUT pos, neg
END

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 15
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

HIGHEST / LOWEST
This technique is used to get the highest or lowest value of many entered values.
The variable for highest value must be initialized with 0 (expected smallest value)
The variable for lowest value must be initialized with 1000 (expected highest value)

Example: November 2011, Paper 13 – Q16

R
The weather conditions in a town are being monitored over a year (365 days). The values recorded
per day are weather type and temperature (e.g. CLOUDY, 25).

KU
Write an algorithm, using pseudocode or flowchart only, which:
 inputs the weather type and temperature for each day
 outputs the number of days that were CLOUDY, RAINING, SUNNY or FOGGY
 outputs the highest recorded temperature for the year
 outputs the lowest recorded temperature for the year
Solution:
SET weathertype, to “ ”

A
SET temp, avg, hightest, cloud, rain, sun, fog to 0
SET lowest to 1000

FOR day = 1 to 365


Input weathertype
TH
Input temp

IF weathertype = “CLOUDY” THEN


cloudyday = cloudyday + 1
ELSEIF weathertype = “RAINY” THEN Checking and counting of days
Rainyday= rainyday + 1
ELSEIF weathertype = “SUNNY” THEN
ED

sunnyday = sunnyday + 1
ELSEIF weathertype = “FOGGY” THEN
foggyday = foggyday + 1
ENDIF
HM

IF temp > highest THEN


highest = temp
ELSEIF temp < lowest THEN Checking and collecting
lowest = temp highest and lowest values
ENDIF

Sum = sum + temp


NEXT
A

OUTPUT cloudyday, rainyday, sunnyday, foggyday


OUTPUT highest
OUTPUT lowest

END

Exercise:
November 2011, Paper 13 – Q16
Specimen Paper 1, 2011 – Q19

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 16
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

November 2010, Paper 13 – Q17(a)


November 2009 – Q17(b)
November 2007 – Q16(b)
November 2005 – Q17

PSEUDOCODES PROBLEMS

R
PROBLEMS
Questions 1 to 3 contain sections of pseudocode which contain errors. Locate the errors and suggest
the correct coding. Questions 4 to 10 are problems which require an algorithm to be written in

KU
pseudocode – there is “no right answer” here; as long as the pseudocode works then the solution is
acceptable.

(1) The following section of pseudocode inputs 1000 numbers and then outputs how many were
negative, how many were positive and how many were zero.

Locate the 3 errors and suggest a corrected piece of code.

A
1 negative = 1: positive = 1
2 for x = 0 to 1000
3 input number
4 if number < 0 then negative = negative + 1
TH
5 if number > 0 then positive = positive + 1
6 endif
7 endif
8 next
9 print negative, positive

(2) The following section of pseudocode inputs rainfall (in cm) for 500 days and outputs the average
rainfall and the highest rainfall input.
ED

Locate the 3 errors and suggest a corrected piece of code.


1 highest = 1000
2 days = 1
3 while days > 0
4 input rainfall
HM

5 if rainfall > highest then highest = rainfall


6 endif
7 total = total + rainfall
8 days = days + 1
9 average = total/500
10 endwhile
11 print average, highest

(3) The following section of pseudocode inputs a number, n, multiplies together 1 x 2 x 3 x ……. x n,
calculates input number/sum and outputs result of the calculation.
A

Locate the 3 errors and suggest a corrected piece of code.


1 input n
2 for mult = 1 to n
3 sum = 0
4 sum = sum * mult
5 result = n/sum
6 next
7 print result

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 17
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

(4) Regis lives in Brazil and often travels to USA, Europe and Japan. He wants to be able to convert
Brazilian Reais into US dollars, European euros and Japanese yen. The conversion formula is:

currency value = number of Reais X conversion rate

For example, if Regis is going to USA and wants to take 1000 Reais (and the exchange rate is
0.48) then he would input USA, 1000 and 0.48 and the output would be: 480 US dollars.

R
Write an algorithm, using pseudocode, which inputs the country he is visiting, the exchange rate
and the amount in Brazilian Reais he is taking. The output will be value in foreign currency and

KU
the name of the currency.

(5) As part of an experiment, a school measured the heights (in metres) of all its 500 students.

Write an algorithm, using pseudocode, which inputs the heights of all 500 students and outputs
the height of the tallest person and the shortest person in the school.

A
(6) A geography class decide to measure daily temperatures and hours of sunshine per day over a
12 month period (365 days)

Write an algorithm, using pseudocode, which inputs the temperatures and hours of sunshine for
TH
all 365 days, and finally outputs the average (mean) temperature for the year and the average
(mean) number of hours per day over the year.

(7) A small shop sells 280 different items. Each item is identified by a 3 – digit code. All items that start
with a zero (0) are cards, all items that start with a one (1) are sweets, all items that start with a
two (2) are stationery and all items that start with a three (3) are toys.

Write an algorithm, using pseudocode, which inputs the 3 – digit code for all 280 items and
ED

outputs the number of cards, sweets, stationery and toys.

(8) A company are carrying out a survey by observing traffic at a road junction. Each time a car,
bus, lorry or other vehicle passed by the road junction it was noted down.

10 000 vehicles were counted during the survey.


HM

Write an algorithm, using pseudocode, which:


 inputs all 10000 responses
 outputs the number of cars, buses and lorries that passed by the junction during the survey
 outputs the number of vehicles that weren’t cars, buses or lorries during the survey

(9) Speed cameras read the time a vehicle passes a point (A) on the road and then reads the time
it passes a second point (B) on the same road (points A and B are 100 metres apart). The speed
of the vehicle is calculated using:
A

The maximum allowed speed is 100 kilometres per hour. 500 vehicles were monitored using these
cameras over a 1 hour period.

Write an algorithm, using pseudocode, which:

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 18
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

 inputs the start time and end time for the 500 vehicles that were monitored
 calculate the speed for each vehicle using the formula above
 outputs the speed for each vehicle and also a message if the speed exceeded 100 km/hour
 output the highest speed of all the 500 vehicles monitored

(10) There are ten stations on a railway line:


1 ------ 2 ------ 3 ------ 4 ------ 5 ------ 6 ------ 7 ------ 8 ------ 9 ------ 10

R
The train travels in both directions (i.e. from 1 to 10 and then from 10 to 1). The fare between
each station is $2.

KU
A passenger inputs the number of the station at the start of his journey and the number of the
destination station and the fare is calculated (e.g if a passenger gets on a station 3 and his
destination is station 9 his fare will be $12). The calculation must take into account the direction
of the train (e.g. a passenger getting on at station 7 and getting off at station 1 will also pay $12
and not a negative value!!).

A
A discount of 10% is given if 3 or more passengers are travelling together.

Write an algorithm, using pseudocode, which:


 inputs the number of passengers travelling
TH
 inputs the station number of the starting point and the station number of the destination
 calculates the total fare taking into account the direction of travel
 calculates any discount due
 outputs the cost of the tickets and prints the tickets

SOLUTIONS TO PSEUDOCODE PROBLEMS

1. Three errors:
ED

Line error correction


1 totals should be zero negative = 0: positive = 0
2 loops 1001 times for x = 1 to 1000
6 no checks for zeros if number = 0 then zero = zero + 1
Input (OR else zero = zero + 1)
HM

(lines 6 to 9 need to be re-numbered 7 to 10; also need to add the following:


zero = 0 in line 1 and add zero to output list at the end of the algorithm)

2. Three errors:

Line error correction


1 wrongly set value highest = 0
3 while loops never stops while days <= 5000 do
9 average needs to be line 9: endwhile
A

outside while loop line 10: average = total/500

3. Three errors:

Line error correction


3 sum = 0 inside loop should be set outside loop before line 1
3 sum = 0 initial value this value should be sum = 1
5 calculation result = n/sum should come between lines 6 and 7
4.
1 input country

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 19
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

2 input conv_rate
3 input no_reais
4 currency_value = no_reais * conv_rate
5 if country = USA then print “$”, currency_value
6 if country = Europe then print “€”, currency_value
7 if country = Japan then print “¥”, currency_value

5.

R
1 tallest = 0
2 shortest = 500
3 for count = 1 to 500 do

KU
4 input height
5 if height > tallest then tallest = height
6 if height < smallest then smallest = height
7 next
8 print tallest, shortest

A
6.
1 sum_temp = 0: sum_hours = 0
2 for count = 1 to 365 do
3 input temp, hours
4 sum_temp = sum_temp + temp
5 sum_hours = sum_hours + hours
TH
6 next
7 mean_temp = sum_temp/365
8 mean_hours = sum_hours/365
9 print mean_temp, mean_hours

7.
1 cards = 0: stationery = 0: toys = 0
ED

2 for count = 1 to 280 do


3 input code
4 if code < 100 then cards = cards + 1
5 if code > 99 and code < 200 then stationery = stationery + 1
6 if code > 199 and code < 300 then toys = toys + 1
7 else print “error”
HM

8 next
9 print cards, stationery, toys

8.
1 cars = 0: buses = 0: lorry = 0: others = 0
2 for count = 1 to 10000 do
3 input vehicle
4 if vehicle = car then cars = cars + 1
5 if vehicle = bus then buses = buses + 1
6 if vehicle = lorry then lorry = lorry + 1
A

7 else others = others + 1


8 next
9 print cars, buses, lorry, others

9.
1 highest = 0
2 for count = 1 to 500 do
3 input start_time, end_time
4 speed = 100/(end_time – start_time) {NOTE: m/second}
5 speed = speed * 3.6 {NOTE: conversion to km/hour}

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 20
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

6 if speed <= 100 then print “speed is OK”


7 else print “maximum speed exceeded”
8 print speed
9 if speed > highest then highest = speed
10 next
11 print highest

10.

R
1 input start
2 input end
3 if end > start then no_stations = end – start

KU
4 else no_stations = start – end
5 cost = no_stations * 2
6 input no_passengers
7 if no_passengers > 2 then cost = 0.9 * cost
8 final_cost = cost * no_passengers
9 print final_cost

A
10 print tickets

[NOTE: it is possible to use other loop structures other than for …. to; the algorithms would work equally
as well with repeat …. until or while …. endwhile. The for …. to loops work particularly well when an
exact count is known (e.g. exactly 100 temperatures). If we had to input temperatures until they
TH
became negative it would be best to use a while or repeat loop, for example].
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 21
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

 Understand and use standard flowchart symbols to represent the above statements, commands
and structures

Flowchart
A graphic map of the path of control or data through the operations in a program or an information-
handling system. Symbols such as squares, diamonds, and ovals represent various operations. These

R
symbols are connected by lines and arrows to indicate the flow of data or control from one point to
another.

KU
OR

The block diagrammatic representation of the flow of program is known as Flowchart.

Methods of Solution for Flowcharts: Flowchart Symbols


SHAPE NAME SYMBOL PURPOSE

A
Oval Terminal Interrupt TH Start/Stop of Flowchart

Parallelogram Input/output Box For Input and Output

For Variable initialization


Rectangle Process Box
Calculation
ED

For Selection
Rhombus Decision Box
IF condition
HM

Lines with arrow Flowlines Direction of flow


A

On-page To continue the flowchart


Small Circle
Connector on same page

Off-page To continue the flowchart


connector on another page

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 22
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Introduction to Flowcharts
This section covers the use of flow diagrams (charts) in the production of algorithms. Systems
flowcharts are different and these are covered in a different section (Systems analysis).

The early part of section 3.1 (i.e. top down design, structure diagrams, menus, libraries of procedures
and subroutines) is covered adequately by standard text books.

R
This section primarily covers four areas:
1. Common flow chart symbols
2. Writing flowcharts to solve problems

KU
3. Dry running of flowcharts to determine its function and outputs
4. Exercises to test the above concepts

1. Common flow chart symbols

A
TH
ED
HM
A

2. Writing flowcharts to solve problems


The following five problems are also covered in section 3.2 where the algorithms are constructed
using pseudocode. Candidates may choose to answer questions using either flowcharts or
pseudocode but a working knowledge of both techniques is well advised.

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 23
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Example 1
A town contains 5000 houses. Each house owner must pay tax based on the value of the house.
Houses over $200 000 pay 2% of their value in tax, houses over $100 000 pay 1.5% of their value in tax
and houses over $50 000 pay 1% of their value in tax. All others pay no tax. Write an algorithm to
solve this problem in the form of a flowchart.

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 24
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Example 2
The following formula is used to calculate n: n = (x * x)/(1 – x). The value x = 0 is used to stop the
algorithm. The calculation is repeated using values of x until the value x = 0 is input. There is also a
need to check for error conditions. The values of n and x should be output. Write an algorithm to
show this repeated calculation in the form of a flowchart.

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 25
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Example 3
Write an algorithm in the form of a flowchart which takes temperatures input over a 100 day period
(once per day) and outputs the number of days when the temperature was below 20 °C and the
number of days when the temperature was 20 °C and above.

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 26
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Example 4
Write an algorithm in the form of a flowchart which:
 inputs the top speeds (in km/hr) of 5000 cars
 outputs the fastest speed and the slowest speed
 outputs the average (mean) speed of all the 5000 cars

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 27
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Example 5
A shop sells books, maps and magazines. Each item is identified by a unique 4 – digit code. All books
have a code starting with 1, all maps have a code starting with 2 and all magazines have a code
starting with 3. The code 9999 is used to end the algorithm.

Write an algorithm in the form of a flowchart which inputs the codes for all items in stock and outputs
the number of books, number of maps and the number of magazines in stock. Include any validation
checks needed.

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 28
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

DRY RUNNING OF FLOWCHARTS


Dry running of flowcharts is basically a technique to:
 determine the output for a known set of data to check it carries out the task correctly
 check on the logic of the algorithm
 determine the function of the algorithm

R
When dry running a flowchart it is advisable to draw up a trace table showing how variables change
their values at each stage in the algorithm. The advantages of doing this are:

KU
 if you make a mistake, it is easier to back track to where the error occurred rather than starting
from the beginning again
 there is less chance of an error being made
 encourages a more logical approach

The following three examples show all stages in the dry running for the given set of input data:

A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 29
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Example 1
This algorithm inputs 3 numbers, each number goes through successive division by 10 until its value is
less than 1. An output is produced which contains the number input and a value generated by the
flowchart processing.

Data to be used: X = 85, 3190, -40

R
KU
A
TH
ED
HM

X N T Output T, N
85 1 85
8.5 2
0.85 85, 2
3190 1 3190
319 2
31.9 3
3.19 4
A

0.319 3190, 4

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 30
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Example 2
This algorithm inputs 5 values and outputs how many input numbers were negative and how many
were positive.

Data to be used: N = 1, -5, 2, -8, -7

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 31
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

Example 3
This algorithm inputs the number of hours of sunshine recorded each day for a week (7 days). The
output is the highest value for hours of sunshine and the average (mean) value for the numbers of
hours of sunshine per day.

Data to be used: hours = 9.0, 7.8, 1.2, 4.5, 10.0, 6.4, 3.1

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 32
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

FLOWCHARTS PROBLEMS

Questions 1 to 7 are problems which require an algorithm to be written in the form of a flowchart.
Questions 8 to 10 require a trace table to be written and find the expected output for the given set
of data. The answers to these questions can be found in Section 6.

R
(1) Regis lives in Brazil and often travels to USA, Europe and Japan. He wants to be able to convert
Brazilian Reais into US dollars, European euros and Japanese yen. The conversion formula is:
currency value = number of Reais X conversion rate

KU
For example, if Regis is going to USA and wants to take 1000 Reais (and the exchange rate is 0.48)
then he would input USA, 1000 and 0.48 and the output would be: 480 US dollars.

Write an algorithm, using a flowchart, which inputs the country he is visiting, the exchange rate and
the amount in Brazilian Reais he is taking. The output will be value in foreign currency and the name
of the currency.

A
(2) As part of an experiment, a school measured the heights (in metres) of all its 500 students.

Write an algorithm, using a flowchart, which inputs the heights of all 500 students and outputs the
height of the tallest person and the shortest person in the school.
TH
(3) A geography class decide to measure daily temperatures and hours of sunshine per day over a
12 month period (365 days)

Write an algorithm, using a flowchart, which inputs the temperatures and hours of sunshine for all 365
days, and finally outputs the average (mean) temperature for the year and the average (mean)
number of hours per day over the year.
ED

(4) A small shop sells 280 different items. Each item is identified by a 3 – digit code. All items that start
with a zero (0) are cards, all items that start with a one (1) are sweets, all items that start with a
two (2) are stationery and all items that start with a three (3) are toys.

Write an algorithm, using a flowchart, which inputs the 3 – digit code for all 280 items and outputs
the number of cards, sweets, stationery and toys.
HM

(5) A company are carrying out a survey by observing traffic at a road junction. Each time a car,
bus or lorry passed by the road junction it was noted down.

10 000 vehicles were counted during the survey.

Write an algorithm, using an algorithm, which:


 inputs all 10000 responses
 outputs the number of cars, buses and lorries that passed by the junction during the survey
 outputs the number of vehicles that weren’t cars, buses or lorries during the survey
A

(6) Speed cameras read the time a vehicle passes a point (A) on the road and then reads the time
it passes a second point (B) on the same road (points A and B are 100 metres apart). The speed
of the vehicle is calculated using:

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 33
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

The maximum allowed speed is 100 kilometres per hour. 500 vehicles were monitored using these
cameras over a 1 hour period.

Write an algorithm, using a flowchart, which:


 inputs the start time and end time for the 500 vehicles that were monitored
 calculate the speed for each vehicle using the formula above
 outputs the speed for each vehicle and also a message if the speed exceeded 100 km/hour

R
 output the highest speed of all the 500 vehicles monitored

(7) There are ten stations on a railway line:

KU
1 ------ 2 ------ 3 ------ 4 ------ 5 ------ 6 ------ 7 ------ 8 ------ 9 ------ 10

The train travels in both directions (i.e. from 1 to 10 and then from 10 to 1). The fare between
each station is $2.

A
A passenger inputs the number of the station at the start of his journey and the number of the
destination station and the fare is calculated (e.g if a passenger gets on a station 3 and his
destination is station 9 his fare will be $12). The calculation must take into account the direction
of the train (e.g. a passenger getting on at station 7 and getting off at station 1 will also pay $12
and not a negative value!!).
TH
A discount of 10% is given if 3 or more passengers are travelling together.

Write an algorithm, using a flowchart, which:


 inputs the number of passengers travelling
 inputs the station number of the starting point and the station number of the destination
 calculates the total fare taking into account the direction of travel
 calculates any discount due
ED

 outputs the cost of the tickets and prints the tickets


HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 34
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

(8) Draw the trace table and determine the output from the following flowchart using the following
data:

number = 45, -2, 20.5

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 35
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

(9) Draw the trace table and determine the output from the following flowchart using the following
data (NOTE: input of the word “end” stops the program and outputs results of the survey):

vehicle = car, car, lorry, bus, van, van, car, car, bus, car, end

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 36
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

(10) Draw the trace table and determine the output from the following flowchart using the following
data:

X = 5, -3, 0, -3, 7, 0, 6, -11, -7, 12

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 37
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

SOLUTIONS TO FLOWCHART PROBLEMS

1.

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 38
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

2.

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 39
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

3.

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 40
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

4.

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 41
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

5.

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 42
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

6.

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 43
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

7.

R
KU
A
TH
ED
HM
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 44
2210
2.1 ALGORITHM DESIGN AND PROBLEM-SOLVING AHMED THAKUR
2.1.2 PSEUDOCODE AND FLOWCHARTS

8. (NOTE: there is an error in the question in Section 3.1 – output box should read: OUTPUT X,T)

R
KU
A
9. (NOTE: there is an error in the question in Section 3.1 – the “others = others + 1” statement
should by-pass all the other counting procedures)
TH
ED
HM

10.
A

COMPUTER SCIENCE https://www.facebook.com/groups/OAComputers/


ahmed_thakur@hotmail.com, 0300-8268885 Page 45
2210

Das könnte Ihnen auch gefallen