Program design and problem solving techniques

dokka 19,107 views 49 slides Mar 10, 2012
Slide 1
Slide 1 of 49
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49

About This Presentation

Program design and problem solving techniques


Slide Content

1
Program Design &
Problem-Solving
Techniques
http://ashesh.ramjeeawon.info
Management Student/ Freelance Developer
University of Mauritius
Module Pre-Requisites:
None

2
Problem Solving – Program Design: Algorithm /Pseudocode
Today’s Session
Steps in program development
Algorithmic Problem Solving
How to write Pseudocode?
Meaningful names
Structure Theorem
Communication between modules
Module Cohesion and Coupling
Sequential File update

3
Problem Solving – Program Design: Algorithm /Pseudocode
Problem Solving - Big Problemo!!!

4
Problem Solving – Program Design: Algorithm /Pseudocode
How Do We Write a Program?
A computer is not intelligent. It cannot analyze a problem and come up with a
solution. A human (the programmer) must analyze the problem, develop the
instructions for solving the problem, and then have the computer carry out the
instructions.
To write a program for a computer to follow, we must go through a two-phase
process: problem solving and implementation

5
Problem Solving – Program Design: Algorithm /Pseudocode
Problem-Solving Phase
Analysis and Specification. Understand (define) the problem and
what the solution must do.
General Solution (Algorithm). Specify the required data types and
the logical sequences of steps that solve the problem.
Verify. Follow the steps exactly to see if the solution really does
solve the problem.

6
Problem Solving – Program Design: Algorithm /Pseudocode
Implementation Phase
Concrete Solution (Program). Translate the algorithm (the general
solution) into a programming language.
Test. Have the computer follow the instructions. Then manually
check the results. If you find errors, analyze the program and the
algorithm to determine the source of the errors, and then make
corrections.
Once a program has been written, it enters a third phase:
maintenance.

7
Problem Solving – Program Design: Algorithm /Pseudocode
Maintenance Phase
Use. Use the program.
Maintain. Modify the program to meet changing requirements or to
correct any errors that show up while using it.
The programmer begins the programming process by analyzing the
problem, breaking it into manageable pieces, and developing a
general solution for each piece called an algorithm. The solutions to
the pieces are collected together to form a program that solves the
original problem. Understanding and analyzing a problem take up
much more time than Figure 1.1 implies. They are the heart of the
programming process.

8
Problem Solving – Program Design: Algorithm /Pseudocode

9
Problem Solving – Program Design: Algorithm /Pseudocode
Compiler and Interpreters

10
Steps in program development
Ref:

11
Problem Solving – Program Design: Algorithm /Pseudocode
Steps in program development
1.Define the problem into three separate
components:
inputs
outputs
processing steps to produce required outputs.

12
Problem Solving – Program Design: Algorithm /Pseudocode
Steps in program development
1.Outline the solution.
Decompose the problem to smaller steps.
Establish a solution outline.
Initial outline may include:
major processing steps involved
major subtasks
user interface
major control structures
major variable and record structures
mainline logic

13
Problem Solving – Program Design: Algorithm /Pseudocode
Steps in program development
1.Develop the outline into an algorithm.
The solution outline is now expanded into an
algorithm.
What is an algorithm? – a set of precise steps that
describe exactly the tasks to be performed and the
order in which they are to be carried out.
Pseudocode will be used to represent the solution
algorithm

14
Problem Solving – Program Design: Algorithm /Pseudocode
1.Test the algorithm for correctness.
Very important in the development of a
program, but often forgotten
Major logic errors can be detected and
corrected at an early stage
Go through the algorithm step-by-step with
test data to ensure the program will actually
do what it is supposed to do.
Steps in program development

15
Problem Solving – Program Design: Algorithm /Pseudocode
1.Code the algorithm into a specific
programming language.
Start to code the program into a chosen
programming language after all design
considerations from Steps 1–4 are met.
Steps in program development

16
Problem Solving – Program Design: Algorithm /Pseudocode
1.Run the program on the computer.
This step uses a program compiler and
programmer-designed test data to machine-
test the code for
syntax errors
logic errors
Steps in program development

17
Problem Solving – Program Design: Algorithm /Pseudocode
1.Document and maintain the program.
Is really an ongoing task from the initial
definition of the problem to the final test
Documentation involves:
external documentation
internal documentation
Steps in program development

