Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPS 120: Introduction to Computer Science

Similar presentations


Presentation on theme: "CPS 120: Introduction to Computer Science"— Presentation transcript:

1 CPS 120: Introduction to Computer Science
Algorithm Design and Problem Solving

2 Session Goals Determine whether a problem is suitable for a computer solution Describe the computer problem-solving process Distinguish between following an algorithm and developing one Apply top-down design methodology to develop an algorithm to solve a problem

3 The Program Development Cycle
Analyze the problem Design the solution algorithm Design the user interface Write the code Test and debug the program Complete the documentation

4 Problem Solving Problem solving is the act of finding a solution to a perplexing, distressing, vexing, or unsettled question

5 Problem Solving In 1945 George Polya wrote How to Solve It: A New Aspect of Mathematical Method His How to Solve It list is quite general Written in the context of solving mathematical problems The list becomes applicable to all types of problems

6 Ask Questions... …to understand the problem
What do I know about the problem? What is the information that I have to process in order the find the solution? What does the solution look like? What sort of special cases exist? How will I recognize that I have found the solution? Flowcharting and Pseudocode

7 Look for Familiar Things
You should never reinvent the wheel In computing, you see certain problems again and again in different guises A good programmer sees a task, or perhaps part of a task (a subtask), that has been solved before and plugs in the solution Functions and subroutines

8 Divide and Conquer Break up a large problem into smaller units that we can handle Applies the concept of abstraction The divide-and-conquer approach can be applied over and over again until each subtask is manageable Modularization

9 Computer Problem-Solving
The computer problem-solving process

10 The Interactions Between Problem-Solving Phases

11 The Program Development Cycle
Analyze the problem Design the solution algorithm Design the user interface Write the code Test and debug the program Complete the documentation

12 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.

13 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++, Visual Basic, COBOL, Java, etc. The logical sequence of steps

14 Algorithms An algorithm is set of instructions for solving a problem or sub-problem in a finite amount of time using a finite amount of data The instructions are unambiguous

15 Following an Algorithm
Preparing a Hollandaise sauce

16 Following an Algorithm (cont.)
Preparing a Hollandaise sauce

17 Programs are Solutions to Problems
Programmers arrive at these solutions by using one or more of these devices: Structured Programming Structure charts Logic flowcharts Pseudocode (sue-dough'-code) Solutions to problems need to be developed before code is written

18 Developing an Algorithm
The plan must be suitable in a suitable form Two methodologies that currently used Top-down design Object-oriented design

19 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.

20 Top-Down Design Breaking the problem into a set of sub-problems called modules Creating a hierarchical structure of problems and sub-problems

21 Structure Charts Structure charts illustrate the structure of a program by showing independent hierarchical steps. Major divisions are subdivided into smaller pieces of information.

22 Top-Down Design An example of top-down design This process continues for as many levels as it takes to expand every task to the smallest details A step that needs to be expanded is an abstract step

23 A General Example Planning a large party
Subdividing the party planning

24 Flowcharts and Pseudocode
Forms of documentation used to build and communicate the detailed parts your structured designs

25 Flowchart A graphical representation of an algorithm.

26 Pseudocode Uses a mixture of English and formatting to make the steps in the solution explicit

27 Flowcharts & Pseudocode are Important
A graphical layout of the algorithm is often very useful in spotting “illogical” logic! Pseudocode – Make a detailed description of your algorithm’s logic before worrying about 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

28 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

29 Logic Flowcharts These represent the flow of logic in a program and help programmers “see” program design.

30 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

31 How to Draw a Flowchart Five steps which can be used as a guide for completing flowcharts. Start with a 'trigger' event (it may be the beginning of the program) Initialize any values that need to be defined at the start of the program Note each successive action concisely and clearly Go with the main flow (put extra detail in other charts -- this is the basis of structured programming) Follow the process through to a useful conclusion (end at a 'target' point -- like having no more records to process)

32 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

33 Sample Program Flowchart
Terminal Symbol Start Initialize Variables Preparation Symbol or Variables = I/O Symbol Open Files Read a Record No More items? Process Record (Total Time) Close Files Decision Symbol Stop Yes Process Record (Detail Time) Process Symbol Write Record

34 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

35 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.

36 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.

37 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

38 Writing Pseudocode You need to reach a balance between excessive and insufficient detail. Write only what is necessary to understand and communicate the essential parts of your algorithm

39 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

40 Pseudocode for a Generalized Program
START Intialize variables LOOP While More records do READ record PROCESS record PRINT detail record ENDLOOP CALCULATE TOTALS PRINT total record END

41 Rules for Pseudocode Make the pseudocode language-independent
Indent lines for readability Make keywords 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 TERMINATE all routines with an END instruction

42 A Computer Example Problem
Create an address list that includes each person’s name, address, telephone number, and address This list should then be printed in alphabetical order The names to be included in the list are on scraps of paper and business cards

43 A Computer Example

44 A Computer Example

45 A Computer Example

46 A Computer Example

47 A Computer Example

48 Relation of Flowcharts & Pseudocode

49 The Program Development Cycle
Analyze the problem Design the solution algorithm Design the user interface Write the code Test and debug the program Complete the documentation

50 Data Processing Facts enter the computer They are:
Checked for accuracy Organized Calculated with Stored


Download ppt "CPS 120: Introduction to Computer Science"

Similar presentations


Ads by Google