total

DCT 1123
Problem Solving & Algorithm
Chapter 2 & 3:
Problem Solving & Problem Analysis
Contents
Algorithms Design Strategies
 Defining the problem
 Designing a solution algorithms
 Checking the solution algorithm

Algorithm Design Strategies
Step 1: Investigation step
i. Identify the process
ii. Identify the major decision
iii. Identify the loops
iv. Identify the variable
Algorithm Design Strategies
Step 2: Preliminary algorithm step
i. Devise a high level algorithm
ii. Step through the algorithm. Does this
“walk-through” reveal any major
problem? If it does, correct the problem
Algorithm Design Strategies
Step 3: Refining the algorithm step
i. Incorporate any refinements indicated in
step 2
ii. Group together processes where
appropriate
iii. Group together where appropriate
iv. Test the algorithm again by stepping
through it
Stepwise Refinement



aka Top Down Approach
A way of developing a computer program by
first describing general functions, then
breaking each function down into details
which are refined in successive steps until
the whole program is fully defined.
Stepwise refinement was first introduced by
Wirth in 1971, applying it to pseudo-code,
flowchart, block diagrams, formal
specifications and used in every phase of
software development.
Stepwise Refinement
Example:
 Brush Teeth

◦ find toothbrush
◦ find toothpaste tube
◦ open toothpaste tube



Put thumb and pointer finger on cap
turn fingers counter-clockwise
repeat prior step until cap falls off
◦ squeeze tube onto toothbrush

(details omitted)
◦ clean teeth



put brush on teeth
move back and fourth vigorously
repeat above step 100 times
◦ clean up

rinse brush
 turn on water
 put head of brush under running water for 30 seconds
 turn off water


put cap back on toothpaste
put all items back in cabinet
Variable




Let us think that I ask you to retain the number 5 in
your mental memory, and then I ask you to memorize
also the number 2 at the same time.
You have just stored two different values in your
memory. Now, if I ask you to add 1 to the first
number I said, you should be retaining the numbers 6
(that is 5+1) and 2 in your memory.
Values that we could now -for example- subtract and
obtain 4 as result.
The whole process that you have just done with your
mental memory is a similar of what a computer can
do with two variables.
Variable
a = 5; b = 2;
 a = a + 1;
 result = a - b;

Variable
Obviously, this is a very simple example
since we have only used two small integer
values, but consider that your computer
can store millions of numbers like these
at the same time and conduct
sophisticated mathematical operations
with them.
 Therefore, we can define a variable as a
portion of memory to store a determined
value.

Data Types
When programming, we store the variables in our
computer's memory, but the computer has to know
what kind of data we want to store in them, since it is
not going to occupy the same amount of memory to
store a simple number than to store a single letter or
a large number, and they are not going to be
interpreted the same way.
 The memory in our computers is organized in bytes.
 A byte is the minimum amount of memory that we
can manage in C++. A byte can store a relatively small
amount of data: one single character or a small
integer (generally an integer between 0 and 255).

Data Types
Name
Description
Size*
Range*
char
Character or small integer.
1byte
signed: -128 to 127
unsigned: 0 to 255
short int
(short)
Short Integer.
2bytes
signed: -32768 to 32767
unsigned: 0 to 65535
4bytes
signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
4bytes
signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
int
Integer.
long int
(long)
Long integer.
bool
Boolean value. It can take one
1byte
of two values: true or false.
true or false
float
Floating point number.
4bytes
+/- 3.4e +/- 38 (~7 digits)
double
Double precision floating
point number.
8bytes
+/- 1.7e +/- 308 (~15 digits)
Developing an algorithm

In developing the algorithm, the following
process must be followed:
1. Defining the problem
2. Designing a solution algorithm
3. Checking the solution algorithm
Defining the problem
This step involves carefully reading and
rereading the problem until you
understand completely what is required
 Additional information will need to be
sought to help resolve any ambiguities or
deficiencies in the problem specification

Defining the problem

The problem should be divided into three
separate components:
1. Input: a list of source data provided to the
problem
2. Output: a list of the outputs required
3. Processing: a list of actions needed to
produce the required output
Defining the problem
When reading the problem statement, the
input and output components are easily
identified, because they use descriptive
words such as nouns and adjectives
 The problem statement usually describes
the processing steps as actions, using
verbs and adverbs

Defining the problem

