Notion of Algorithm problem algorithm input Program or


Notion of Algorithm problem algorithm input Program or Software output 2

Algorithm Definition An algorithm is a sequence of unambiguous instructions for solving a problem for any legitimate input in a finite time. 3



Methods of Specifying an Algorithm There are 2 options that are most widely used nowadays: Pseudocode Flowchart 6


ตวอยางรหสจำลอง Algorithm mean 1. total 0 , count 0 2. Loop (not end of file) 2. 1 read number 2. 2 total + number 2. 3 count + 1 3. average total / count 4. Print average 5. end Algorithm max, min 1. read number 2. max number , min number 3. Loop (not end of file) 2. 1 read number 2. 2 if number > max 2. 2. 1 max number 2. 3 if number < min 2. 3. 1 min number 4. Print max, min 5. end 8

ตวอยางรหสจำลอง Algorithm mean 1. 2. 3. read N total 0 , count 0 while count < N do Algorithm mean 1. 2. 3. 1 read number 3. 2 total + number 3. 3 count + 1 2. 1 read number 2. 2 total + number 2. 3 count + 1 4. 5. 6. average total / count Print average end read N total 0 For count 1 to N do 4. 5. 6. average total / count Print average end 9


สญลกษณทใชในผงงาน Start /End Input/Output Decision Process Print Connector 11

ตวอยางผงงาน A Start not end of file N Print max Y read number Stop max number A N number > max Y max number 12


Standard Operations Declaration Expression Assigning Values 14

Declaration module header : main, submodule e. g. Algorithm Euclid() , Module Sum(), Subprogram Mean() data : input/output e. g. Input: integer n , character d , array A with integer 30 elements Output: summation of n elements 15

Expression • consist of operand , operator • types : • arithmetic: a + (b – c) * 4 • relational: x > y , i ≠ 10 • logical: (i < 5) and (j < 10) 16



