0% found this document useful (0 votes)
3 views3 pages

Software Development Worksheet 2 Answers

The document consists of a worksheet focused on software development, specifically writing and following algorithms. It includes tasks related to a Caesar cipher for text encryption, a bubble sort algorithm for sorting an array of marks, and the Russian peasant's algorithm for multiplying integers. Each task provides pseudocode, expected outputs, and explanations of the algorithms' purposes.

Uploaded by

lombardirin
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Software Development Worksheet 2 Answers

The document consists of a worksheet focused on software development, specifically writing and following algorithms. It includes tasks related to a Caesar cipher for text encryption, a bubble sort algorithm for sorting an array of marks, and the Russian peasant's algorithm for multiplying integers. Each task provides pseudocode, expected outputs, and explanations of the algorithms' purposes.

Uploaded by

lombardirin
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Worksheet 2

Unit 3 Software development

Worksheet 2 Writing and following algorithms Answers

Task 1
1. What will be the output from the algorithm below if the user inputs “Hi, Jo!”
Explain briefly the purpose of the algorithm.

function encrypt(message, shift)


message = lowercase(message)
encryptedMessage = ""
for x in message
if x in “abcdefghijklmnopqrstuvwxyz”
num = ord(x) # convert to ASCII value
num = num + shift
if num > ord(“z”) # wrap if necessary
num = num – 26
endif
char = chr(num) # convert back to character
encryptedMessage = encryptedMessage + char
else
encryptedMessage = encryptedMessage + x
endif
next x
return encryptedMessage
endfunction

# main program
shift = 3
msg = input(“Enter your message: ”)
encryptedMessage = encrypt(msg, shift)
print(“The encrypyted message is: ”, encryptedMessage)

Output is kl, mr!

(See Python program W2 Qu 1 Caesar [Link])

The purpose of the program is to encrypt text using the Caesar cipher. All uppercase letters
are changed to lowercase. Non-alphabetic characters such as space, and ! are not
encrypted.

1
Worksheet 2
Unit 3 Software development

Task 2
2. An array marks is defined as follows: marks[15, 18, 14, 9, 16, 12, 10]

A pseudocode algorithm for an algorithm is given below.

items = len(marks)
for i = 0 to items - 2
for j = 0 to (items - i - 2)
if marks[j] > marks[j+1]
temp = marks[j]
marks[j] = marks[j+1]
marks[j+1] = temp
endif
next j
next i
print (marks)

One pass is made through the outer loop of the algorithm.


Complete the trace table below to show how the contents of the array changes.

marks
items i j temp
[0] [1] [2] [3] [4] [5] [6]
15 18 14 9 16 12 10

7 0 0 15 18 14 9 16 12 10

1 15 14 18 9 16 12 10

2 15 14 9 18 16 12 10

3 15 14 9 16 18 12 10

4 15 14 9 16 12 18 10

5 15 14 9 16 12 10 18

What is the name of the algorithm?


Bubble sort
(See Python program W2 Qu 2 bubble [Link] in folder)

2
Worksheet 2
Unit 3 Software development

3. Complete the trace table to determine the purpose of the following algorithm. Test it with
input 11 and 5.

x = input ("Enter the first integer: ")


y = input ("Enter the second integer: ")
z = 0
while x > 0
if x mod 2 == 1 then
z = z + y
endif
x = x div 2
y = y * 2
endwhile
print ("Answer =", z)

x y z x>0 x mod 2 == 1 output

11 5 0 True True

5 10

15

2 20 False

1 40 True

55

0 80 False Answer = 55

The purpose of the algorithm is to multiply two integers.


It is known as the “Russian peasant’s algorithm”. Students can look it up on the Internet to
find out why it works!

The order that the columns are written in the trace table can make a difference to how easy
is to fill in. Putting z before x and y would make a neater trace table. Students could
experiment with this, using different numbers, e.g. 3 and 12.

See Python program W2 Qu 3 Russian peasants [Link].

Get students to code the program and try it out with different integers.

You might also like