0% found this document useful (0 votes)
51 views35 pages

CH-10 Pseudocode and Flowcharts-1

Uploaded by

aabrothers049
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)
51 views35 pages

CH-10 Pseudocode and Flowcharts-1

Uploaded by

aabrothers049
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
You are on page 1/ 35

5 The algorithm allows a number to be entered.

It then calculates and outputs the next number in


the mathematical series.

Fib 1
Prev2 0
Prev1 1
INPUT Number
IF Number = 0
THEN Fib 0
ENDIF
WHILE Number > 2
Fib Prev2 + Prev1
Prev2 Prev1
Prev1 Fib
Number Number - 1
ENDWHILE
OUTPUT Fib

(a) Complete the trace table for the input data: 7

Fib Prev2 Prev1 Number OUTPUT

[4]
(b) Complete the trace table for the input data: 2
Fib Prev2 Prev1 Number OUTPUT

[2]
2 (a) Write an algorithm, using pseudocode, to input three different numbers, multiply the two
larger numbers together and output the result. Use the variables: Number1, Number2 and
Number3 for your numbers and Answer for your result.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

.............................................................................................................................................. [5]

(b) Give two sets of test data to use with your algorithm in part (a) and explain why you chose
each set.

Test data set 1 ..........................................................................................................................

Reason .....................................................................................................................................

...................................................................................................................................................

Test data set 2 ..........................................................................................................................

Reason .....................................................................................................................................

...................................................................................................................................................
[4]
5 The flowchart checks the level of chlorine and the depth of water compared to the height of the
swimming pool. Error messages are output if a problem is found.

START

INPUT Height,
Depth, Chlorine

OK True

OUTPUT
Is Depth > Yes
"Water too
Height/2 ? deep"

No
OUTPUT
Is Depth < Yes "Water too
Height/3 ? shallow"

No
OK False

Yes OUTPUT "Too


Is Chlorine much chlorine
>3 ? add more water"
No

OUTPUT "Too
Yes little chlorine
Is Chlorine
<1 ? add more chlorine"
No

No
Is OK = True ?

Yes

OUTPUT "Pool OK
to use"

END
(a) Complete the trace tables for each set of input data.

Input data: 6, 2.5, 2

Height Depth Chlorine OK OUTPUT

Input data: 4, 3, 1.5

Height Depth Chlorine OK OUTPUT

Input data: 6, 3.5, 4


Height Depth Chlorine OK OUTPUT

[6]
(b) Identify a problem with the algorithm that the flowchart represents.

...................................................................................................................................................

.............................................................................................................................................. [1]
2 (a) Draw a flowchart for an algorithm to input numbers. Reject any numbers that are negative
and count how many numbers are positive. When the number zero is input, the process ends
and the count of positive numbers is output.

(b) Explain the changes you will make to your algorithm to also count the negative numbers.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[2]
3 This pseudocode algorithm inputs two non-zero numbers and a sign, and then performs the
calculation shown by the sign. An input of zero for the first number terminates the process.

INPUT Number1, Number2, Sign


WHILE Number1 <> 0
IF Sign = '+' THEN Answer Number1 + Number2 ENDIF
IF Sign = '-' THEN Answer Number1 - Number2 ENDIF
IF Sign = '*' THEN Answer Number1 * Number2 ENDIF
IF Sign = '/' THEN Answer Number1 / Number2 ENDIF
IF Sign <> '/' AND Sign <> '*' AND Sign <> '-' AND Sign <> '+'
THEN Answer 0
ENDIF
IF Answer <> 0 THEN OUTPUT Answer ENDIF
INPUT Number1, Number2, Sign
ENDWHILE

(a) Complete the trace table for the input data:


*
5, 7, +, 6, 2, –, 4, 3, , 7, 8, ?, 0, 0, /

Number1 Number2 Sign Answer OUTPUT

[3]

(b) Show how you could improve the algorithm written in pseudocode by writing an alternative
type of conditional statement in pseudocode.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[3]
2 (a) Write an algorithm to input 1000 numbers. Count how many numbers are positive and how
many numbers are zero. Then output the results. Use either pseudocode or a flowchart.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

.............................................................................................................................................. [6]

(b) Give one change you could make to your algorithm to ensure initial testing is more
manageable.

...................................................................................................................................................

.............................................................................................................................................. [1]
3 The global trade item number (GTIN-8) barcode has seven digits and a check digit.
This pseudocode algorithm inputs seven digits and calculates the eighth digit, then outputs the
GTIN-8.
DIV(X,Y), finds the number of divides in division for example DIV(23,10) is 2.
MOD(X,Y), finds the remainder in division for example MOD(23,10) is 3.

