You are on page 1of 6

Problem Solving

CS1010E Lecture 12
Problem Solving Methodology
Henry Chia
hchia@comp.nus.edu.sg

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


Semester 1 2011 / 2012

Input, I, presented to E to solve an instance


of the problem, P (I).

Department of Computer Science


School Of Computing

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

National University Of Singapore

How does E solve P for some general I?


CS1010E Lecture 12 p.1/21

CS1010E Lecture 12 p.3/21

Problem Solving

Lecture Outline
General problem solving

It is not about whether you can solve the


problem, but...

Problem solving process

Whether you can provide an algorithm (or


recipe) for E to solve the problem.
The algorithm is exact, effective, general
and must terminate.
The algorithm is constrained by:
the language that E understands;
the abstract mechanism behind how E
works.
CS1010E Lecture 12 p.2/21

CS1010E Lecture 12 p.4/21

Language

Modeling

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 program
elements or constructs.
Communication between modules (function
calls and return values).
Type-awareness in a typed language.
CS1010E Lecture 12 p.5/21

Within the set of possible programs


constructed using the rudimentary constructs,
devise an abstract model that is complete,
consistent and correct.
Completeness
All possible constructs can be modeled
precisely;
Consistency
The same construct is modeled in the
same way everytime;
Correctness
Gives a correct representation of the
internal workings of the true mechanism.

CS1010E Lecture 12 p.7/21

Maze Example

Abstract Mechanism
Assignment
Expression evaluation: arithmetic, relational,
boolean
Type conversions
Function call/activation/termination
Control flow (flow chart)
if..else, switch..case
while, do..while, for
Pointers, arrays/strings and structures

How do you solve this problem?

Recursive calls and activations


CS1010E Lecture 12 p.6/21

CS1010E Lecture 12 p.8/21

Maze Example

Problem Formulation
Get starting and ending positions;
while (destination is not reached)
{
if (you can turn left)
turn left;
else
if (you can go forward)
stay put;
else
if (you can turn right)
turn right;
else
turn back;

Always keep your left hand on the wall


CS1010E Lecture 12 p.9/21

Maze Example

Move one step forward;


}
CS1010E Lecture 12 p.11/21

Problem Solving Process

Problem formulation from abstract description


to algorithm.
Algorithmic problem solving with control flow.
Sequence
Selection
Repetition
What is the input/output? How are they
represented?
How to represent movement using the
left-hand rule?
CS1010E Lecture 12 p.10/21

CS1010E Lecture 12 p.12/21

Problem Solving Process


Phase 1: Analysis Understanding the problem

Problem Solving Process


Phase 3: Implement Carrying out the plan

What are the inputs (or arguments)? What


are the outputs (or results)?

Make sure that you check each step of the


design.

What is the specification of the problem?


What special conditions are there on the
inputs and outputs?

Can you see clearly that each component


and each step does what it should?

Does the problem break into parts?

You should also draw on other programs you


have written. Can they be used?

Draw a figure. Include suitable notations.


Write the algorithm.

Develop the program incrementally in stages.

Can they be modified? Can they guide how to


build the solution?
CS1010E Lecture 12 p.13/21

Problem Solving Process


Phase 2: Design Devising a plan (algorithm)
Have you seen the problem before? Do you
know of a related problem?
Look at the specification. Find familiar
problems with the same or similar
specification.
Can you solve each part of the problem? How
do the different parts interact with each other?
If you cannot solve the proposed problem try
to solve a related one. A more general one?
A more special one? An analogous problem?
CS1010E Lecture 12 p.14/21

CS1010E Lecture 12 p.15/21

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 a
variety of arguments?
Can you think of how you might have solved
the problem differently if you had to start
again?
Can you see how you might use solution (or
part of the solution) to solve another problem?

CS1010E Lecture 12 p.16/21

Problem Solving Process


A great discovery solves a great problem
but there is a grain of discovery in the
solution of any problem.
Your problem may be modest; but if it
challenges your curiosity and brings into
play your inventive facilities, and if you
solve it by your own means, you may
experience the tension and enjoy the
triumph of discovery.
George Polya How to Solve It?
CS1010E Lecture 12 p.17/21

Why is programming fun?

Why is programming fun?


Third is the fascination of fashioning
complex puzzle-like objects of interlocking
moving parts and watching them work in
subtle cycles, playing out the
consequences of principles built in from
the beginning.
Fourth is the joy of always learning, which
springs from the nonrepeating nature of
the task. In one way or another the
problem is ever new, and its solver learns
something: sometimes practical,
sometimes theoretical, and sometimes
both.
CS1010E Lecture 12 p.19/21

Why is programming fun?

First is the sheer joy of making things. As


the child delights in his mud pie, so the
adult enjoys building things, especially
things of his own design.

Finally, there is the delight of working in


such a tractable medium. The
programmer, like the poet, works only
slightly removed from pure thought-stuff.

Second is the pleasure of making things


that are useful to other people. Deep
within, we want others to use our work
and to find it helpful.

Yet the program construct, unlike the


poets words, is real in the sense that it
moves and works, producing visible
outputs separately from the construct
itself.

CS1010E Lecture 12 p.18/21

CS1010E Lecture 12 p.20/21

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

You might also like