Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem Solving Designing Algorithms.

Similar presentations


Presentation on theme: "Problem Solving Designing Algorithms."— Presentation transcript:

1 Problem Solving Designing Algorithms

2 Solution Design Standards
Algorithms (Intro) This lesson aimed at designing solutions Solution: the means of solving a problem or dealing with a difficult situation We could come across many problems Finding the quickest way home Finding our name on a register Checking if a player has won at Noughts and Crosses Solution Design Standards

3 Solution Design Standards
Algorithms (Intro) Each problem may have any number of solutions There isn’t always just one However, some solutions may be better than others Take less time Be less complex Each solution can be defined by an algorithm Solution Design Standards

4 Solution Design Standards
Algorithms (Intro) An algorithm is a series of steps When done in order, solve a problem Algorithms can be described in lots of ways Written text Pseudocode Flowcharts Describing an algorithm in this way is its design Solution Design Standards

5 Solution Design Standards
The following is an algorithm For getting into a secure building Highlight the important parts of it “To get into this secure building, you first need to open the door’s panel. On it is a button. You need to press the button. A card slot will appear near the door’s panel. Put your key-card into this slot. On doing this, a door handle will appear from the door. Make sure you place your thumb on top of the thumb-reader on the door, and turn the handle clockwise. You should hear the door ‘CLICK’ if you got it right. If not, make sure your thumb is correctly on the reader and try turning the handle again.” Solution Design Standards

6 Solution Design Standards
Algorithm Pseudocode Here are the important parts of the algorithm We’ll call these the steps “To get into this secure building, you first need to open the door’s panel. On it is a button. You need to press the button. A card slot will appear near the door’s panel. Put your key-card into this slot. On doing this, a door handle will appear from the door. Make sure you place your thumb on top of the thumb-reader on the door, and turn the handle clockwise. You should hear the door ‘CLICK’ if you got it right. If not, make sure your thumb is correctly on the reader and try turning the handle again.” Solution Design Standards

7 Solution Design Standards
Algorithm Pseudocode We’ll take only these steps And write them Top-to-bottom Include numbers as well Makes the algorithm easier to follow Still the same algorithm Just different description See how Step 6 has sub-steps? a. and b. Only run through those if Step 6 happens Open the door’s panel Press the button Put key-card in slot Place thumb on thumb reader Turn handle clockwise If door didn’t click Make sure thumb is correctly on reader Try turning handle again Solution Design Standards

8 Solution Design Standards
Algorithm Pseudocode Let’s think about a program briefly Program code can contain a few things A sequence of steps Some selection Possibly some iteration The same holds for algorithms Always have a sequence of steps Could have some selection Could have some iteration Open the door’s panel Press the button Put key-card in slot Place thumb on thumb reader Turn handle clockwise If door didn’t click Make sure thumb is correctly on reader Try turning handle again Solution Design Standards

9 Solution Design Standards
Algorithm Pseudocode For example, Step 6 Uses “If” That’s a form of selection If this is true, do this In this case, if the door doesn’t click Do these steps again If we were to make this in a program Would use an if-statement Put a. and b. in if-statement’s scope Open the door’s panel Press the button Put key-card in slot Place thumb on thumb reader Turn handle clockwise If door didn’t click Make sure thumb is correctly on reader Try turning handle again Solution Design Standards

10 Solution Design Standards
Let’s do this again This time go straight to the algorithm’s steps Write each step, in order With numbers in front of them Add sub-steps where needed To check if a number is a prime number (we’ll call it prime here), we run through all the numbers from 2 to one less than prime (we’ll call this divisor). For each divisor, we check if it divides wholly into prime. If it does, we know prime is not a prime number. If we get through all the divisors, we know that prime is a prime number. Solution Design Standards

11 Solution Design Standards
Algorithm Pseudocode Here are the algorithm’s steps We have something here not in the previous example Variables Have a prime and a divisor This isn’t the only solution Just one of them Get a prime number (called prime) For every number from 2 to one less than prime (called divisor) Check if divisor goes wholly into prime If it does, prime is not a prime number After every divisor has been checked (and no divisors found), prime is a prime number. Solution Design Standards

