240 likes | 257 Views
Learn the basics of algorithms, their importance in software development, and how to write and test them. Discover the role of variables and their use in algorithms. Engage in exercises to practice algorithm design.
E N D
Algorithms! 10 Programming
Have I seen an Algorithm before? • Answer - Maybe. Everything we do could be considered an algorithm… can you guess what this algorithm does? Input: toothpaste, toothbrush, water Output: clean teeth, waste froth Begin Get toothbrush Get toothpaste Put toothpaste on toothbrush Wet toothbrush Open Mouth Brush teeth for 2 mins Rinse mouth Rinse toothbrush End
What is an Algorithm? • An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output in a finite amount of time. • Unambiguous – clearly defined, makes sense to others • Output – work or information that comes out of a process • Finite – the algorithm has a beginning and an end • Also… Input – work or information that goes into a process
Difference between Algorithms and Programming • An algorithm is a detailed step-by-step method for solving a problem. • We can plan algorithms using Word, Microsoft Visio, etc… • Programming is an implementation of one or more algorithms in a programming language – eg. VB, ActionScript, Javascript, C++, etc…
Software Development Life Cycle • Question 1 – which stage do you think we would: • Identify what the problems are? • Identify what the solution should be? • Identify the Inputs and Outputs? • Write the algorithm? • Write the computer program? • Test our software and fix any bugs? • Sell our software to the public? • Question 2 – which stage do you think would: • Take the most time and money to fix an error? • Take the least time and money to fix an error?
Importance of Algorithms • I picked this subject to write computer programs – why should I study algorithms first? • Answer – the cost and time spent fixing errors increases exponentially (it multiplies) throughout the software development life cycle. • In other words, get the algorithm design as clear and as specific as you can, and save yourself a lot of time, work and heartache down the track when your software actually works!
Software Development Life Cycle – cost to fix errors 1. REQUIREMENTS 4. DEVELOPMENT TEST (You!) 2. DESIGN (algorithm) 5. ACCEPTANCE TEST (Client) 3. CODE (computer) 6. OPERATION (Public use)
In case you think we’re lying about all this... • 60 % of all errors in software development are made in the conception and design stage. 40 % are made during the coding, and are often a result of the errors made in the earlier stages… • 13% of all software projects are completed on time and on budget. • 60% of all software projects have time delays of over 20% of their original time proposals. • 15% of all software projects begun are abandoned before completion! • The instance of software failures greatly exceeds the instance hardware failures on any computer system anywhere in the world. • Of most software development projects, over 60% is spent on maintainability – ie. Providing updates for post-installation error correction.
What steps do I take to write an Algorithm? • Identify what we want to get out (i.e. the outputs) • Identify what need to go in (i.e. the inputs – often expressed as variables – more on this later) • Write the steps or processes within the algorithm • Test the algorithm and make any adjustments
Setting Out • Setting out is very important! • Make sure each line of the algorithm is set out evenly down the page • Make sure your writing is clear and succinct so that anyone could understand what you mean • Clearly identify the inputs and outputs before the algorithm • Check you have a Begin and End • Don’t try any shortcuts!
Exercises – Your turn! Do these in MS Word 1) Heat up a can of soup. 2) Find the average of four numbers. 3) Catch a bus. 4) Change the time in seconds to minutes. 5) Find the area of a circle. 6) Find the difference between two numbers. 7) Find the most runs scored from a list of 7 batsmen.
Introduction to Variables • Did your answer to number 5 on the previous look like this? INPUTS – radius of circle OUTPUTS – area of circle BEGIN Create variables called “radius of circle”, “squared radius” and “area of circle” Input “radius of circle” from the user Assign “squared radius” with the value of the “radius of circle” 2 (i.e. squared) Assign “area of circle” to the value of the “squared radius” * 3.14 (i.e. multiplied by 3.14) Output “area of circle” to the user. END • You may or may not have something similar – don’t worry! • This algorithm makes use of variables. What are they?
Variables??? Now I’m really confused! • A variable is a location in the computer’s memory that holds a particular value. Because values can (and often do) change (e.g. temperature, speed, etc) we use variables in our algorithms to indicate a value that may change. • Values are just data – meaning numbers or letters (or both). Because of these combinations, we usually tell the computer what types of datathe values are going to be (E.g. the number ‘5’ could be 5 percent, 5 hours, etc) • Some basic data types: • Integer – whole number, e.g. 5, 6, 9, -1, 10004 • Character – any letter, e.g. ‘r’, ‘A’, ‘x’, ‘3’ • Real – decimal numbers, e.g. 4.124, -12.5233 • Boolean – true or false • Currency, temperature, time/date….
A moment to reflect on variables… • The programmer to his son: "Here, I brought you a new basketball." • "Thank you, daddy, but where is the user guide?"
2 more tools to make your algorithms more efficient! (meaning easier!) • Without realising it, you have already used 1 of the 3 building blocks of algorithms – sequence! All sequence means is each process happens step by step, one after the other… you get the point! • The other 2 building blocks are: • Selection • Repetition (sometimes referred to as iteration) • Selection – makes choices in an algorithm, allows our program to “branch” • Repetition – repeats steps for us, so we don’t have to re-write the same steps over and over again!
Selection • Makes choices in our program. Look at the following program. Can you explain it? Input: light colour Output: action of car Begin Create variables “light colour”, “action of car” Input “light colour” If “light colour” = “red” Then “action of car” = “stop” Else If “light colour” = “yellow” Then “action of car” = “wait” Else “action of car” = “go” End If Output “action of car” End Question – why have I left out the line: If “light colour” = “green” Then… a) Will this algorithm still work? b) What is the benefit of leaving this out? Structure of a Selection Statement Note – you do not have to have an “else”! BEGIN Step A Step B IF <condition is true> THEN Step C Step D ELSE Step E Step F END IF END
Your turn! • Do 2_MoreShortExercises.doc • Questions 1-4!
Repetition • Repeats steps for us. • Skip ahead and do 3_GoingLoopy.doc right now!!! • After you have done that, go back and finish Questions 1-4 in 2_MoreShortExercises.doc
6 Ways to Evaluate an Algorithm • Correctness – does it do what it is supposed to do? • Lack of Errors – Is it free from logical errors that make it behave unnaturally? • User proofed – could it cope with heaps of different inputs and situations? Could I stuff it up if I really tried? • Efficiency – does it do what it is supposed to do in the quickest possible way, using the least number of steps? • Modularity – is it broken into small, easy to manage modules, with the main algorithm only using 7 (give or take 2) steps? • Flexibility – could it be modified / adapted to fit different uses? Eg. Could I use a “boiling the egg” algorithm to boil my 2 minute noodles?
Interface design • Very important! Overwhelming majority of end user software in industry is graphical. • Have a look at 4_UserInterfaceDesign.doc or search Google for your own set of criteria for good user interface design. • Rate these criteria in order of how important you think each one is. • User interface designs – use Microsoft Visio 2007 (in Microsoft Office family) • Windows controls available…
Interface Design exercises: • Design interfaces using Visio 07 for the following contexts: • An online calculator • A ticket machine at a bus station • A coke machine • A new media player for windows • A children’s maths game
Modular Design (when you have more then 7 steps – important if you want an “A” in ICT!) Input: toothpaste, toothbrush, water Output: clean teeth, waste froth MAIN Begin Call Prepare Implements Open Mouth Brush teeth for 2 mins Rinse mouth Rinse toothbrush End PREPARE IMPLEMENTS Begin Get toothbrush Get toothpaste Put toothpaste on toothbrush Wet toothbrush End
Project – Algorithm and Interface Design • Due before Easter holidays • Coming soon…