1 / 24

How Are Algorithms Developed?

How Are Algorithms Developed?. CPS120: Introduction to Computer Science Lecture 6. Algorithms.

amberwilson
Télécharger la présentation

How Are Algorithms Developed?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How Are Algorithms Developed? CPS120: Introduction to Computer Science Lecture 6

  2. Algorithms • Muhammad 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 problem • Two parts • Actions to be executed • Order in which those actions are to be done • Computational steps that transform the input data into useful output data. • Algorithms are not programs • They 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 flowcharts • Structure charts • Pseudocode (sue-dough'-code) • Structured Programming • Solutions to problems need tobe developed before code is written

  5. Writing C++ (Source) Code • C++ syntax can obscure understanding • Write out solutions intuitively – on paper • Use flowcharts or pseudocode • Translate ideas to C++ syntax on paper • Try to NEVER compose code at the terminal • Don't try to crate the perfect solution on the first try – it's too difficult • Begin with a partial solution that runs • Make it better iteratively through refinement • Prototyping

  6. Design Philosophy • Spend a little more on an organized software development approach to save a lot during testing, maintenance, and support phases

  7. Pseudocode & Flowcharts are Important • Pseudocode – • 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 language • Pseudocode is generally independent of the implementation language • Flowcharts – • 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. Flowchart • A 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 do • Flowcharts serve as program documentation • Flowcharts allow a programmer to test alternative solution to a problem before coding • Flowcharts provide a method for easy desk checking

  11. Logic Flowcharts • These 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 variable assignment or mathematical operation. With a heading – an internal subroutine Decision. The diamond indicates a decision structure. A diamond always has two flow 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 externally Connector. Connectors avoid crossing flow lines, making the flowchart easier to read. Connectors indicate where flow lines are connected. Connectors come in pairs, one with a flow line in and the other with a flow line out. Off-page connector. Even fairly small programs can have flowcharts that extend several pages. The off-page connector indicates the continuation of the flowchart on another page. 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 right • Draw the flowchart the way you like to read • Use arrowheads on flow lines whenever the flow is not top to bottom, left to right • Be neat ! Use graphics software • Avoid intersecting lines

  14. Start sum= Input price sum=sum+price More items? Yes No tax=sum x 0.0725 total=sum+tax Output sum, tax, and total Stop Flowchart for aCash Register Program Terminal Symbol or Preparation Symbol I/O Symbol Process Symbol Decision Symbol

  15. Disadvantages to Flowcharts • Time consuming • A program flowchart shows how the input becomes output, but it does not show why a particular step is done • Flowcharts 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. Pseudocode • This 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 pseudocode • The rules of Pseudocode are generally straightforward • Should be easily read and understood by non-programmers • All statements showing "dependency" are to be indented. • These include while, do, for, if, switch

  19. Writing Pseudocode • You 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 algorithm • Write 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 language • Each instruction is started on a separate line • Logic-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 structure • Instructions are written from top to bottom, with only one entry point and one exit point • Logically related groups of instructions can be formed into modules and given a name

  21. Pseudocode for a Cash Register Program sum=0 While More items do Input price sum=sum+price End While tax=sum x 0.0725 total=sum+tax Output sum, tax, total

  22. Rules for Pseudocode • Make the pseudocode language-independent • Indent lines for readability • Make key words stick out by showing them capitalized, in a different color or a different font • Punctuation is optional • End every IF with ENDIF • Begin loop with LOOP and end with ENDLOOP • Show MAINLINE first; all others follow • TERMINAE 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 Charts • Structure charts illustrate the structure of a program by showing independent hierarchical steps. • Major divisions are subdivided into smaller pieces of information.

More Related