FOR Count 1 TO 7
INPUT Number
Digit(Count) Number
NEXT
Sum (Digit(1)+Digit(3)+Digit(5)+Digit(7))*3+Digit(2)+Digit(4)+Digit(6)
IF MOD(Sum,10) <> 0
THEN Digit(8) DIV(Sum,10)*10 + 10 - Sum
ELSE Digit(8) 0
ENDIF
OUTPUT "GTIN-8"
FOR Count 1 TO 8
OUTPUT Digit(Count)
NEXT

(a) Complete the trace table for the input data: 5, 7, 0, 1, 2, 3, 4

Digit(1) Digit(2) Digit(3) Digit(4) Digit(5) Digit(6) Digit(7) Digit(8) Sum OUTPUT

Complete the trace table for the input data: 4, 3, 1, 0, 2, 3, 1

Digit(1) Digit(2) Digit(3) Digit(4) Digit(5) Digit(6) Digit(7) Digit(8) Sum OUTPUT

[5]

(b) Explain how you would change the algorithm to input eight digits (seven digits and the check
digit) and output if the check digit entered is correct or not.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

.............................................................................................................................................. [3]
5 The flowchart below represents a program routine.

START

Flag 0
Count 1

Is Name[Count] Yes
> Name[Count + 1]? Temp Name[Count]

No Name[Count] Name[Count + 1]

Name[Count + 1] Temp

Flag 1

Count Count + 1

No Is Count
= 4?

Yes

No Is Flag
= 0?

Yes

END
(a) The array used in the flowchart contains the following data:

Name[1] Name[2] Name[3] Name[4]


Jamal Amir Eve Tara

Complete the trace table using the data given in the array.

Flag Count Name[1] Name[2] Name[3] Name[4] Temp


Jamal Amir Eve Tara

[5]

(b) Describe what the algorithm represented by the flowchart is doing.

...................................................................................................................................................

...............................................................................................................................................[2]
(b) Write an algorithm in pseudocode, using a single loop, to print 50 names that have been
stored in an array.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[3]
2 Write an algorithm using either pseudocode or a flowchart, to:

• input a positive integer


• use this value to set up how many other numbers are to be input
• input these numbers
• calculate and output the total and the average of these numbers.

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

......................................................................................................................................................[6]
5 (a) This flowchart checks a batch of 10 rice sacks for weight. Sacks should weigh 50 kilograms
each. Sacks weighing over 50.5 kilograms or less than 49.5 kilograms are rejected. The
number of sacks accepted and the number of sacks rejected is output.

START

Accept 0
Reject 0
Count 0

Is Count Yes
= 10?
OUTPUT
Accept Accept + 1 Accept,
No
Reject

Count Count + 1
END

INPUT
Sack

Is Sack Yes Reject Reject + 1


< 49.5?

No

Is Sack
No > 50.5? Yes
Complete the trace table for the input data:

50.4, 50.3, 49.1, 50.3, 50.0, 49.5, 50.2, 50.3, 50.5, 50.6

Accept Reject Count Sack OUTPUT

[5]

(b) The size of the batch has increased to 50 sacks. It has been decided to only reject sacks that
are underweight.

State the changes that need to be made to the flowchart.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[2]
2 (a) Write an algorithm to input three different numbers, and then output the largest number. Use
either pseudocode or a flowchart.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[4]

(b) Give two sets of test data to use with your algorithm in part (a) and explain why you chose
each set.

Test data set 1 ...........................................................................................................................

Reason ......................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

Test data set 2 ...........................................................................................................................

Reason ......................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................
[4]
3 This flowchart inputs the weight of items in kilograms to be loaded on a trailer. Any item over
25 kilograms is rejected. The trailer can take up to 100 kilograms.

START

TotalWeight 0
Reject 0

INPUT Weight

Yes
Is Weight
Reject Reject + 1
˃ 25?
No

TotalWeight TotalWeight + Weight

No Is
TotalWeight
˃ 100?
Yes

TotalWeight TotalWeight – Weight

OUTPUT 'Weight of items '’


TotalWeight, ' Number of
items rejected ',Reject

END
Complete the trace table for the input data:

13, 17, 26, 25, 5, 10, 15, 35, 20, 15

Weight Reject TotalWeight OUTPUT

[5]
6 This flowchart inputs a range of temperatures in degrees Fahrenheit.

