3.1.9 Pseudocode Algorithms
Table of Contents
1 Pseudocode algorithms
Try It
x <-- 10 y <-- 3 x <-- x+y y <-- x - y x <-- x - y
- What is the data type of
x
? - What is the final value of
x
? - What is the final value of
y
?
i <-- 1 WHILE i < 100 i <-- i * 2 ENDWHILE
- What is the initial value of
i
? - On which line is a loop started?
- What is the final value of
i
? - How many times does the loop operate?
Try It
words <-- "CS IS KEWL." newWords <-- "" FOR i <-- 1 TO LEN(words) newWords <-- words[i] + newWords ENDFOR
- What is the initial value of
i
- On which line does iteration begin?
- What data type is
newWords
? - What is the final value of
i
? - What is the final value of
newWords
? - What function does this algorithm perform?
Try It
number <-- 7 answer <-- TRUE FOR i <--2 TO (number - 1) IF number % i = 0 THEN answer <-- FALSE ENDIF ENDFOR
- What data type is
answer
? - What is the initial value of
i
? - What is the final value of
i
? - What does the
%
do? - What is the final value of
answer
? - If the initial value of
number
was 8, what is the final value ofanswer
? - How about 7919?
- What function does this algorithm perform?
Try It
arr <-- [14,2,9,5,1,13,25] N <-- LEN(arr) swapped <-- TRUE hold <-- "" WHILE swapped = TRUE swapped <-- FALSE FOR i <-- 2 TO N IF arr[i-1] > arr[i] THEN hold <-- arr[i] arr[i] <-- arr[i-1] arr[i-1] <-- hold swapped <-- TRUE ENDIF ENDFOR ENDWHILE
- What is the data type of
arr
? - What is the value of
N
? - What is the value of hold after the
WHILE
loop has been entered the first time? - What is the initial value of
i
- What is the final value of
i
? - What is the initial value of
hold
? - What is the final value of
swapped
? - What function does this algorithm perform.