Sei sulla pagina 1di 2

Pseudocode: Practice

1. Trace the following algorithmic fragment for N = 6. Show all working in a trace table.
SUM = 0
loop COUNT from 1 to (N div 2)
if N mod COUNT = 0 then
SUM = SUM + COUNT
end if
end loop
if SUM = N then
output "perfect"
else
output "not perfect"
end if

2.Write a pseudocode that reads all the Temperatures in Canada for the entire month from a collection
TEMP.
output the lowest temperature
average temperature.

3.Write a pseudocode that reads all the numbers from a collection NUMBERS.
Print the factors of each number
HL
An array IBDATA stores the highest points students achieved in their IB examination. Every year the highest
point of the student is entered. The array stores the values in descending order of the points scored by
students.
The points stored in IBDATA is shown below
45 45 41 38 37 37 36 36 35 35

Write a pseudocode to input the points scored and the data must be placed in the correct position. Only 10
highest points will be stored.
4.Now write a code and test the program so that it stores the points in the correct position.

Example 5: Write pseudo code that will perform the following.


a) Read in 5 separate numbers.
b) Calculate the average of the five numbers.
c) Find the smallest (minimum) and largest (maximum) of the five entered numbers.
d) Write out the results found from steps b and c with a message describing what they are

Example 6: Write pseudo code that reads 10 numbers and writes them all in sorted order.
Example 7: Write pseudo code that will calculate a running sum. A user will enter numbers that will be
added to the sum and when a negative number is encountered, stop adding numbers and write out the
final result.
Section B [18]
9. Consider the array NUMBERS.
N14/4/COMSC/SP1/ENG/TZ0/XX

(a) (i) Identify the consequence of attempting to output NUMBERS [6]. [1]
(ii) Describe a method of preventing the problem in part (a)(i). [2]
(b) Construct the algorithm that will output the average of all values in the array NUMBERS. [4]
The method minPos() determines the index of the smallest value in an array.
The method maxPos()determines the index of the largest value in an array. For example,
NUMBERS.maxPos() is 3.
Consider the following algorithm fragment.
S=NUMBERS.minPos()
L=NUMBERS.maxPos()
T=NUMBERS[S]
NUMBERS[S]=NUMBERS[L]
NUMBERS[L]=T

(c) (i) Show the contents of the array NUMBERS after the algorithm is applied. [2]
(ii) Deduce the purpose of the algorithm. [2]
(d) Construct the algorithm for the method maxPos(). [4]

Potrebbero piacerti anche