18
Problem Solving – Program Design: Algorithm /Pseudocode
Algorithmic Problem Solving

19
Problem Solving – Program Design: Algorithm /Pseudocode
An introduction to algorithms and pseudocode
What is an algorithm?
Lists the steps involved in accomplishing a task
(like a recipe)
Defined in programming terms as ‘a set of
detailed and ordered instructions developed to
describe the processes necessary to produce
the desired output from a given input’

20
Problem Solving – Program Design: Algorithm /Pseudocode
An introduction to algorithms and pseudocode
What is an algorithm?
An algorithm must:
Be lucid, precise and unambiguous
Give the correct solution in all cases
Eventually end

21
Problem Solving – Program Design: Algorithm /Pseudocode
An introduction to algorithms and pseudocode
What is pseudocode?
Structured English (formalised and abbreviated
to look like high-level computer language)

22
Problem Solving – Program Design: Algorithm /Pseudocode
How to write pseudocode
There are six basic computer operations:
1.A computer can receive information
2.A computer can put out information
3.A computer can perform arithmetic
4.A computer can assign a value to a variable or
memory location
5.A computer can compare two variables and
select one of two alternate actions
6.A computer can repeat a group of actions

23
Problem Solving – Program Design: Algorithm /Pseudocode
Exercise Time
Do some reading on the website below:
http://www.cs.dartmouth.edu/~fabio/teaching/cs4-winter07/notes/06_algo-intro.html

24
Problem Solving – Program Design: Algorithm /Pseudocode
Meaningful names
When designing an algorithm, a
programmer must introduce some unique
names which represents variables or
objects in the problem.
Names should be meaningful.
Names should be transparent to
adequately describe variables (Number1,
number2, etc.).

25
Problem Solving – Program Design: Algorithm /Pseudocode
Meaningful names
Underscore is used when using more than
one word (sales_tax or word_count).
Most programming language does not
tolerate a space in a variable as space
would signal the end of a variable name.
Another method is to use capital letters as
a word separator (salesTax or
wordCount).

26
Problem Solving – Program Design: Algorithm /Pseudocode
The Structure Theorem

27
Problem Solving – Program Design: Algorithm /Pseudocode
The Structure Theorem
1.Sequence
Straightforward execution of one processing
step after another
Represents the first four basic computer
operations
1.Receive information
2.Put out information
3.Perform arithmetic
4.Assign values

28
Problem Solving – Program Design: Algorithm /Pseudocode
The Structure Theorem
A typical sequence statement in an algorithm might
read:
Add 1 to pageCount
Print heading line1
Print heading line2
Set lineCount to zero
Read customer record
These instructions illustrate the sequence control
structure as a straightforward list of steps written one
after the other, in a top-to-bottom fashion

29
Problem Solving – Program Design: Algorithm /Pseudocode
The Structure Theorem
1.Selection
Presentation of a condition and the choice
between two actions, the choice depending
on whether the condition is true or false
Represents the decision-making abilities of
the computer
Illustrates the fifth basic computer operation –
compare two variables and select one of two
alternate actions

30
Problem Solving – Program Design: Algorithm /Pseudocode
The Structure Theorem
In pseudocode, selection is represented by the
keywords IF, THEN, ELSE and ENDIF
IF condition p is true THEN
statement(s) in true case
ELSE
statement(s) in false case
ENDIF
If condition p is true, then the statement in true case will
be executed, and the statement in the false case will be
skipped (vice versa)

31
Problem Solving – Program Design: Algorithm /Pseudocode
The Structure Theorem
1.Repetition
Presentation of a set of instruction to be
performed repeatedly, as long as the condition
is true
Block statement is executed again and again
until a terminating condition occurs
Illustrates the sixth basic computer operation
– to repeat a group of actions.

32
Problem Solving – Program Design: Algorithm /Pseudocode
The Structure Theorem
Written in pseudocode as:
DOWHILE condition p is true
statement block
ENDDO
DOWHILE is a leading decision loop – condition
is tested before any statements are executed
ENDDO triggers a return of control to the
retesting of the condition
Condition is true, statements are repeated until
condition is found false

33
Problem Solving – Program Design: Algorithm /Pseudocode
Exercise Time
In a complete program/ algorithm, show
the 3 concepts of Structure Theorem