Example:A program is required to read three numbers,
add them together and print their total
◦ Tackle this problem in two stages. First underline the
nouns and adjectives used in the specification.This will
establish the input and output components, as well as any
objects required.
Defining the problem
A program is required to read three numbers, add
them together and print their total
◦ By looking at the underlined nouns and
adjectives, it is easy to see that the input for this
problem is three numbers and the output is the
total
◦ It is helpful to transform this components in a
simple diagram, called a defining diagram
Defining the problem
Input
Number1
Number2
Number3
Defining Diagram
Processing
Output
Total
Defining the problem
◦ Second, underline (in a different color) the verbs
and adverbs used in the specification
A program is required to read three numbers, add
them together and print their total
◦ By looking at the underlined words, it can be
seen that the processing verbs are ‘read’, ‘add
together’ and ‘print’.
◦ These steps can be added to our defining
diagram
Defining the problem
Input
Processing
Output
Number1
Read three numbers
Total
Number2
Add numbers together
Number3
Print total number
Defining Diagram

Now all the nouns and verbs in the
specification have been considered and
the defining diagram is complete
Defining the problem
When it comes to writing down the
processing steps in an algorithm, use
words that describe the work to be done
in terms of single specific tasks or
functions
 For example:

Read three numbers
Add numbers together
Print total number
Designing a solution algorithm
Designing a solution algorithm is the most
challenging task in the program life cycle.
 The first attempt at designing a solution
algorithm usually does not result in a
finished product
 Pseudocode is useful in this trial and
error process, since it is relatively easy to
add, delete or alter

Designing a solution algorithm
The defining diagram that we have created in
the previous slide shows what is required, and a
simple calculation will establish how
 Using pseudocode and the sequence control
structure the algorithm can be established as
follows:

Add three numbers
Read number1, number2, number3
Total = number1 + number2 + number3
Print total
END
Designing a solution algorithm

There are a number of points to consider in
this solution algorithm:
◦ A name has been given to the algorithm,
add_three_numbers
◦ An END statement at the end of the algorithm
indicates that the algorithm is complete
◦ All processing steps between the algorithm name and
the END statement have been indented for
readability
◦ Each processing step in the defining diagram relates
directly to one or more statements in the algorithm
Designing a solution algorithm
Start
Input number 1, number 2, number 3
Total = number 1 + number 2 + number 3
Display Total
Stop
Checking the solution algorithm
After a solution algorithm has been
established, it must be tested for
correctness
 This step is necessary because most
major logic errors occur during the
development of the algorithm
 It is much easier to detect errors in the
pseudocode than in the corresponding
program code

Checking the solution algorithm
Desk checking involves tracing through
the logic of the algorithm with some
chosen test data
 It is a walkthrough the logic of the
algorithm exactly as a computer would
 This not only help detect error early, but
also helps you to become familiar with
the way the program runs

Selecting test data
When selecting the test data to desk
check an algorithm, look at the program
specification and choose simple test cases
that are based on the requirements
 To desk check the algorithm, you need
only few simple test cases that will follow
the major paths of the algorithm logic

Steps in desk checking
1.
2.
3.
4.
5.
6.
Choose simple input test cases that are valid. Two or three
test cases are usually sufficient
Establish what the expected results
Make a table on a piece of paper of the relevant variable
names within the algorithm
Walk the first test case through the algorithm, line by line,
keeping a step-by-step record of the contents of each
variable in the table as the data passes through the logic
Repeat the walkthrough process using the other test data
cases, until the algorithm has reached its logical end
Check that the expected result established in Step 2
matches the actual result developed in Step 5
Example of desk check
1.
2.
Choose 2 sets of input test data. The three numbers
selected will be 10, 20 and 30 for the first test case
and 40, 41 and 42 for the second test case
First data set
Second data set
Number1
10
40
Number2
20
41
number3
30
42
Establish the expected result for each test case
total
First Data Set
Second Data Set
60
123
Example of desk check
3.
Choose 2 sets of input test data. The three numbers
selected will be 10, 20 and 30 for the first test case
and 40, 41 and 42 for the second test case
Statement number1
number2
number3
20
30
total
First Pass
1
10
2
60
3
Print
Second Pass
1
40
41
42
2
123
3
Print
Example of desk check
4.
Check that the expected result (60 and 123) match
the actual result (the total column in the table)

This desk check, which should take no more than a
few minutes, indicates that the algorithm is correct.
You can now proceed to code the algorithm into a
programming language
Conclusion
We must fully understand a problem before can
attempt to find a solution
 The method suggested was to analyze the
actual words used in the specification with the
aim of dividing the problem into three separate
components; input, output and processing
 After the initial analysis of the problem, you
must attempt to find a solution and express this
solution as an algorithm
