Download presentation

Presentation is loading. Please wait.

Published byJoy McLaughlin Modified about 1 year ago

1
Lecture 4 Problem solving with Algorithm, Flowcharts and Data Structure Structured Programming Instructor: Prof. K. T. Tsang

2
How to solve a problem Understand the problem Find the connection between the given data and the final solution Write down a plan to obtain the solution Carry out your plan Examine the solution obtained

3
Understand the problem Ask questions What do I know about the problem? What data/information I need? What does the solution looks like? What are the special cases? Have I solved similar problem before?

4
Strategies to find the solution Look for similarity If I have solved similar problem before, use similar solution. Divide and conquer Break up a large problem into smaller ones or one you already know how to solve. It is always easier to solve smaller or known problems.

5
Plan for the solution Carefully write down all the steps you need to do in order to solve the problem. Make sure your plan is complete and consistent. Consider all the possible outcomes from the intermediate steps and handle them correctly. In computer problem solving, this plan is called an Algorithm.

6
Algorithm 算法 A computing procedure (a finite set of well-defined instructions/steps) devised to accomplish some task or to solve some problem (i. e. given an initial state, following the procedure will lead to a desired end-state). This procedure/method is often independent of the computer system or programming language we use to solve the problem. To find simple and efficient algorithms that are easy to implement (i.e. to turn it into computer codes) is an important part of computer science. Often, the computational complexity of the algorithm depends on the use of suitable data structures (an organization of data involved in the problem).

7
Algorithm -2 Informally, the concept of an algorithm is often illustrated by the example of a recipe, although many algorithms are much more complex. Algorithms often have steps that repeat (iterate) or require decisions (such as logic or comparison). In most higher level programs, algorithms can have complex patterns, each using smaller and smaller sub-methods. In many programming languages, algorithms are implemented as functions or procedures.

8
Data structure A data structure is a way of storing data in a computer so that it can be used efficiently. Often a carefully chosen data structure will allow a more efficient algorithm to be used. An array-- a sequence of data structure. A binary tree --a simple type of branching linked data structure.

9
Flowcharts -- a schematic (graphical) representation of a process/procedure. A procedure is breakdown to its sequential steps. Each steps is represented symbolically and their relationship to other steps is shown schematically in a graph. They are commonly used in business/economic presentations to help the audience visualize the content better, or to find flaws in the process. In computer science, flowcharts are used to illustrate the steps in an algorithm.

10
Flowcharts -- an example A simple flowchart for what to do if a lamp doesn't work Lamp doesn’t work Lamp plugged in? Plug in lamp Replace bulb Bulb burned out? Buy new lamp no yes no

11
Flowcharts an example to test if N is a prime number Choose 0*
*

12
Pseudo-code A description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines or language-specific syntax.

13
Example of a pseudo-code: a recipe to make a cake Get all ingredients: butter, milk, flour, water, sugar, salt Heat butter to melt Mix melted butter, flour, milk, water, sugar and salt Mix all ingredients until it become a smooth paste Pour paste to a pre-greased container Preheat oven Put container to oven Bake for 30 minutes or until cake is brown outside Take container out and use a tooth-pick to check whether cake is done inside If cake not yet done, re-bake it for 5 minutes and check again until it is fully done

14
Example of a pseudo-code: to test if n is a prime number For all integer 2=*
*

15
Example of a pseudo-code: to test if n is a prime number For all integer 2=*
*

16
Example of a pseudo-code: find all prime numbers < N For all integers 2

17
Top-down design humans can only handle a relatively low level of complexity a program longer than 30/50 lines is already probably too complex top-down design is a method to decrease complexity of programming task by breaking it down into a few simpler relatively independent subtasks (other method names - stepwise refinement and divide and conquer) if a subtask is still complex, further subdivide to produce a hierarchy of tasks each simple subtask should be coded by a separate function or other separated piece of code added benefit - different people (or teams) can work on different subtasks

18
Example of top-down design Plan a party Invite the peoplePrepare food Make a list of guestCall the people Plan the menuShop for foodCook the food Main module (level 0) Level 1 Go to market Get in a car Select food DrivePark the car Level 2 Level 3 Level 4 Select the date

19
Example of top-down design Solve ax^2 + bx + c = 0 x1 = (-b+rd)/(2*a); x2 = (-b-rd)/(2*a) rd = sqrt(D) D= b*b -4*a*c

20
Example of top-down design Find Pythagoras triplet Pick a pair (i,j) such that 0*0 is an integer yes no
*

21
Procedural Approach for computer problem solving Sketch the overall computation at a high level in human languages, producing a pseudo-code description of the computation (with flowchart) Identify central data structures: what data will be needed throughout the entire computation Identify procedures: sub-problems in the overall computation Determine inputs and output of each procedure Translate pseudo-code into code: sub-problems become calls to methods which solve individual sub-problems Add methods for sub-problems Repeat previous steps for each stub method

22
Example Here is an example problem that we can use in the discussion of top-down design. read in a sequence of temperatures compute the average temperature determine if the temperatures are strictly increasing determine the largest daily increase in temperature

23
Solving the example problem by top-down design Sketch pseudo-code. In this case, the problem description can serve as the pseudo-code. prompt user for number of days read temperature for each day find the average temperature and print it determine if temperatures are strictly increasing, if so print message determine largest daily increase and print it

24
Identify main data structures In this case, the main data used by the entire computation is the sequence of temperatures for each day. We can represent an individual temperature using a floating point number, and the overall sequence can be an array of number. So, this is our central data structure: an array of floating point values.

25
Identify procedures The name of each method should directly reflect its purpose in the overall computation: for example: promptUserForNumDays readTemperatures findAverageTemperature determineIfStrictlyIncreasing determineLargestDailyIncrease

26
Programming as problem solving Define the problem - what is the input and output? - any constraints to be satisfied? - what information is essential? Develop a solution - construct the algorithm (steps lead to the answer) - implement the algorithm in a program (computer code) Compile, test and debug the program Document - give description of solution and explain it Maintain the program - test and revise the code as needed - improve the algorithm to make it more efficient/faster

27
What makes a good program? Correctness –Meets the problem requirements –Produces correct results Easy to read and understand Easy to modify Easy to debug Efficient –Fast –Requires less memory

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google