Lecture 12

21
CS1010E Lecture 12 Problem Solving Methodology Henry Chia [email protected] Semester 1 2011 / 2012 Department of Computer Science School Of Computing National University Of Singapore CS1010E Lecture 12 – p.1/21

description

l

Transcript of Lecture 12

Page 1: Lecture 12

CS1010E Lecture 12

Problem Solving Methodology

Henry Chia

[email protected]

Semester 1 2011 / 2012

Department of Computer Science

School Of Computing

National University Of Singapore

CS1010E Lecture 12 – p.1/21

Page 2: Lecture 12

Lecture Outline

General problem solving

Problem solving process

CS1010E Lecture 12 – p.2/21

Page 3: Lecture 12

Problem Solving

An entity, E, that solves a problem, P .

Input, I, presented to E to solve an instanceof the problem, P (I).

E outputs S as the solution of P (I).

How does E solve P for some general I?

CS1010E Lecture 12 – p.3/21

Page 4: Lecture 12

Problem Solving

It is not about whether you can solve theproblem, but...

Whether you can provide an algorithm (orrecipe) for E to solve the problem.

The algorithm is exact, effective, generaland must terminate.

The algorithm is constrained by:the language that E understands;the abstract mechanism behind how E

works.

CS1010E Lecture 12 – p.4/21

Page 5: Lecture 12

Language

What are the primitive values?int, double, char

boolean (represented using int)

Representation of problems using primitives,or complex structures involving primitives(e.g. structures, arrays and pointers).

Writing syntactically correct programelements or constructs.

Communication between modules (functioncalls and return values).

Type-awareness in a typed language.CS1010E Lecture 12 – p.5/21

Page 6: Lecture 12

Abstract Mechanism

Assignment

Expression evaluation: arithmetic, relational,boolean

Type conversions

Function call/activation/termination

Control flow (flow chart)if..else, switch..casewhile, do..while, for

Pointers, arrays/strings and structures

Recursive calls and activationsCS1010E Lecture 12 – p.6/21

Page 7: Lecture 12

Modeling

Within the set of possible programsconstructed using the rudimentary constructs,devise an abstract model that is complete,consistent and correct.

CompletenessAll possible constructs can be modeledprecisely;ConsistencyThe same construct is modeled in thesame way everytime;CorrectnessGives a correct representation of theinternal workings of the true mechanism.

CS1010E Lecture 12 – p.7/21

Page 8: Lecture 12

Maze Example

How do you solve this problem?

CS1010E Lecture 12 – p.8/21

Page 9: Lecture 12

Maze Example

“Always keep your left hand on the wall”

CS1010E Lecture 12 – p.9/21

Page 10: Lecture 12

Maze Example

Problem formulation from abstract descriptionto algorithm.

Algorithmic problem solving with control flow.SequenceSelectionRepetition

What is the input/output? How are theyrepresented?

How to represent movement using the“left-hand rule”?

CS1010E Lecture 12 – p.10/21

Page 11: Lecture 12

Problem Formulation

Get starting and ending positions;while (destination is not reached){

if (you can turn left)turn left;

elseif (you can go forward)

stay put;else

if (you can turn right)turn right;

elseturn back;

Move one step forward;}

CS1010E Lecture 12 – p.11/21

Page 12: Lecture 12

Problem Solving Process

CS1010E Lecture 12 – p.12/21

Page 13: Lecture 12

Problem Solving Process

Phase 1: Analysis – Understanding the problem

What are the inputs (or arguments)? Whatare the outputs (or results)?

What is the specification of the problem?What special conditions are there on theinputs and outputs?

Does the problem break into parts?

Draw a figure. Include suitable notations.

Write the algorithm.

CS1010E Lecture 12 – p.13/21

Page 14: Lecture 12

Problem Solving Process

Phase 2: Design – Devising a plan (algorithm)

Have you seen the problem before? Do youknow of a related problem?

Look at the specification. Find familiarproblems with the same or similarspecification.

Can you solve each part of the problem? Howdo the different parts interact with each other?

If you cannot solve the proposed problem tryto solve a related one. A more general one?A more special one? An analogous problem?

CS1010E Lecture 12 – p.14/21

Page 15: Lecture 12

Problem Solving Process

Phase 3: Implement – Carrying out the plan

Make sure that you check each step of thedesign.

Can you see clearly that each componentand each step does what it should?

Develop the program incrementally in stages.

You should also draw on other programs youhave written. Can they be used?

Can they be modified? Can they guide how tobuild the solution?

CS1010E Lecture 12 – p.15/21

Page 16: Lecture 12

Problem Solving Process

Phase 4: Testing – Looking back

Test each part as well as in its entirety.

Can you test that the solution works on avariety of arguments?

Can you think of how you might have solvedthe problem differently if you had to startagain?

Can you see how you might use solution (orpart of the solution) to solve another problem?

CS1010E Lecture 12 – p.16/21

Page 17: Lecture 12

Problem Solving Process

A great discovery solves a great problembut there is a grain of discovery in thesolution of any problem.

Your problem may be modest; but if itchallenges your curiosity and brings intoplay your inventive facilities, and if yousolve it by your own means, you mayexperience the tension and enjoy thetriumph of discovery.

George Polya — How to Solve It?

CS1010E Lecture 12 – p.17/21

Page 18: Lecture 12

Why is programming fun?

First is the sheer joy of making things. Asthe child delights in his mud pie, so theadult enjoys building things, especiallythings of his own design.

Second is the pleasure of making thingsthat are useful to other people. Deepwithin, we want others to use our workand to find it helpful.

CS1010E Lecture 12 – p.18/21

Page 19: Lecture 12

Why is programming fun?

Third is the fascination of fashioningcomplex puzzle-like objects of interlockingmoving parts and watching them work insubtle cycles, playing out theconsequences of principles built in fromthe beginning.

Fourth is the joy of always learning, whichsprings from the nonrepeating nature ofthe task. In one way or another theproblem is ever new, and its solver learnssomething: sometimes practical,sometimes theoretical, and sometimesboth.

CS1010E Lecture 12 – p.19/21

Page 20: Lecture 12

Why is programming fun?

Finally, there is the delight of working insuch a tractable medium. Theprogrammer, like the poet, works onlyslightly removed from pure thought-stuff.

Yet the program construct, unlike thepoet’s words, is real in the sense that itmoves and works, producing visibleoutputs separately from the constructitself.

CS1010E Lecture 12 – p.20/21

Page 21: Lecture 12

Why is programming fun?

Programming then is fun because it

gratifies creative longings built

deep within us and delights

sensibilities we have in common

with all men.

Frederick P. Brooks, Jr.The Mythical Man-Month

CS1010E Lecture 12 – p.21/21