+ All Categories
Home > Documents > Lecture 4 Problem solving with Algorithm, Flowcharts and Data Structure Structured Programming...

Lecture 4 Problem solving with Algorithm, Flowcharts and Data Structure Structured Programming...

Date post: 16-Dec-2015
Category:
Upload: joy-mclaughlin
View: 226 times
Download: 0 times
Share this document with a friend
Popular Tags:
27
Lecture 4 Problem solving with Algorithm, Flowcharts and Data Structure Structured Programming Instructor: Prof. K. T. Tsang
Transcript
Page 1: Lecture 4 Problem solving with Algorithm, Flowcharts and Data Structure Structured Programming Instructor: Prof. K. T. Tsang.

Lecture 4

Problem solving with Algorithm, Flowcharts

and Data Structure

Structured Programming Instructor: Prof. K. T. Tsang

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

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

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

Understand the problemAsk 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?

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

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.

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

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.

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

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

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

Algorithm -2Informally, 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.

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

Data structureA 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.

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

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.

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

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 bulbBulbburned out?

Buy new lamp

no

yes

yes

no

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

Flowcharts an example to test if N is a prime number

Choose 0<i<NFind remainder r=N%i

r=0? N is not a prime

Choose another iIs there other i<N

not tested?

N is a prime

no

yes

yes

no

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

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.

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

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

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

Example of a pseudo-code: to test if n is a prime number

For all integer 2=<i<n• Begin loop

– Find r=n%i– If (r=0) n is not a prime; return or exit;– else continue

• End loopn is a prime; return or exit;

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

Example of a pseudo-code: to test if n is a prime number

For all integer 2=<i<n• Begin loop

– Find r=n%i– If (r=0) n is not a prime; return 0;– else continue

• End loopn is a prime; return 1;

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

Example of a pseudo-code: find all prime numbers < N

For all integers 2<n<N

•Begin loop–Test if n is a prime number

•End loop

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

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

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

Example of top-down design

Plan a party

Invite the people Prepare food

Make a list of guest Call the people

Plan the menu Shop for food Cook the food

Main module (level 0)

Level 1

Go to market

Get in a car

Select food

Drive Park the car

Level 2

Level 3

Level 4

Select the date

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

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

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

Example of top-down design

Find Pythagoras triplet

Pick a pair (i,j) such that 0<i<N, 0<j<N

Find s2=i*i + j*j

Check if s2=k*k, where k>0 is an integer

yes

no

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

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

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

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

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

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

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

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.

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

Identify procedures

The name of each method should directly reflect its purpose in the overall computation: for example:

• promptUserForNumDays readTemperatures findAverageTemperature determineIfStrictlyIncreasing determineLargestDailyIncrease

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

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

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

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


Recommended