As each temperature is input, it is compared with the previous highest temperature. If it is higher
than the current highest, it replaces the previous highest temperature and then it is converted to
degrees Celsius.

For ease of calculation, the final step of the Fahrenheit to Celsius conversion has been
approximated as division by 2.

When –1 is entered, the input process stops and the highest temperature (in both Fahrenheit and
Celsius) is output.

START

HighF -100
HighC -100

INPUT TempF

Is TempF No Is TempF No
= -1? > HighF?

Yes Yes

HighF TempF

HighC (TempF-32)/2

OUTPUT 'The highest temperature is, ',


HighF, ' Fahrenheit, ',
HighC, ' Celsius.'

END
Complete the trace table for the input data:
68, 46, 50, 86, 65, 50, 40, 30, –1

HighF HighC TempF OUTPUT

[5]
3 The flowchart below inputs the price of an item under $10. The change from a $10 note is output.
Any amount less than 5 cents is rounded up to 5 cents.

The predefined function INT rounds a number down to the nearest whole number; for example
Z ← INT(5.7) gives the value Z = 5

START

INPUT Price

Change 10 – Price

Is
Change Yes Dollars INT(Change)
>= 1?

No Change Change – INT(Change)

OUTPUT Dollars,
" dollars"

Is
Change Yes
Change Change – 0.5
>= 0.5?

No OUTPUT "One 50
cent coin"

TenCents INT(Change * 10 + 0.5)

OUTPUT TenCents,
" ten cent coins"

Is Change*10 – Yes OUTPUT "One 5


TenCents cent coin"
> 0?

No

END
Complete the trace table for the input data: 6.29

Price Change Dollars TenCents OUTPUT

[5]
3 The flowchart below inputs an integer. The predefined function DIV gives the value of the division,
for example Z ← 11 DIV 3 gives the value Z = 3. The predefined function MOD gives the value
of the remainder, for example Z ← 11 MOD 3 gives the value Z = 2.

67$57

,1387;

1R
,V;!
"
<HV

$ ;',9
% ;02' 287387;

