Presentation on theme: "How Are Algorithms Developed?"— Presentation transcript:
1 How Are Algorithms Developed? CPS120:Introduction to Computer ScienceLecture 6
2 AlgorithmsMuhammad ibn Musa Al'Khowarizmi, a Tashkent cleric who in the twelfth century developed the concept of a written process to be followed to achieve some goal, and published a book on the subject that gave it is modern name -- algorithm.
3 What is an Algorithm?An algorithm is merely the sequence of steps taken to solve a problemTwo partsActions to be executedOrder in which those actions are to be doneComputational steps that transform the input data into useful output data.Algorithms are not programsThey need to be coded in a programming language like C++
4 Programs are Solutions to Problems Programmers arrive at these solutions by using one or more of these devices:Logic flowchartsStructure chartsPseudocode (sue-dough'-code)Structured ProgrammingSolutions to problems need to be developed before code is written
5 Writing C++ (Source) Code C++ syntax can obscure understandingWrite out solutions intuitively – on paperUse flowcharts or pseudocodeTranslate ideas to C++ syntax on paperTry to NEVER compose code at the terminalDon't try to crate the perfect solution on the first try – it's too difficultBegin with a partial solution that runsMake it better iteratively through refinementPrototyping
6 Design PhilosophySpend a little more on an organized software development approach to save a lot during testing, maintenance, and support phases
7 Pseudocode & Flowcharts are Important Make a detailed description of your algorithm’s logic before worrying about C++ syntax and data layout.An algorithm you develop using pseudocode should be capable of implementation in any procedural programming languagePseudocode is generally independent of the implementation languageFlowcharts –A graphical layout of the algorithm is often very useful in spotting “illogical” logic!
8 Flowcharts and Pseudocode Forms of documentation used to build and communicate the detailed parts your structured designs
9 FlowchartA graphical representation of an algorithm.
10 Reasons Programmers Draw Flowcharts Drawing a flowchart gives the programmer a good visual reference of what the program will doFlowcharts serve as program documentationFlowcharts allow a programmer to test alternative solution to a problem before codingFlowcharts provide a method for easy desk checking
11 Logic FlowchartsThese represent the flow of logic in a program and help programmers “see” program design.
12 Common Flowchart Symbols Terminator. Shows the starting and ending points of the program. A terminator has flow lines in only one direction, either in (a stop node) or out (a start node).Data Input or Output. Allows the user to input data and results to be displayed.Processing. Indicates an operation performed by the computer, such as a variableassignment or mathematical operation. With a heading – an internal subroutineDecision. The diamond indicates a decision structure. A diamond always has twoflow lines out. One flow lineout is labeled the “yes” branch and the other is labeled the“no” branch.Predefined Process. One statement denotes a group of previously defined statements.Such as a function or a subroutine created externallyConnector. Connectors avoid crossing flow lines, making the flowchart easier to read.Connectors indicate where flow lines are connected. Connectors come in pairs, one witha flow line in and the other with a flow line out.Off-page connector. Even fairly small programs can have flowcharts that extend severalpages. The off-page connector indicates the continuation of the flowchart on anotherpage. Just like connectors, off-page connectors come in pairs.Flow line. Flow lines connect the flowchart symbols and show the sequence of operations during the program execution.Common Flowchart Symbols
13 Rules for Drawing Flowcharts Top to bottom and left to rightDraw the flowchart the way you like to readUse arrowheads on flow lines whenever the flow is not top to bottom, left to rightBe neat ! Use graphics softwareAvoid intersecting lines
14 Flowchart for a Cash Register Program Terminal SymbolStartsum=Input pricesum=sum+priceMoreitems?tax=sum xtotal=sum+taxOutput sum, tax,and totalStopNoYesorPreparation SymbolI/O SymbolProcess SymbolDecision Symbol
15 Disadvantages to Flowcharts Time consumingA program flowchart shows how the input becomes output, but it does not show why a particular step is doneFlowcharts are subjective
16 What is Pseudocode?Pseudocode is an artificial and informal language that helps programmers develop algorithms.Pseudocode is a "text-based" detail (algorithmic) design tool.An English description of an algorithm in sufficient detail to allow its implementation to be easily written.
17 PseudocodeThis device is not visual but is considered a “first draft” of the actual program.Pseudocode is written in the programmer’s native language and concentrates on the logic in a program—not the syntax of a programming language.
18 General Rules for Pseudocode There is no standard pseudocodeThe rules of Pseudocode are generally straightforwardShould be easily read and understood by non-programmersAll statements showing "dependency" are to be indented.These include while, do, for, if, switch
19 Writing PseudocodeYou need to reach a balance between excessive and insufficient detail.In some cases you only need a skeleton of the logic and in other cases you may want to show the intricacies of your algorithmWrite what is necessary to understand and communicate the essential parts of your algorithm without becoming bogged down in detail
20 Pseudocode Statement Rules Statements are written in a simple English-like languageEach instruction is started on a separate lineLogic-showing keywords are written in UPPER CASE or typed in BOLD UPPERCASE(e.g. IF, THEN, FOR, DO etc.)These are the only uppercase words in this form of pseudocode.Indentation is used to show structureInstructions are written from top to bottom, with only one entry point and one exit pointLogically related groups of instructions can be formed into modules and given a name
21 Pseudocode for a Cash Register Program sum=0While More items doInput pricesum=sum+priceEnd Whiletax=sum xtotal=sum+taxOutput sum, tax, total
22 Rules for Pseudocode Make the pseudocode language-independent Indent lines for readabilityMake key words stick out by showing them capitalized, in a different color or a different fontPunctuation is optionalEnd every IF with ENDIFBegin loop with LOOP and end with ENDLOOPShow MAINLINE first; all others followTERMINAE all routines with an END instruction
23 Structured Programming Structured program languages lend themselves to flowcharts, structure charts, and pseudocode.Structured programming languages work best where the instructions have been broken up into small, manageable parts.
24 Structure ChartsStructure charts illustrate the structure of a program by showing independent hierarchical steps.Major divisions are subdivided into smaller pieces of information.