chapter 3: problem analysis problem solving & algorithms (dct 1123)

26
Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

Upload: wayne-ellin

Post on 31-Mar-2015

272 views

Category:

Documents


9 download

TRANSCRIPT

Page 1: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

Chapter 3: Problem Analysis

Problem Solving & Algorithms(DCT 1123)

Page 2: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• Algorithm Discovery

• Algorithm Design Strategies

• Stepwise Refinement

• Control Requirements

• Variable

• Data type

• Sample Problem & Solution

Contents

Page 3: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• Sequence (aka process)

• Decision (aka selection)

• Repetition (aka iteration or looping)

The Key Features of an Algorithm

Page 4: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• Each step or process in the algorithm is executed in the specified order

• Each processes must be in a correct place otherwise the algorithm will most probably fail

Sequence

Page 5: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• The 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 Prayer

• The 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

Page 6: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• The repeat loop is used to iterate or repeat a process or sequence of process until some condition becomes true

• The general form:– Repeat – Process 1– Process 2– Process n– Until proposition

• Example:– Repeat

– Put water in kettle

– Until kettle is full

The Repetition Constructs

Page 7: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

Step 1: Investigation step

i. Identify the process

ii. Identify the major decision

iii. Identify the loops

iv. Identify the variable

Algorithm Design Strategies

Page 8: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

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

Page 9: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

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

Algorithm Design Strategies

Page 10: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• 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

Page 11: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• 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

Stepwise Refinement

Page 12: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• 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

Page 13: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• a = 5; b = 2;

• a = a + 1;

• result = a - b;

Variable

Page 14: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• 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.

Variable

Page 15: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• 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

Page 16: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

Data Types

Name Description Size* Range*

char Character or small integer. 1byte signed: -128 to 127unsigned: 0 to 255

short int (short) Short Integer. 2bytes signed: -32768 to 32767

unsigned: 0 to 65535

int Integer. 4bytessigned: -2147483648 to 2147483647unsigned: 0 to 4294967295

long int (long) Long integer. 4bytes

signed: -2147483648 to 2147483647unsigned: 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)

Page 17: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• Carefully reading and rereading the problem until you understand completely what is required

• The problem should be divided into three separate components:1. Input: a list of source data provided to the problem2. Output: a list of the outputs required3. Processing: a list of actions needed to produce the required output

Defining the problem

Page 18: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

Problem:

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

Page 19: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

Input Processing Output

Number 1 Read three numbers Total

Number 2 Add numbers together

Number 3 Print total numbers

Sample Problem & Solution – defining diagram

Page 20: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• Now all nouns & verbs in the specification have been considered and the defining diagram is complete

• We 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

Page 21: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• Start

1. Input number 1, number 2, number 3

2. Total = number 1 + number 2 + number 3

3. Display total

• End

Sample Problem & Solution - Pseudocode

Page 22: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

Sample Problem & Solution - FlowchartStart

Input number 1, number 2, number 3

Total = number 1 + number 2 + number 3

Display Total

Stop

Page 23: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• Choose 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

Page 24: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• Set 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

Page 25: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• If at the end of a desk check, the actual results do not match the expected results, the solution algorithm probably contains logic error

• In this case, the programmer needs to go back to the solution algorithms

Sample Problem & Solution – Desk Check

Page 26: Chapter 3: Problem Analysis Problem Solving & Algorithms (DCT 1123)

• http://users.evtek.fi/~jaanah/IntroC/DBeech/3gl_algorithm1.htm

• Simple Program Design. A Step by Step Approach. Lesley Anne Robertson. Thomson Course Technology

• http://www.cplusplus.com/doc/tutorial/variables/

Reference