Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Techniques :: Flow Diagrams and Pseudocode

Similar presentations


Presentation on theme: "Programming Techniques :: Flow Diagrams and Pseudocode"— Presentation transcript:

1 Programming Techniques :: Flow Diagrams and Pseudocode
Last modified: 28th June 2019

2 www.drfrostmaths.com ? Everything is completely free.
Why not register? Registering on the DrFrostMaths platform allows you to save all the code and progress in the various Computer Science mini-tasks. It also gives you access to the maths platform allowing you to practise GCSE and A Level questions from Edexcel, OCR and AQA. With Computer Science questions by: Your code on any mini-tasks will be preserved. Note: The Tiffin/DFM Computer Science course uses JavaScript as its core language. Most code examples are therefore in JavaScript. Using these slides: Green question boxes can be clicked while in Presentation mode to reveal. Slides are intentionally designed to double up as revision notes for students, while being optimised for classroom usage. The Mini-Tasks on the DFM platform are purposely ordered to correspond to these slides, giving your flexibility over your lesson structure. ?

3 Specifying Algorithms
An algorithm is just a set of instructions to carry out a problem. When designing an algorithm, we often plan the steps that the algorithm will need to take before we write the code. If we’re working in a team or want to share the algorithm with others, we need some way to describe how the algorithm work that is not dependent on a particular programming language. Take for example a well-known algorithm like Dijkstra’s Algorithm, which finds the shortest path between two vertices in a graph: We could write a worded description of the algorithm. We could write ‘pseudocode’ (which we’ll look at in a second)

4 Specifying Algorithms
In some cases we can also describe an algorithm by providing a flow diagram. Start Flow diagrams provide a visual way of showing the sequence of steps in an algorithm. ? Indicates route to follow in the algorithm. Input number No ? Start/Stop ? Start and end of the algorithm (note rounded corners) ? Is number 42? e.g. Values taken from console, read from file, outputted to console… Input/Output ? ? A general step/instruction in your algorithm. Process Yes ? Where we decide where to progress based on a particular question. There will be multiple arrows coming out. ? Destroy Earth Decision ? Reference other flow diagrams. Subroutines are covered in another resource. ? Sub Routine End ?

5 Output current position
Example Flow Diagram You want to devise an algorithm that searches an array for the name “Bob” and outputs the position in the array where Bob is found. We want to do a linear search, i.e. check the items in order one by one until we find the item. The algorithm should output the position of the name if found, and -1 otherwise. Draw a flow diagram for this algorithm. ? No At end of array? No Is name “Bob” Get next name in array Start Yes Yes Output current position -1 Stop

6 Receive shot notification
Test Your Understanding You are producing a scorekeeping system for a game of archery, and keep a tally of the player’s score. Each time they hit the target, they score 10 points, and if not, score no points. There repeat this until they’ve had 20 attempts, at which point their score is displayed. Draw a flow diagram for this algorithm. ? Start Add 10 to score No Yes Had 20 shots? No Hit target? Set score to 0 Receive shot notification Yes Output score Stop

7 Pseudocode Pseudocode is notation intended to resemble code. It is intended to be language-neutral and not be concerned with the exact syntax of a particular language. Like flow diagrams, the intention is that programmers can then convert these quickly into code in a particular language. As pseudocode is intended to resemble code, this is much easier than converting a flow diagram to code. Pseudocode is not a real language and is not an exact science. The important thing is to make it easy to understand and easy to convert to actual code. Avoid being too general or too vague. It is fine to use control structures that are common to most languages, e.g. if statements, while and for loops. Regardless of the language-specific function needed to input a number, it’s immediately obvious what this line means. age:= input(“What is your age”) age:= age + 3 print(“Your age in 3 years time is “+age) It’s again clear, regardless of syntax, that we want reassign age with a value 3 greater. Ask for age Increment age by 3 Output the new age Technically this is still pseudocode. But it reads more like a description. A programmer would have to think more to turn it into code.

8 Converting Flow Diagrams to Pseudocode
An algorithm can be represented by the following flow diagram: Using appropriate pseudocode, write this algorithm. End Start Add numbers Output result Input a number 𝑥 Yes Is 𝑥 even? No Yes Input a number 𝑦 Subtract 𝑦 from 𝑥 ? x = input(“Enter a number x”) y = input(“Enter a number y”) if(isEven(x))print x + y else print x - y while we could have used x%2 = 0 for “is x even?”, this is too language specific. isEven(x) is very clear in its meaning.

9 Round 2 – Where a while is required
An algorithm can be represented by the following flow diagram: Using appropriate pseudocode, write this algorithm. Add number to total Start No Is number >10? Let total be 0 Input a number Yes Output total End ? total = 0 num = input(“Enter a number”) while(num <= 10) total= total + num endwhile print(“Total is “+total) You need to be careful here that you don’t add the last number inputted if that number was greater than 10. It’s possible to avoid duplicating the input line of code.

10 Exam-Style Question ? ? ? OCR Specimen Paper
Either a more description-based or pseudocode based approach is acceptable.

11 Review ? ? ? Why might we write pseudocode for an algorithm?
Gives a clear idea of how the algorithm works, in such a way that makes it easy to translate to actual code. It is not tied down to the specifics a particular programming language. Why then might we prefer a flow diagram to pseudocode? Gives a clearer visual indication of the flow of the code. What does the following flowchart shape mean? A decision where subsequent flow in the diagram depends on the result of that decision. ? ? ?


Download ppt "Programming Techniques :: Flow Diagrams and Pseudocode"

Similar presentations


Ads by Google