![Assigning Value Format: variable expression e. g. x 1 j i*i sum + a[n] Assigning Value Format: variable expression e. g. x 1 j i*i sum + a[n]](http://slidetodoc.com/presentation_image_h2/51b1b6bf6a5aee1943c857dd11ba56b7/image-20.jpg)
Assigning Value Format: variable expression e. g. x 1 j i*i sum + a[n] check (m<5) and (n<4) 20

Example Algorithm degree_conversion Input : a number as degree Celcius Output : an equivalent degree Fahrenheit 1. read celcius 2. fahrenheit 9/5*celcius+32 3. write celcius , fahrenheit 4. end 21

Basic Control Structures Sequence Control Selection/Decision Iteration /Repetition 22

Sequence Control Assignment stmt. Input/Output stmt. 23

Sequence Control Assignment stmt. e. g. j i+k*2 name “hawaii” i 0 24

Sequence Control Input/Output stmt. e. g. open file fn 1, fn 2 close file fn 1 read var 1, var 2, … from fn 1 write var 1, var 2, … to fn 2 read var 1, var 2, … write var 1, var 2, … 25

Selection/Decision simple (if-then-else) nested if multiple (switch-case) 26

Simple Selection if-then-else sequence of if if-then (-else) 27

Simple Selection if-then e. g. if-then-else e. g. 28

Simple Selection sequence of if-then e. g. 29

Simple Selection sequence of if-then (-else) e. g. 30

Nested Selection stmt. if-then (if-then) if-then-else (if-then-else) if-then (if-then-else) -else (if-then-else) 31

Nested Selection stmt. รปแบบ : if-then (if-then) ตวอยาง if (�������� ) then if (���� ) then …. . end if 32

Nested Selection stmt. if-then-else (if-then-else) ตวอยาง if (���� ) then …. Else if (���� ) then … [else] …. end if 33
![Nested Selection stmt. if-then (if-then-else) -else (if-then-else) if (�������� ) then …. [else] …. Nested Selection stmt. if-then (if-then-else) -else (if-then-else) if (�������� ) then …. [else] ….](http://slidetodoc.com/presentation_image_h2/51b1b6bf6a5aee1943c857dd11ba56b7/image-34.jpg)
Nested Selection stmt. if-then (if-then-else) -else (if-then-else) if (�������� ) then …. [else] …. end if Else if (���� ) then … [else] …. end if ตวอยาง 34

Multiple Selection stmt. case/switch case ans of 1: x n*n 2: x n*m 3: x n*n*m 4: x n*n*n else write “input error !” end case 35

Iteration /Repetition (Loop) for while-do repeat-until (do-while) 36

Iteration /Repetition (Loop) for statement e. g. 1. for i 0 to 10 1. 1 read num Requirement specification คอ ? <ขนตอนวธนแกปญหาอะไร ? > 1. 2 sum+num 2. write “sum =”, sum 37

Iteration /Repetition (Loop) while-do statement e. g. 1. i 1 , sum 0 2. while (i<10) do 2. 1 read num 2. 2 sum + num 2. 3 i i + 1 3. write “sum =”, sum 38

Iteration /Repetition (Loop) repeat-until (do-while) statement e. g. 1. i 1 , sum 0 2. repeat 2. 1 read num 2. 2 sum + num 2. 3 i i + 1 until (i>10) 3. write “sum =”, sum 39


Answer แบบท 1 = 12 + 22 + 32 + … + n 2 Algorithm sumsqr(n) 1. read n 2. sumsq 0 3. for i 1 to n 3. 1 sumsq + i * i 4. display “sumsquare of ”, n, “=”, sumsq 5. end 41

Answer แบบท 2 = {n (n+1)(2 n+1)}/6 Algorithm sumsqr(n) 1. read n 2. sumsq n * (n+1) * (2 n+1) / 6 3. display “sumsquare of ”, n, “=”, sumsq 4. end 42


Answer Algorithm BMI 1. for i 1 to 10 1. 1 read w, h 1. 2 bmi w / (h * h) 1. 3 display “BMI = ”, bmi 1. 4 if (bmi >= 40) then display (“too fat”) else if (bmi >= 30) then display (“fat”) else if (bmi >= 25) then display (“quite fat”) else if (bmi >=18. 5) then display (“slim”) else display (“too slim”) 2. end 344 -211 Algorithmic Process and Programming , created by Dararat Saelee 44



Input/Output Data Input: data to be known before/while processing a program Output: result from program execution Types: decide appropriate type of input /output data ; e. g. int , longint, char, float , double Structure: relationship of each data : struct or record, series of data set (array) 47

Constant/Variable Data Constant: its value is fixed before running until stop running e. g. 50, ‘y’ , “male” Variable: its value can be changed during the execution e. g. x = y + z 48


เครองมอในการพฒนาโปร แกรม q. Programming Environment q. Program Development Tools 50

S/W Development Environments set of tools and techniques provided for users to develop programs in easier way and more convenient use e. g. compiler + editor + debugger + file system + GUI or windowed interface 51

Programming Environment Integrated Development Environment (IDE) : Editor Compiler Preprocessor Libraries debugging tools and other utilities e. g. Turbo C, C-Free, Dev. C 52

Programming Environment Development kits : compiler, libraries e. g. JDK/JRE Tools : only editor e. g. Notepad editor & tools e. g. Edit. Plus 53

Program Development Tools Editor Libraries Preprocessor Compiler Debugger Utilities 54

Program Development Tools Editors : Notepad , Turbo C/C++ editor , Editor. Plus , EClipse Libraries : Header files (C - #include) Packages (Java - import) 55

Program Development Tools Preprocessor : C - #define Compilers : C – gcc , Turbo C/C++ Java – javac , java 56

Program Development Tools Debuggers : Turbo C/C++ trace , watch DOS debug Utilities : C - online help Java – help message 57
- Slides: 57