Problem Solving Algorithms DCT 1123 Chapter 3 Problem
Problem Solving & Algorithms (DCT 1123) Chapter 3: Problem Analysis
Contents � Algorithm Discovery � Algorithm Design Strategies � Stepwise Refinement � Control Requirements � Variable � Data type � Sample Problem & Solution
The Key Features of an Algorithm � Sequence (aka process) � Decision (aka selection) � Repetition (aka iteration or looping)
Sequence � 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
The Decision construct If…then, If…then…else � 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 as: decision can also be stated ◦ If proposition then process 1 else process 2 ◦ For example: If male then wear a baju melayu else wear a baju kurung
The Repetition Constructs � 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
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. 1 byte signed: -128 to 127 unsigned: 0 to 255 short int (short) Short Integer. 2 bytes signed: -32768 to 32767 unsigned: 0 to 65535 4 bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 int Integer. long int (long) Long integer. 4 bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 bool Boolean value. It can take one of two values: true or false. 1 byte true or false float Floating point number. 4 bytes +/- 3. 4 e +/- 38 (~7 digits) double Double precision floating point number. 8 bytes +/- 1. 7 e +/- 308 (~15 digits)
Defining the problem � 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
Sample Problem & Solution – defining problem 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 diagram Input Processing Output Number 1 Read three numbers Total Number 2 Add numbers together Number 3 Print total numbers
Sample Problem & Solution � 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 - Pseudocode � Start Input number 1, number 2, number 3 2. Total = number 1 + number 2 + number 3 3. Display total � End 1.
Sample Problem & Solution - Flowchart Start Input number 1, number 2, number 3 Total = number 1 + number 2 + number 3 Display Total Stop
Sample Problem & Solution – Desk Check � 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 First data set Second data set Number 1 10 40 Number 2 20 41 Number 3 30 42 First data set Second data set 60 123 � Establish total the expected results for each case
Sample Problem & Solution – Desk Check � 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 Statement Number 1 Number 2 within Numberthe 3 program Total used to. Number identify each statement First Pass 1 10 20 30 2 60 3 display Second Pass 1 40 41 42 2 123 3 display
Sample Problem & Solution – Desk Check � 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
Reference � http: //users. evtek. fi/~jaanah/Intro. C/DBeech/3 gl_algorithm 1. htm � Simple Program Design. A Step by Step Approach. Lesley Anne Robertson. Thomson Course Technology � http: //www. cplus. com/doc/tutorial/variables/
- Slides: 26