12 Solution Design Standards
Algorithm Pseudocode We can try making this more ‘code’ like Make variables Use more correct code syntax Will end up with something ‘like’ code Pseudocode Literally ‘fake code’ IsPrime(prime) FOR divisor ← 2 to prime – 1 IF divisor divides cleanly into prime RETURN false END IF END FOR RETURN true END IsPrime Solution Design Standards

13 Solution Design Standards
Algorithm Pseudocode Every construct in a programming language Can be converted into Pseudocode We can write Pseudocode in whatever way we want However, we’ll use some standards This will make every piece of Pseudocode easy to understand Solution Design Standards

14 Solution Design Standards
Algorithm Pseudocode Always start constructs with capital letters Scope needs to ‘start’ and ‘end’ Assignment handled by an arrow (←) OUTPUT FOR WHILE IF FOR EACH DO ELSE ELSE IF WHILE playing IF number is even … … END WHILE END IF pivot ← length / 2 Solution Design Standards

15 Solution Design Standards
For the following problem, create Pseudocode for an algorithm that solves it “Given two numbers (which are assigned the words Fizz and Buzz respectively), how could we output the numbers from 1 to 100, replacing the number with Fizz and/or Buzz whenever it is a multiple of them? Solution Design Standards

16 Solution Design Standards
Algorithm Pseudocode Here is one way of solving this problem There are lots of other ways of doing this Note the IF, ELSE IF, ELSE chain The END IF doesn’t appear until the end Makes them all part of the same selection OutputFizzBuzz100(fizz, buzz) FOR number ← 1 to 100 IF number divisible by fizz AND buzz OUTPUT “FizzBuzz” ELSE IF number divisble by fizz OUTPUT “Fizz” ELSE IF number divisible by buzz OUTPUT “Buzz” ELSE OUTPUT number END IF END FOR END OutputFizzBuzz100 Solution Design Standards

17 Solution Design Standards
Algorithm Flowcharts There is a more graphical way of defining/representing algorithms Flow Charts Show the flow of a program from start to finish Arrows represent direction in the program Shapes represent statements Different shapes do different things Solution Design Standards

18 Solution Design Standards
Algorithm Flowcharts Shapes are called nodes Lines are called edges Here are each of the nodes Solution Design Standards

19 Solution Design Standards
Algorithm Flowcharts START Node Shows the start of a program/algorithm Should always have one in a flowchart END Node Shows the end of a program/algorithm Can have one or more Solution Design Standards

20 Solution Design Standards
Algorithm Flowcharts PROCESS Node Describes a statement in a program/algorithm Usually handles Variable declaration/assignment Function calls INPUT/OUTPUT Node Can output OR get input Solution Design Standards

21 Solution Design Standards
Algorithm Flowcharts BRANCH Node Handles selection Can have two or more arrows coming from it Could be yes/no question, or more open Note that we don’t have iteration nodes For-loops or while-loops We need to cleverly use BRANCH nodes to show loops Solution Design Standards

22 Solution Design Standards
Algorithm Flowcharts This flowchart has a BRANCH node at the end Checks if number is 100 If it isn’t it increments number Then goes back up the flowchart This creates a loop Since number is being incremented We can assume it’s a for-loop If no incrementing was done Could use a while-loop instead Solution Design Standards

23 Solution Design Standards
Time for some flowchart practice Create a flowchart for the Bubble Sort algorithm If you’re unsure, here’s what it is “First, get a list/array of integers. Then, starting at the beginning, work our way to the last but one number. For every number we look at, compare it to the next number along. If the current number is larger than the next number, we swap them around. We repeat this whole process until we get through all the numbers without making any swaps.” Solution Design Standards

24 Solution Design Standards
Algorithm Flowcharts Here is one way of making the flowchart We use BRANCH nodes to create loops The second BRANCH checks the index against a number After incrementing Sounds like a for-loop The third BRANCH checks a boolean value No incrementing done Sounds like a while-loop Solution Design Standards

25 Solution Design Standards
Time for some general practice Take a look at the scenario below, and come up with a solution for it Create Pseudocode and a Flowchart for this solution “A student has a huge list of student names in front of him, in alphabetical order. He needs to look through this list for a specific student. Can you think of any way (or process) the student can use to find the name their looking for?” Solution Design Standards

26 Solution Design Standards
Implement your solution’s design in a programming language Use any of the following languages C# Java Python Solution Design Standards

27


Download ppt "Problem Solving Designing Algorithms."

Similar presentations


Ads by Google