287387% (1'

; $

Complete a trace table for each of the two input values 33 and 75.

Trace table for input value 33

X A B OUTPUT

Trace table for input value 75

X A B OUTPUT

[4]
3 The flowchart below calculates the number of tins of paint required to paint walls. The flowchart
inputs the height and width of a wall in metres, the number of doors and the number of windows.
A value of –1 for the height stops the input.

START

Area 0
Tins 0

INPUT Height,
Width, Doors,
Windows

Is Yes
Height Tins INT(Area/10 + 0.5)
= –1?

No

OUTPUT Tins
Area Area + Height * Width
– Doors * 1.5 – Windows

END

Complete the trace table for the input data:

3, 5, 1, 0, 3, 7, 0, 0, 3, 5, 0, 3, 3, 7, 1, 1, –1, 0, 0, 0

Area Tins Height Width Doors Windows

[4]
4 The flowchart below inputs the height of children who want to ride on a rollercoaster. Children
under 1.2 metres are rejected. The ride starts when eight children have been accepted.

67$57

5LGHUV
5HMHFW

,1387
+HLJKW

,V <HV
+HLJKW 5HMHFW5HMHFW
"

1R

5LGHUV5LGHUV

1R 5LGHUV <HV 287387


" 5HDG\WRJR 5HMHFW

(1'
Complete the trace table for the input data:

1.4, 1.3, 1.1, 1.3, 1.0, 1.5, 1.2, 1.3, 1.4, 1.3, 0.9, 1.5, 1.6, 1.0

Riders Reject Height OUTPUT

[4]
3 (a) This pseudocode inputs an integer. The predefined function DIV gives the value of the
division, e.g. Y 10 DIV 3 gives the value Y = 3. The predefined function MOD gives the
value of the remainder, e.g. Y 10 MOD 3 gives the value Y = 1.

INPUT X
WHILE X > 15
DO
T1 X DIV 16
T2 X MOD 16
CASE T2 OF
10:OUTPUT A
11:OUTPUT B
12:OUTPUT C
13:OUTPUT D
14:OUTPUT E
15:OUTPUT F
OTHERWISE OUTPUT T2
ENDCASE
X T1
ENDWHILE
CASE X OF
10:OUTPUT A
11:OUTPUT B
12:OUTPUT C
13:OUTPUT D
14:OUTPUT E
15:OUTPUT F
OTHERWISE OUTPUT X
ENDCASE
Complete a trace table for each of the two input values 37 and 191.

Trace table for input value 37 Trace table for input value 191
X T1 T2 OUTPUT X T1 T2 OUTPUT

[4]

(b) State the purpose of the pseudocode in part (a).

...................................................................................................................................................

...............................................................................................................................................[2]
3 (a) The flowchart inputs an integer. The predefined function DIV gives the integer result of the
division, e.g. Y 10 DIV 3 gives the value Y = 3. The predefined function MOD gives the
value of the remainder, e.g. Y 10 MOD 3 gives the value Y = 1.

START

Posn @ 1
New @ 0

INPUT X

T1 @ X DIV 2
T2 @ X MOD 2
New @New+T2 * Posn
Posn @Posn * 10

X @T1

Yes is T1
>= 2?

No

New @New+T1 * Posn

OUTPUT New

END
Complete a trace table for each of the two input values 5 and 12.

Trace table for input value 5

X Posn New T1 T2 OUTPUT

Trace table for input value 12

X Posn New T1 T2 OUTPUT

[6]

(b) State the purpose of the flowchart in part (a).

...............................................................................................................................................[1]
3 The flowchart below inputs the weight of a number of parcels in kilograms. Parcels weighing more
than 25 kilograms are rejected. A value of –1 stops the input.

The following information is output: the total weight of the parcels accepted and number of parcels
rejected.

START

7RWDOĸ
5HMHFWĸ

INPUT
Weight

Is Weight Yes OUTPUT Total,


= –1? Reject

No END

Is Weight Yes
5HMHFWĸ5HMHFW
> 25?

No

7RWDOĸ7RWDO:HLJKW
Complete the trace table for the input data:

1.8, 26.0, 7.0, 11.3, 10.0, 2.5, 25.2, 5.0, 19.8, 29.3, –1

Total Reject Weight OUTPUT

[5]
3 (a) The flowchart below inputs six single digit numbers. The predefined function MOD gives the
value of the remainder, for example, Y ← 10 MOD 3 gives the value Y = 1

START

INPUT
A, B, C, D, E, F

Total @ A*1 + B*2 + C*3 + D*4 + E*5

Check @ Total MOD 11

Is Check = Yes OUTPUT


F? 'Accept'

No

OUTPUT
'Reject'

END

Complete a trace table for each of the two sets of input data.
Trace table set 1 5, 2, 4, 3, 1, 5
A B C D E F Total Check Output

Trace table set 2 3, 2, 1, 0, 7, 3


A B C D E F Total Check Output

[4]
(b) State the purpose of the flowchart in part (a).

...................................................................................................................................................

...............................................................................................................................................[1]

(c) Identify a problem with this flowchart and explain how to correct it.

Problem ....................................................................................................................................

...................................................................................................................................................

Solution .....................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[3]
3 The flowchart inputs the size of a number of car engines; a value of –1 stops the input.
This information is output: average engine size and number of engines with size > 1.5

START

Engine ← 0
Count ← 0
Number ← 0

INPUT Size

is Size Yes OUTPUT


Average ← Engine/Number
= –1 ? Average, Count

No

STOP
is Size Yes
Count ← Count + 1
> 1.5 ?

No

Number ← Number + 1

Engine ← Engine + Size


Complete the trace table for the input data.

1.8, 2.0, 1.0, 1.3, 1.0, 2.5, 2.0, 1.3, 1.8, 1.3, –1

Engine Count Number Size Average OUTPUT

[6]
2 Jatinder uses Internet banking.
This pseudocode checks her PIN.

c ← 0
INPUT PIN
x ← PIN
REPEAT
x ← x/10
c ← c + 1
UNTIL x < 1
IF c <> 5
THEN
PRINT “error in PIN entered”
ELSE
PRINT “PIN OK”
ENDIF

(a) What value of c and what message would be output if the following PINs were entered?

5 1 0 2 0 Value of c:

Message:

5120 Value of c:

Message: [2]

(b) What type of validation check is being carried out here?

[1]
5 A floor turtle uses these instructions.

Instruction Meaning

FORWARD d Move d cm forward

BACKWARD d Move d cm backward

LEFT t Turn left t degrees

RIGHT t Turn right t degrees

REPEAT n Repeat the next set of instructions n times

ENDREPEAT End of REPEAT loop

PENUP Raise the pen

PENDOWN Lower the pen

(Each square in the drawing is 10 cm by 10 cm.)

Start Finish
Complete the set of instructions to draw the shape (shown in bold lines).

PENDOWN

LEFT 90

REPEAT

[5]

You might also like