Download presentation

Presentation is loading. Please wait.

Published byWayne Ellin Modified over 4 years ago

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

2
Algorithm Discovery Algorithm Design Strategies Stepwise Refinement Control Requirements Variable Data type Sample Problem & Solution Contents

3
Sequence (aka process) Decision (aka selection) Repetition (aka iteration or looping) The Key Features of an Algorithm

4
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

5
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

6
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

7
Step 1: Investigation step i.Identify the process ii.Identify the major decision iii.Identify the loops iv.Identify the variable Algorithm Design Strategies

8
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

9
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

10
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

11
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

12
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

13
a = 5; b = 2; a = a + 1; result = a - b; Variable

14
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

15
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

16
NameDescriptionSize*Range* charCharacter 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 intInteger.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. 1bytetrue or false floatFloating point number.4bytes+/- 3.4e +/- 38 (~7 digits) double Double precision floating point number. 8bytes+/- 1.7e +/- 308 (~15 digits)

17
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 problem 2.Output: a list of the outputs required 3.Processing: a list of actions needed to produce the required output Defining the problem

18
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

19
InputProcessingOutput Number 1Read three numbersTotal Number 2Add numbers together Number 3Print total numbers Sample Problem & Solution – defining diagram

20
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

21
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

22
Sample Problem & Solution - Flowchart Start Input number 1, number 2, number 3 Total = number 1 + number 2 + number 3 Display Total Stop

23
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 setSecond data set Number 11040 Number 22041 Number 33042 First data setSecond data set total60123

24
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 NumberNumber 1Number 2Number 3Total First Pass 1102030 260 3display Second Pass 1404142 2123 3display

25
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

26
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

Similar presentations

Presentation is loading. Please wait....

OK

By the end of this session you should be able to...

By the end of this session you should be able to...

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google