0% found this document useful (0 votes)
19 views2 pages

T3 Homework 3 Answers

Uploaded by

lhuixuan2
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)
19 views2 pages

T3 Homework 3 Answers

Uploaded by

lhuixuan2
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

Homework 3

Developing algorithms using pseudo-code


Unit 1 Fundamentals of algorithms

Answers
1. Write an algorithm in pseudo-code which asks the user to enter a password. If the user
enters “NotAtHome!”, then print “Welcome” and set a variable called correctPassword to
True.
If they enter a different password, display “Wrong password” and set correctPassword to
False.
OUTPUT "Please enter password"
password ← USERINPUT
correctPassword ← False #the correct password is first set to false
IF password = "NotAtHome!" THEN
OUTPUT "Welcome"
correctPassword ← True
ELSE
OUTPUT "Wrong password"
correctPassword ← False #this section isn’t really needed as correctPassword is
#initialised as False – however, if it hasn’t been initialised
#it will be needed
ENDIF

2. The following algorithm accepts three integer values from the user, and then prints the
maximum value entered. Some lines are missing.
Complete the program by inserting instructions after Line 4. [4]
1 OUTPUT "Please enter three values."
2 x ← USERINPUT
3 y ← USERINPUT
4 z ← USERINPUT
5 OUTPUT "Maximum value entered is: "
IF x > y AND x > z THEN
OUTPUT x
ELSE IF y > x AND y > z THEN
OUTPUT y
ELSE
OUTPUT z
ENDIF

1
Homework 3
Developing algorithms using pseudo-code
Unit 1 Fundamentals of algorithms

3. Write an algorithm for a program which inputs the lengths a, b and c of the three
sides of a triangle. The program then determines whether the triangle is right-angled
and prints out a message to say whether or not the triangle is right angled. You may
assume that a is the longest side. The triangle is right-angled if a2 = b2 + c2
a = USERINPUT
b = USERINPUT
c = USERINPUT

asquared = a * a
bsquared = b * b
csquared = c * c
sumbandc = bsquared + csquared
IF asquared = sumbandc THEN
OUTPUT "the triangle is right angled"
ELSE
OUTPUT "the triangle is not right angled"
ENDIF

[Total 12 Marks]

You might also like