120 likes | 303 Views
Algorithms. By Tanya Duffy. What are Algorithms?. Describe step by step how to solve a problem Represented by Pseudo code or Flowcharts Pseudo code uses English & some keywords Flowcharts are diagrams using lines and arrows to elements. Terminator. Process. Decision. Subprogram.
E N D
Algorithms By Tanya Duffy
What are Algorithms? • Describe step by step how to solve a problem • Represented by Pseudo code or Flowcharts • Pseudo code uses English & some keywords • Flowcharts are diagrams using lines and arrows to elements Terminator Process Decision Subprogram
Four Programming Structures • Sequence • Selection • Repetition • Subprograms
Sequence Flowchart BEGIN Pseudocode BEGIN Fill a kettle with water Boil the water in the kettle Put the tea leaves in the pot Pour boiling water in the pot END Problem: Write a set of instructions that describe how to make a pot of tea Fill a kettle with water Boil the water in the kettle Put the tea leaves in the pot Pour the boiling water in the pot END
Selection Pseudocode BEGIN IF the telephone is ringing THEN Answer the telephone END IF END BEGIN Problem 1: Write a set of instructions to describe when to answer the phone. The telephone is ringing False True Answer the telephone END
Selection Flowchart BEGIN Problem 2: Write a set of instructions to follow when approaching a set of traffic control lights Pseudocode BEGIN IF the signal is green THEN proceed through the intersection ELSE Stop the vehicle ENDIF END The signal is green False True Stop the vehicle Proceed through the intersection END
Selection BEGIN Problem 3: Write a set of instructions that describes how to respond to all possible signals at a set of traffic control lights. Signal is Pseudocode BEGIN CASEWHERE signal is red : stop the vehicle amber : stop the vehicle green : proceed through the intersection OTHERWISE : proceed with caution ENDCASE END red amber green otherwise Stop the vehicle Stop the vehicle Proceed through the intersection Proceed with caution END
Repetition BEGIN Pseudocode BEGIN WHILE the train is moving keep wholly within the carriage ENDWHILE END PRE-TEST Problem: Determine a safety procedure for travelling in a carriage on a moving train The train is moving False True Keep wholly within the carriage END
Repetition BEGIN Pseudocode BEGIN REPEAT beat the egg whites UNTIL fluffy END POST-TEST Problem: Determine a procedure to beat egg whites until fluffy Beat the egg whites Egg whites fluffy False True END
Subprograms Guess the Number Problem In a simple number game your opponent thinks of a secret number between 1 and 100. In no more than 10 guesses you have to try to guess the number. After each guess your opponent tells you if your guess was too high, too low or correct. Your opponent also keeps track of how many guesses you have had and tells you the game is over when you use all of your ten guesses or when you guess the number correctly. Describe an algorithm which takes the role of your opponent in this game. Include in your solution a subprogram which checks for illegal guesses (those less than 1 or greater than 100). Include also the subprogram which generates a secret number between 1 and 100. Do not expand this but assume it is available.
Solving a Maze Getting a robot or a computer to find its own way through a maze is not easy. There are problems in detecting the walls to avoid collisions, problems in getting out of “dead-ends”, problems in avoiding logic traps where the computer or robot is simply unable to progress any further… For a human who can see the entire maze, the answer is easy!! But if you were a robot who could only see just 1 square directly ahead, how could you get HOME?
Pseudocode • Very precise form of written instructions • Layout and words follow set rules • KEYWORDS always written in UPERCASE letters • All but simplest pseudo code algorithms split into several separate lists called Sub Programs