Pseudocode Assignment
Pseudocode Assignment
Name: ________________________________________________________
Question
Fill in the blanks question, 2 blanks
Complete the pseudocode that rejects any number that is not even. Run the
algorithm as many times as the user would like and on quitting, display the
number of rejects at the end of the algorithm.
Pass ← 0
Fail ← 0
INPUT Number
WHILE Number > 0 DO
IF MOD(Number,2) =
THEN
OUTPUT "pass"
Pass ← Pass + 1
ELSE
OUTPUT "fail"
Fail ← Fail + 1
ENDIF
OUTPUT "Enter 0 to quit program"
INPUT
ENDWHILE
OUTPUT "The number of rejects is", Fail
Question
The algorithm to calculate the class average of 100 pupils is given below. But the
algorithm runs indefinitely. Can you spot the error in the code?
StudentCount ← 100
ClassTotal ← 0
ClassAverage ← 0
WHILE StudentCount <=100 DO
INPUT IndividualTotal
ClassTotal ← ClassTotal + IndividualTotal
StudentCount ← StudentCount - 1
ENDWHILE ClassAverage ← ClassTotal / 100
OUTPUT ClassAverage
Question
Fill in the blanks question, 4 blanks
Given this pseudocode:
INPUT Number
FOR Index ← 0 TO 5
Sum ← Number + Index
IF MOD(Sum,2) = 0
THEN
OUTPUT “Lucky draw”
ELSE
OUTPUT “No draw”
INPUT Number
ENDIF
ENDFOR
NEXT Index
The input values are 3, 2, 4, 6, 1.
3 0 3 No Draw
2 1 3 No Draw
4 2 6 Lucky Draw
6 4 10 Lucky Draw
6 5 11 No Draw
Question
This pseudocode represents the Bubble sort algorithm.
SizeOfArray ← LENGTH(MyArray)
FOR i ← 1 TO SizeOfArray
Swapped ← False
FOR j ← 1 to SizeOfArray - 1
IF MyArray[j] > MyArray[j+1]
THEN
temp ← MyArray[j+1]
MyArray[j+1] ← MyArray[j]
MyArray[j] ← temp
Swapped ← True
ENDIF
NEXT j
IF (NOT Swapped)
THEN
break
ENDIF
NEXT i
The array MyArray used in the pseudocode contains the following data:
5 8 7 2
When you go through the array from left to right comparing adjacent elements
and swapping them, if necessary, you complete a pass.
This bubble sort algorithm might need to make several passes in order to sort the
array.
temp ← Letters[i+1]
Letters[i+1] ← Letters[i]
Letters[i] ← temp
The array Letters used in the pseudocode contains:
M R G
Fill in the blanks to show the new state of the array Letters when i has the
value of 2.