DCT 1123 Chapter 3 Problem Analysis Algorithm Discovery Algorithm Design Strategies Stepwise Refinement Control Requirements Variable Data type Sample Problem amp Solution Contents Sequence aka process ID: 802888
Download The PPT/PDF document "Problem Solving & Algorithms" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Problem Solving & Algorithms(DCT 1123)
Chapter 3: Problem Analysis
Slide2Algorithm DiscoveryAlgorithm Design StrategiesStepwise Refinement
Control Requirements
VariableData typeSample Problem & Solution
Contents
Slide3Sequence (aka process)Decision (aka selection)Repetition (aka iteration or looping)
The Key Features of an Algorithm
Slide4Each step or process in the algorithm is executed in the specified orderEach processes must be in a correct place otherwise the algorithm will most probably fail
Sequence
Slide5The outcome of a decision is either true or false
It is based on some condition that can only result in a true or false for example:
If today is Friday then Friday PrayerThe decision can also be stated as:
If proposition then process 1 else process 2
For example: If male then wear a
baju
melayu
else wear a
baju kurung
The Decision construct If…then, If…then…else
Slide6The repeat loop is used to iterate or repeat a process or sequence of process until some condition becomes true
The general form:
Repeat Process 1Process 2Process n
Until
proposition
Example:
Repeat
Put water in kettle
Until kettle is full
The Repetition Constructs
Slide7Step 1: Investigation stepIdentify the process
Identify the major decision
Identify the loopsIdentify the variable
Algorithm Design Strategies
Slide8Step 2: Preliminary algorithm stepDevise a high level algorithm
Step through the algorithm. Does this “walk-through” reveal any major problem? If it does, correct the problem
Algorithm Design Strategies
Slide9Step 3: Refining the algorithm stepIncorporate any refinements indicated in step 2
Group together processes where appropriate
Group together where appropriate
Test the algorithm again by stepping through it
Algorithm Design Strategies
Slide10aka Top Down ApproachA 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
Slide11Example: 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
Stepwise Refinement
Slide12Let 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
Slide13a = 5; b = 2; a = a + 1; result = a - b;
Variable
Slide14Obviously, 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.
Variable
Slide15When 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
Slide16Data 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
int
Integer.
4bytes
signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
long int (long)
Long integer.
4bytes
signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
bool
Boolean value. It can take one of two values: true or false.
1byte
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)
Slide17Carefully reading and rereading the problem until you understand completely what is requiredThe problem should be divided into three separate components:
Input: a list of source data provided to the problem
Output: a list of the outputs required
Processing: a list of actions needed to produce the required output
Defining the problem
Slide18Problem:A program is required to read three numbers, add them together and print their total
Tackle this problem in two stages:
Underline the nouns and adjectives used in the specification. This will establish the input & output components
Nouns is “three numbers”
Adjectives is “total”
The input is
three numbers
and the output is the
total
Sample Problem & Solution – defining problem
Slide19Input
Processing
Output
Number 1
Read three numbers
Total
Number 2
Add numbers together
Number 3
Print total numbers
Sample Problem & Solution – defining diagram
Slide20Now all nouns & verbs in the specification have been considered and the defining diagram is completeWe understand the input to the problem, the output to be produced and the processing steps required to convert the input to the output
When it comes to writing down the processing steps in an algorithm, you should use words that describe the work to be done in terms of single, specific tasks or functions
Sample Problem & Solution
Slide21StartInput number 1, number 2, number 3
Total = number 1 + number 2 + number 3
Display total End
Sample Problem & Solution - Pseudocode
Slide22Sample Problem & Solution - Flowchart
Start
Input number 1, number 2, number 3
Total = number 1 + number 2 + number 3
Display Total
Stop
Slide23Choose TWO sets of input test data. The THREE numbers selected will be 10, 20 and 30 for the first case and 40, 41 and 42 for the second case
Establish the expected results for each case
Sample Problem & Solution – Desk Check
First data set
Second data set
Number 1
10
40
Number 2
20
41
Number 3
30
42
First data set
Second data set
total
60
123
Slide24Set up a table of relevant variable names, and pass each test data set thru the solution algorithm, statement by statement. Line numbers have been used to identify each statement within the program
Sample Problem & Solution – Desk Check
Statement Number
Number 1
Number 2
Number 3
Total
First Pass
1
10
20
30
2
60
3
display
Second Pass
1
40
41
42
2
123
3
display
Slide25If at the end of a desk check, the actual results do not match the expected results, the solution algorithm probably contains logic errorIn this case, the programmer needs to go back to the solution algorithms
Sample Problem & Solution – Desk Check
Slide26http://users.evtek.fi/~jaanah/IntroC/DBeech/3gl_algorithm1.htmSimple Program Design. A Step by Step Approach. Lesley Anne Robertson. Thomson Course Technology
http://www.cplusplus.com/doc/tutorial/variables/
Reference