Welcome to Sandilands Primary School Cluster Conference Friday 3 rd October 2014 An Introduction to Algorithms The Language of Programming
The National Curriculum The National Curriculum states that children should be taught … A high-quality computing education equips pupils to use computational thinking and creativity to understand and change the world. Computing has deep links with mathematics, science, and design and technology. The core of computing is computer science, in which pupils are taught the principles of information and computation, how digital systems work, and how to put this knowledge to use through programming. Building on this knowledge and understanding, pupils are equipped to use information technology to create programs, systems and a range of content. Computing also ensures that pupils become digitally literate – able to use, and express themselves and develop their ideas through, information and communication technology – at a level suitable for the future workplace and as active participants in a digital world.
The National Curriculum The national curriculum for computing aims to ensure that all pupils … can understand and apply the fundamental principles and concepts of computer science, including abstraction, logic, algorithms and data representation can analyse problems in computational terms, and have repeated practical experience of writing computer programs in order to solve such problem At Key Stage 1 understand what algorithms are; how they are implemented as programs on digital devices; and that programs execute by following precise and unambiguous instructions. create and debug simple programs use logical reasoning to predict the behaviour of simple programs
The National Curriculum The national curriculum for computing aims to ensure that all pupils … can understand and apply the fundamental principles and concepts of computer science, including abstraction, logic, algorithms and data representation can analyse problems in computational terms, and have repeated practical experience of writing computer programs in order to solve such problem At Key Stage 2 design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts. use sequence, selection and repetition in programs; work with variables and various forms of input and output use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs
Computational Thinking Computational Thinking is not really about computers. It is more about problem solving and about thinking critically and logically about a task so that it can be approached in an efficient and methodical yet creative way. We apply Computational Thinking or Critical Thinking to many tasks and key problems in our lives. It is about: analysing a problem or major task having a method to approach it creating small steps / tasks creating abstractions (models) being efficient (avoiding repetition) having a flow of actions making logical decisions evaluating progress
Algorithms An algorithm is a sequence of instructions for performing a task. Usually an algorithm is created when a precise set of instructions or rules need to be followed to achieve a specific goal. Examples of algorithms: a recipe for baking a cake instructions for flying a plane from London to Madrid instructions for playing a board game instructions for building a piece of IKEA furniture a route map (SATNAV) Algorithms and sets of instructions designed for computers are called programs. Programs are written in programming languages and must be written in a very specific way in order to be understood and followed by the computer. A programmer might first write an algorithm to design a program before committing it to code.
Abstraction Abstraction is about summarising (or simply referring to it by a name) some of the detail (which may or may not have been worked out) of our thinking and problem-solving processes so that we can see and approach a task clearly and efficiently. Abstraction simplifies things for us. It lets us see the whole or part of the problem. It lets us see the wood rather than the trees. We use abstraction all the time in our lives. When we have a conversation, we do not repeat the detail of our life and daily stories in order to convey some meaning. For example, when we refer to doing the “washing” we are making an abstraction (a label which neatly packages the detail) of the many tasks we sequence, repeat, select and combine to complete the problem/task known as “washing”. Some people are not good at abstraction in conversation – they like the detail of a problem or story they want to share.
Abstraction This is a very simple representation of a very complex system. The diagram represents the internet and connections between digital devices. It is an abstraction. We do not really need to know the detail unless we are working at that level.
Abstraction The London Underground Map This is probably one of the most famous abstractions in the world. The simplicity of line and colour represent a very complex system of transportation.
Decomposition To decompose something means to break it down into its constituent parts. To decompose a problem or a task means to examine the problem and break it up into smaller tasks. Some of those tasks might need to be repeated or may only be required if decision-making within the main task requires them to be carried out. Some parts of the decomposed problem may only be able to be carried out when other parts have been completed whereas others may be able to be carried out simultaneously. A decomposed task can be represented in a diagram.
Sequence When we sequence things we arrange them in a particular order. Sometimes, the order in which we place instructions does not matter. However, there are times when the order of items in a set of instructions is crucial to its working and success. The order in which one carries out the task of making a cup of tea or making toast is important. You cannot butter the bread before toasting it and you cannot pour water on the tea bag before boiling the water. A computer will ALWAYS carry out instructions in the order in which they are coded. It is therefore important that the sequence is correct. Can any part of this sequence be changed to achieve the same result? Might one put the toast on a plate before buttering it?
Repetition Repetition is the process of repeating a task a set number of times. Some tasks, like drawing a square, for example, have repeated tasks. FORWARD 100 RIGHT 90 FORWARD 100 RIGHT 90 FORWARD 100 RIGHT 90 FORWARD 100 RIGHT 90 Here you can see that the same TWO instructions are used FOUR times to complete the square so we might write the program as follows: REPEAT 4 [FORWARD 100 RIGHT 90]
Repetition There are various ways in which we can repeat something … forever loop (will keep repeating until the program is stopped) a count-controlled loop (a set number of times) a conditional loop (until a condition is met) FOREVER LOOP The TWO commands inside the FOREVER LOOP will execute over and over again until the program itself is stopped. There is no other way in which the program can exit the repeat loop. Let’s try some repeat loops as an activity
Repetition The TWO commands inside the REPEAT LOOP will execute exactly 10 times. The program will then move on to the next instruction immediately following the REPEAT loop. There are various ways in which we can repeat something … forever loop (will keep repeating until the program is stopped) a count-controlled loop (a set number of times) a conditional loop (until a condition is met) COUNT-CONTROLLED LOOP
Repetition The commands inside the REPEAT UNTIL will execute only until the condition set is true. IN this example, until the Scratch sprite is touching the sprite Dog2. When this is so, the repeat will stop and the program will move on to the next command say “Hello!” There are various ways in which we can repeat something … forever loop (will keep repeating until the program is stopped) a count-controlled loop (a set number of times) a conditional loop (until a condition is met) COUNT-CONTROLLED LOOP
Selection Selection means that part of the algorithm is only executed if certain conditions are true. For example, here is an algorithm to determine whether I will take my umbrella or my jacket. If it is raining then take umbrella else take jacket The part of the algorithm take umbrella is only executed IF it is raining. If it is not raining, the take jacket part of the algorithm comes in action
Selection Sometimes you may need to decide whether or not to carry out an action using TWO conditions. With selection you combine and test a number of conditions using OR – the action will execute if one OR more of the conditions is true. For example, I will take my umbrella if it is raining or it is cloudy. If it is neither raining nor cloudy, I will not take my umbrella but I will take my jacket. If it is raining OR it is cloudy then take umbrella else take jacket Raining but not cloudy = umbrella Cloudy but not raining = umbrella Raining and cloudy = umbrella Not Raining and Not cloudy = jacket
Selection Sometimes you may need to decide whether or not to carry out an action using TWO conditions. With selection you combine and test a number of conditions using AND – the action will execute ONLY if ALL of the conditions are true. For example, I only have a glass of wine on Fridays after 7pm. I drink water at other times. If it is Friday AND it is after 7pm then have wine else drink water Friday but not after 7pm = drink water After 7pm but not Friday = drink water Not Friday and not after 7pm = drink water Friday and after 7pm = drink wine
Variables Variable means something is liable to change. In algorithms and programs, variables are usually changing values. Variables usually have a label or a name so that we can refer to them. For example, in a game there may be a number of values that change. An obvious one is the score of each player which would increase if the player is successful and meets the criteria for increasing their score. In a computer program, variables have unique names. We can usually apply mathematical functions to variables to change their values. For example, in a game, the score might be increased by this statement: SCORE = SCORE + 1 or in Scratch … The program managing this score board has a number of variables to manage.
An Algorithm for a Game The OUTLINE of the game The game involves ONE player. At the beginning of the game, the player has FIVE lives. The player rolls a dice. If the player rolls a 4, 5 or 6, the player gains ONE point. If the player rolls a 1, 2 or 3, the player loses ONE life. The player continues to roll the dice until either the player has gained 5 points (in which case he wins the game and receives a congratulatory message) or the player loses all five lives (in which case he loses the game and receives a commiseration message). Let’s play the game ….
An Algorithm for a Game Let’s think about the game in a computational way, looking at all the computational processes that are carried out we roll the dice (generating a random number) we make a decision about the value of the score we make a decision about the value of the lives we make a decision about when to end the game we make a decision about which message to show We have a number of values that change (variable) throughout the game. These are: the number of lives (LIVES) the player’s score (SCORE) the number rolled on the dice (DICE)
An Algorithm for a Game How might we represent the game in computational language, reducing the narrative which we as humans use in speech? In a higher-level (very friendly) language we might say something like: If we still have some lives and have not reach a score of 5 Roll the Dice (value is DICE) If DICE>3 then add 1 to SCORE if DICE<4 then reduce LIVES by 1 repeat the above If all lives lost (LIVES=0) then say “Better look next time” If not all lives lost (LIVES>0) then say “Well done – you win” We could then turn this into a pictorial algorithm or flowchart. This is a method of abstraction so that we can the algorithm represented on paper without all the detail of how it will actually be coded.
Set SCORE to 0 Change LIVES by -1 REPEAT UNTIL SCORE=5 OR LIVES = 0 ROLL DICE (Value = DICE) IF (DICE>3) Change SCORE by 1 ELSE Set LIVES to 5 IF LIVES=0 SAY “You Lose – Better look next time!” ELSE SAY “Well done – you win!”
Tracing the actions of a program (Predicting its behaviour)
An Algorithm for a Game This is how the game would look in the SCRATCH language. The large section bound by the REPEAT UNTIL command is the part which is doing most of the work. A random number is generated (equivalent to the throw of a dice) and decisions are made as how the score or the number of lives are affected by the value of the dice. Once the repeat loop is finished, the program looks only at the number of lives remaining in choosing a final comment for the player.
Debugging Debugging is the action of correcting any errors in an algorithm or computer program in order to make it function correctly. Often DEBUGGING involves predicting the actions of the program by running through its actions and values before we run them, perhaps using a flowchart and a truth table. We use computational reasoning to do this. running the program, seeing what happens and determining how the functioning or outcomes of the program differ from what was expected or planned. Where and why did it go wrong? checking that all of the necessary commands are included and then checking that the sequence, repetition and selection of commands is correct
Set SCORE to 0 Change LIVES by -1 REPEAT UNTIL SCORE=5 OR LIVES = 0 Change SCORE by 1 ELSE Set LIVES to 5 IF LIVES=0 SAY “You Lose – Better look next time!” ELSE SAY “Well done – you win!” Can you spot the bug in this program? Clue: Something happens in the repeat loop that should not happen every time. ROLL DICE (Value = DICE) IF (DICE>3)
Set SCORE to 0 Change LIVES by -1 REPEAT UNTIL SCORE=5 OR LIVES = 0 Change SCORE by 1 ELSE Set LIVES to 5 IF LIVES=0 SAY “You Lose – Better look next time!” ELSE SAY “Well done – you win!” These commands will reset the LIVES and SCORE values EVERY time the repeat loop is run. This means the game will NEVER end. ROLL DICE (Value = DICE) IF (DICE>3)
Set SCORE to 0 Change LIVES by -1 REPEAT UNTIL SCORE=5 OR LIVES = 0 Change SCORE by 1 ELSE Set LIVES to 5 IF LIVES=0 SAY “You Lose – Better look next time!” ELSE SAY “Well done – you win!” IF (DICE>3) ROLL DICE (Value = DICE) These commands need to placed at the very beginning of the program BEFORE the REPEAT UNTIL loop is executed. In this case the sequence of the commands is very important to the success of the program and the game.
These commands will execute every time the game is repeated. Given that the LIVES is also set back to 5 and the SCORE to 0, the game will never end.