File - Lets learn Computing .co.uk

advertisement
By the end of this session you should
be able to...
Understand what is meant by ‘the use of
algorithms to describe a problem’ including:
• Analysis and design of algorithms for a given
situation.
• Compare the suitability of different algorithms for
a given task and data set.
Good Practice
• Write the date at the top of a new page at the start of
every lesson
• When you see the pen symbol, copy down what is on
the board (you can put it into your own words).
• Your book will be checked regularly, it must be
presentable.
• When you see the book symbol with a page number,
you should turn to that page in your text book.
Page: 00
Starter
How many programming languages are there?..
Programming
Languages
Transferable skills
• So if there are so many programming languages, how
do we ensure all programmers follow the correct
method to meet a projects needs?
• We use algorithms to represent the program before we
make the program.
• An algorithm is transferable as it represents the flow of
data/ concepts. All programming languages have
common features such as IF statements.
• By writing an algorithm we are ensuring any
programmer can develop the final solution. Why do
you think this is important?
Watch this…
• https://www.khanacademy.org/computing/co
mputer-science/algorithms/intro-toalgorithms/v/what-are-algorithms
So what is an algorithm?
• Algorithms are a set of instructions that can be
followed to perform a certain task.
• Two common methods of writing algorithms are
flowcharts and pseudo code.
• You need to be familiar with both, by which you
should read, write and interpret these methods
of displaying solutions.
• It is through algorithms we are able to develop
programming code that computers can use.
Page: 049
Writing algorithms
• Algorithms can be notoriously hard to do all but
for the most simple task.
• Non-trivial problems can have a range of out
comes. It is the job of the person writing the
algorithm to find the most effective outcome.
• The power of algorithms comes from the short
cuts that have been designed into them.
• Algorithms must work with any instance of the
same problem. The whole point of presenting
algorithms to a computer is that they can be
applied to different sets of similar data.
Task
What does this algorithm do?
Start
Is
temp
<19
Yes
Is
temp
>21
Yes
Turn heating on
No
No
Turn heating off
Task
What does this algorithm do?
Start Program
If temp <19 turn heating on
Else If temp >21 turn heating
off
Task
• Create a simple program in Python that
replicates the algorithm on the previous slide.
• You should ask the user to specify the
temperature and have an output that reads
something similar to “heating is on”, etc.
Understanding pseudo code
• Flowcharts are relatively straight forward to
follow, but pseudo code can be tricky to get your
head round at first.
• Follow this Link:
https://www.khanacademy.org/computing/comp
uter-programming/programming/goodpractices/p/pseudo-code#
• Now have a go at replicated the task completed
in the video but this time do it in Python.
Sequence, Selection and Iteration
• An algorithm follows a sequence of
instructions. However simple sequences do
not allow for complex instructions to take
place.
• We can use selection and Iteration to build a
complex algorithm.
Selection
• IF Check the condition
• IF – THEN Action to take based on condition
• IF – THEN – ELSE If condition is not valid perform
another action
• CASE Select response from an array
• ENDIF Ends the construct
Iteration
• FOR Continues a loop for a set number of
times
• WHILE Continues a loop while a condition
holds true
• REPEAT Continues a loop until the condition is
met
How to correctly write Pseudocode
Indent
Selection in capitals
IF statements
ended correctly
Get temperature
IF temperature <19 THEN
Switch heating ON
ELSE
IF temperature >20
THEN
Switch heating OFF
ENDIF
ENDIF
Further help
• Useful links:
• http://www.wikihow.com/Write-Pseudocode
• https://www.youtube.com/watch?v=4G0EYfrrDT8
• http://www.slideshare.net/DamianGordon1/pseudocode10373156
• http://users.csc.calpoly.edu/~jdalbey/SWE/pdl_std.html
Task:
• Review the links above to familiarise yourself with pseudo
code.
Algorithm selection
• The selection of whether to use a flowchart or pseudo
code to represent a computer program often depends
on the audience and complexity of the computer
solution.
• For example, if you know you are going to program the
solution straight away then as we have seen pseudo
code straight into your programming language might
be the best choice. However, representing the heating
system might never be programmed but used to
explain to customers how the heating system works. So
a flowchart is more appropriate.
Task
• For each of the following tasks identify a possible
algorithm method and write a solution to the
problem using your chosen method.
– Drawing a snowman inside a snow globe
– Representing how traffic light on a bridge work
– Representing a system that checks the username and
password of a user
– Representing a system that generates 10 questions
about Game of Thrones and then assigns a character
to the player depending on their answers.
Task
• Choosing at least two of your algorithms,
represent the solutions within a programming
language of your own choice.
• Remember to comment your code to ensure
you understand what you have done when
you come to revise.
Identifying what an algorithm does
• It is not enough to just be able to write an
algorithm; you need to be able to identify what
one does.
• Within an exam you might be presented with
pseudo code, a flowchart or a section of code
from Java or Python. Your task will be to explain
what it does!
• Remember computational methods are:
abstraction, problem decomposition and the
development of algorithms.
Example
Code:
def find_max (L):
max = 0
for x in L:
if x > max:
max = x
return max
Algorithm:
1.Set max to 0.
2.For each number x in the
list L, compare it to max. If x is
larger, set max to x.
3.max is now set to the largest
number in the list.
Task
Describe what the following code does:
int x, count, even;
x = 0;
even = 0;
cin>>count;
while(x < count)
{ cout<<even;
even = even+2;
x = x+1;
}
Answer
Read count
Set x to 0;
While(x < count)
Set even to even + 2
x=x+1
write even
Task
Describe what the following pseudo code does:
Read num1, num2, num3
If (num1 < num2)
If(num2 < num3)
Write num1 , num2, num3
Else
If(num3 < num1)
Write num3, num1, num2
Else
Write num1, num3, num2
else
If(num1 < num3)
Write num2 , num1, num3
Else
If(num3 < num2)
Write num3, num2, num1
Else
Write num2, num3, num1
Task
Program a solution to the pseudo code below:
1. Declare an integer variable called n
2. Declare an integer variable sum
3. Declare an integer variable f1
4. Declare an integer variable f2
5. set sum to 0
6. set f1 and f2 to 1
7. set n to 50
8. repeat n times
a. sum = f1 + f2
b. f2 = f1
c. f1 = sum
d. print sum
9. end loop
Task
Program a solution to the pseudo code below:
1. Declare an integer variable called n
2. Declare an integer variable sum
3. Declare an integer variable f1
4. Declare an integer variable f2
5. set sum to 0
6. set f1 and f2 to 1
7. set n to 50
8. repeat n times
a. sum = f1 + f2
b. f2 = f1
c. f1 = sum
d. print sum
9. end loop
Project tasks
• Your final project will be a computer based solution to
the day care problem.
• Using both flowcharts and pseudo code write
algorithms showing possible solutions to the problem.
• As demonstrated over the previous lessons you might
want to write pseudo code into notepad so that it can
be copied into the program you choose to write your
final solution in.
• It is a good idea to spend some time thinking about
how you are going to represent the different parts of
the problem.
Download