34
Problem Solving – Program Design: Algorithm /Pseudocode
Necessary to consider flow of information
between modules
This flow of information is called
‘intermodule communication’ and can be
accomplished by the scope of the variable
Communication between modules

35
Problem Solving – Program Design: Algorithm /Pseudocode
Scope of a variable
The portion of a program in which that variable
has been defined and to which it can be
referenced
Variables can be global where the scope of the
variable is the whole program
Scope of the variable is simple the module
which it is defined
Communication between modules

36
Problem Solving – Program Design: Algorithm /Pseudocode
Global data
Date that can be used by all the modules in a
program
Every module in the program can access and
change data
Lifetime of a global variable spans the
execution of the whole program
Communication between modules

37
Problem Solving – Program Design: Algorithm /Pseudocode
Local data
Variable are defined within the submodule are
called local variables
The scope of a local variable is simply the
module in which it is defined
The lifetime of a local variable is limited to the
execution of the single submodule in which it is
defined
Communication between modules

38
Problem Solving – Program Design: Algorithm /Pseudocode
Exercise time
Show the difference between a global
variable and a local variable via the use of
a program, in any language of your
choice.

39
Problem Solving – Program Design: Algorithm /Pseudocode
Side effects
Side effect is a form of a cross-communication
of a module with other parts of a program,
Occurs when a subordinate module alters the
value of a global variable inside a module
Communication between modules

40
Problem Solving – Program Design: Algorithm /Pseudocode
Passing parameters
Parameters are simply data items
transferred from a calling module to its
subordinate module at the time of calling
To pass parameters between modules, two
things can happen:
The calling module must name the parameters
that it wants to pass to the submodule
The submodule must be able to receive those
parameters and return them to the calling module
if required
Communication between modules

41
Problem Solving – Program Design: Algorithm /Pseudocode
Formal and actual parameters
Parameters names that appear when a
submodule is defined are known as formal
parameters
Variables and expressions that are passed to a
submodule in a particular call are called actual
parameters
Communication between modules

42
Problem Solving – Program Design: Algorithm /Pseudocode
Value and reference parameters
Parameters may have one of three function:
1.To pass information from a calling module to a
subordinate module
2.To pass information from a subordinate module to
its calling module
3.To fulfil a two-way communication role
Communication between modules

43
Problem Solving – Program Design: Algorithm /Pseudocode
Value and reference parameters
Value parameters
Value parameters pass a copy of the value of a
parameter from one module to another
Reference parameters
Reference parameter pass the memory address of a
parameter from one module to another
Communication between modules

44
Problem Solving – Program Design: Algorithm /Pseudocode
Module cohesion
Cohesion is a measure of the internal
strength of a module
It indicates how closely the elements or
the statements of a module are associated
with each other
The more closely the elements of a
module are associated with each other,
the higher the cohesion of the module

45
Problem Solving – Program Design: Algorithm /Pseudocode
Module coupling
Coupling is a measure of the extent of
information interchange between
modules
Tight coupling implies large
dependence on the structure of one
module by another
Loose coupling is the opposite of tight
coupling. Modules with loose coupling
are more independent and easier to
maintain

46
Problem Solving – Program Design: Algorithm /Pseudocode
Sequential file update
Master file
Is a file that contains permanent and semi-
permanent information about the data
entities it contains
Transaction file
Contains all the data and activities that are
included on the master file. These are
transactions to:
Add a new record
Update or change an existing record
Delete an existing record

47
Problem Solving – Program Design: Algorithm /Pseudocode
Sequential file update
Sequential update logic
Sequential processing falls generally into
three categories:
1.If the key on the transaction record is less than the
key on the old master record, the transaction is
probably an add transaction
2.If they key on the transaction record is equal to the
key on the old master record, the transaction is
probably an update or delete transaction
3.If the key on the transaction record is greater that
the key on the old master record, there is not
matching transaction for that master record

48
Problem Solving – Program Design: Algorithm /Pseudocode
The End – Thank you for your kind attention
References:
Simple Program Design – A step by step
approach by L. A. Robertson

49
Problem Solving – Program Design: Algorithm /Pseudocode
http://ashesh.ramjeeawon.info
Management Student/ Freelance Developer
University of Mauritius
BCS Student Member