- BBHS GCSE Computing

advertisement
Solving problems
Algorithms
An algorithm is a series of instructions that when followed solves a problem. All computer programs are an
expression of an algorithm, though it is important to realise that an algorithm does not have to be a computer
program. An algorithm could just as easily be written down on paper as a series of steps or expressed as a flow chart
or other diagram.
This worksheet is going to try and help you develop an understanding of how to solve problems and express those
solutions as a computer program.
Problem 1 – Outputting the 2 times table
Specification – Write a program that outputs the 2 times table from 1 x 2 to 12 x 2.
This is a simple problem, but it will help illustrate the stages you need to go through in order to solve a problem.
Let’s start by visualising the output. The program, if we are successful should output something like this:
1x2=2
2x2=4
3x2=6
.
12 x 2 = 24
Knowing what the output looks like we can now start to think about how we will get the computer to do this.
We know that we need to output 12 lines, just like the ones above because we need to do 1 x 2 all the way up to 12
x 2.
We should know that we need a loop, because we need to repeat the same calculation 12 times, with one small
change, the number we multiply 2 by each time. This number increases by 1 each time (1 x 2, 2 x 2, 3 x 2 etc.). Let’s
call this value that increments each time n.
Also, we should remember that if we know exactly how many times we are going to repeat a loop before we start
the loop, then we can use a FOR loop.
This would give us an algorithm that looks something like this (note this is not yet written in BBC Basic, it is written in
pseudo code, a form of structured English that helps you express what needs doing without worrying about the
syntax of a particular language):
Use a FOR loop when you know how
FOR n = 1 TO 12
many times you are going to repeat
PRINT n “x 2 = “ n * 2
something BEFORE you enter the loop.
NEXT n
We can also express this as a flow chart:
Start
n=1
n > 12?
Yes
No
PRINT
n*2
n=n+1
End
Here is the algorithm written in BBC Basic:
This is the output generated by this program:
At the start of this program n assumes the value 1, it is then incremented each time we go round the loop, until n
eventually reaches 12. This then ends the loop.
We can however come up with a more general solution to this problem that would output any times table:
Problem 2 – Validating an email address
Specification – Write a program that validates an email address, an email address must contain an ‘@’ sign.
Continue to ask the user to enter an address until this criteria is met.
This is another fairly simple problem. We need to:


Accept from the user an email address
Check that the address contains an ‘@’ sign
o If it does output ‘Valid email address’ and end the program
o If it does not output ‘Invalid email address’ and request they enter the address again
This would give us an algorithm looking something like this (again not yet in BBC Basic syntax):
REPEAT
INPUT Address
IF Address contains ‘@’ THEN
Valid = TRUE
PRINT ‘Valid email address’
ELSE
Valid = FALSE
PRINT ‘Invalid email address, please try again.’
END IF
UNTIL Valid = TRUE
Here is the flow chart:
Start
Input
Address
No
Contains
‘@’?
Yes
Valid =
TRUE
Valid =
FALSE
Output
‘Invalid
address’
Output ‘Valid
address’
No
Valid =
TRUE?
Yes
End
Here is the algorithm written in BBC Basic:
Use the Help Tutorial if you
cannot remember what
commands do
Problem 3 – Who is the head of house?
Specification – A program should accept the name of a house e.g. ‘Derwent’ it should then output the assistant
head of that house e.g. ‘Mr Edge’. If ‘Dove’ where entered it would output ‘Mr Barker’ etc.
Use the space below to plan your program. You might begin by sketching what will appear on screen if the program
runs successfully. You might then begin by writing some pseudo code to try and work out how the algorithm will
work.
Solved with a CASE statement:
Solved with an IF statement:
Problem 4 – A strong password
Specification – A password is strong if it contains a mixture of upper and lower case letters as well as numbers.
Write a program that accepts a password and outputs whether it is ‘weak’ or ‘strong’. HINT: you may need to use
a loop and several of the different String processing functions (see the String section in the Tutorial.
Use the space below to help you plan your solutions:
Solution:
Download