Final Document
Final Document
1 !"#$%&
#
2 ( $%)# !"#*%
) # !(
%)
)% )
!"## *(!"#
%)
,
-(()"
(./ -0
# ! )
)*
)1
(*)"() #03# *
*)
/)
*
4()"
-
!(5 !"#
4
(/$ $# 6
( 3)# )7"4
(
"4)()
6) #"4
)"4
) /($9($/(6
**3% )% $"
7
*("*)
# "
,
)
(*
#*)**(!"# ) 1 (
" 0
(0
#)" 0(
/
",9 "
/. - - (0
#)" 0( #
6) -
03*%$%& )03: 7
#6!"#*3% !(
.))*)
% $ ;"=
#"4
(!"#
!(
)!("4
) /#*)* /
"4 /#*)**(!"#)
"(0
#)/
:(03"( # "4 /#*)*!"/ / "
- 6)()
#6!"# )"()()
%)%
, !"# # ,(
#
0( # .
)03$()!
*
!
% ** .- - .()
#6. ?3)" (
# -( .@)-(( ((0
((0"
, !!( .- ) # ((
.-" /.
-$% %)
"#
#6 " !(/
)
.
*
!(5 ()() )0
( :A " .-(()
$%
$ 5% #
/ (0
( .C- /
6"
"
)
# *
0
( .// $
(
" : - .
*
28/04/2010
1 of 229
60 /!)
(Master Teacher in Programming)
1 #
2 (
!"#$%& # ( $%)# !"#*%
)) # ?3)
%)
)
% !"##
) *(!"##
#
-(()" (./ -0
# ! )
)*
)1 (
*)"()
)% .))"*)
$ ;"=
#"4
(
!"#
!(
)!("4
)/
#*)* / "4 /
"(0
#)/ ( :(03"()
"4 /#*)*!"/%) /
,."
) *)
% $
1.
*
, )(*
6* *
)# )(*)"4)1 6 6 (!("
9(# 9)C)
2.
*
,$! )
)*
# *
% :
6#"4
)1 6
3.
*
,*(!"#
6#"4
6
6 !(!"#*( )/# ((!(6
4.
*
,6
",($
# ((
28/04/2010
2 of 229
+$$
$-
.$-
#$%
"
-
$!#$%
(60 !)
3
%
08.30 - 09.00
09.00 - 09.30
09.30 - 12.00
%&'
09.00-12.00
) (
:"
"?,"4!"# $,3)"4# ."
/*(!"# ,3)
)0
(
!( 6, (
" 7)%
predicates as conditions for selections and iterations
subprograms: procedures and functions
pointers
array and pointers
structures as aggregation of data
self-referential structures and pointers
%#)
*
Post-Test 6 3 / $
!!(
0 (
*)03())
#"49
-0
9
"
#$%
(!())"4
*/)/)
Problem-based learning
Problem Solving Steps
"
#$%
()
Problem-based learning
Problem Solving Steps
%&'
! )
)!"#
" 0#"
6
)*
# #
) *
# 6
)
/ Editor/Compiler
6)#)$ (if, switch)
6)#?%6 (for, while, do while)
Function and Procedure
Parameter passing (with Pointer)
Structures
%&' ()
13.00-16.00
09.00-12.00
1D Array
13.00 - 16.30
09.00-12.00
13.00-16.30
13.00-16.00
28/04/2010
9 +$
$
%&' %!
#)" 9*
( if-else-if, switch)
*
6- )
,
)1
!"# # #
) ())(
()*
3 of 229
$
7
%
09.00-12.00
%&'
2D Array (Sorting and Searching algorithms)
13.00-16.00
9 +$
$
%&' '
#)" 9*
( if-else-if, switch)
*
6- )
,
)1
!"# # #
) ())(
$% #
(
Structures and Self-Referential Structures
Dynamic Memory Allocation and Linked-list
09.00-12.00
13.00-16.00
09.00-12.00
13.00-16.00
09.00-10.00
10-00-12.00
10
13.00-16.00
11
09.00-12.00
12
13.00-16.00
09.00-12.00
(
Post-test
).
$
(
1
("=)
13.00-15.00
).
$
(
2
(Cs)
15.00-16.00
*"
*. )*()(( 80 3)
:t *
#
(
-u #")%
"
$X
0-49
50-79
80-100
28/04/2010
(
1 ("=) (
2 (Cs)
25
40
35
4 of 229
$
28/04/2010
5 of 229
4 (Hardware)
(Software)
2.1
(Computer Hardware)
( .. 2525)
3 (Input Data)
(Data Processing) (Output Result) 2.1
28/04/2010
6 of 229
(Processing)
(Input)
(Output)
(Memory)
2.2
5 (Central Processing Unit) (CPU)
(Input Unit) (Output Unit) (Main Memory)
(Secondary Memory)
(Case)
(Monitor)
(Keyboard)
28/04/2010
7 of 229
(Intel)
Intel-Compatible Processor
(AMD) (Cyrix)
workstation
28/04/2010
8 of 229
(port)
(Input Devices)
(Keyboard) (Mouse)
(Scanner) (Digital Camera)
(Keyboard)
101 105
(Alphanumeric key) (Numeric key)
(Function key) F1 F12
Esc,
PrintScreen
28/04/2010
9 of 229
Window
key Window
Start
(gaming
keyboard)
28/04/2010
10 of 229
(Serial Port) PS/2
USB (Wireless keyboard)
(Mouse)
GUI
(mouse pointer)
(Click)
(Dragging)
2 3 PS/2,
USB
(Output Unit)
28/04/2010
11 of 229
(Text)
(Graphics)
(Audio)
(Video)
(Output Devices)
(Monitor)
(soft copy)
(Monochrome)
CRT (Cathode Ray Tube)
(flat panel
display) (LCD)
LCD
LCD
(resolution), (size),
(dot pitch)
28/04/2010
12 of 229
1600 x 1200
1600 1200
Aspect
SVGA
XGA
SXGA
WXGA
UXGA
WSXGA
WUXGA
WQXGA
Ultra XGA
Wide Super XGA
Wide Ultra XGA
Wide Quad XGA
800 x 600
1024 x 768
1280 x 1024
1280 x 800 or
1366 x 768
1600 x 1200
1680 x 1050
1920 x 1200
2560 x 1600
Ratio
4:3
4:3
5:4
16 : 10 or
16 : 9
4:3
16 : 10
16 : 10
16 : 10
WXGA
1280 x 1024
19 21
(dit pitch) pixel pitch
0.28 mm
(hard copy)
Dot-Matrix
(Laser Printer)
(Inkjet Printer)
(Speaker)
(Sound Card)
28/04/2010
13 of 229
(Main Memory)
28/04/2010
14 of 229
2.
ROM BIOS
(Byte) (kilobyte)
(Megabyte) (Gigabyte) (Terabyte)
kB
MB
GB
TB
1,024
1,048,576
1,073,741,825
1,099,511,627,776
(Secondary Memory)
(Harddisk), CD, DVD,
(Flash Memory)
1.44 MB
28/04/2010
15 of 229
Kilobyte (kB)
Megabyte(MB)
Gigabyte (GB)
Terabyte(TB)
Petabyte(PB)
Exabyte(EB)
Zettabyte(ZB)
Yottabyte(YB)
1
1
1
1
1000
1
1000
1
2 1,024
220 1,048,576
230
240
250
260
270
280
10
2.3
(Personal Computer)
PCCompatible IBM Apple
PC Windows
Apple Macintosh Mac OS
PC Windows
Apple
Mac OS
28/04/2010
16 of 229
(Notebook Computer)
Mobile Computer
PC
PC
Notebook
(Handheld Computer)
(Stylus)
mail
PDA (Personal Digital Assistant)
Pocket PC
Hand-held Stylus
28/04/2010
17 of 229
Server
PC
(Mainframe Computer)
(Supercomputer)
PC
28/04/2010
18 of 229
(Embedded Computer)
28/04/2010
19 of 229
1
. 1.
..2.
..3.
. 4. BIOS
. 5.
. 6.
. 7.
. 8.
. 9.
. 10.
2
1.
.
.
.
.
2.
.
.
. Flash Memory
.
3.
. (chip)
.
.
.
4.
. MiniComputer
. MicroComputer
. Embedded Computer
. Microprocessor
5.
.
.
.
.
6. Apple
. Mac OS
. Windows
. DOS
.
7.
.
.
.
.
28/04/2010
20 of 229
8.
.
.
9.
.
.
10.
. mobile computer
. notebook computer
. desktop personal computer
.
28/04/2010
.
.
.
.
21 of 229
(Machine Language)
(mnemonic)
(Assembly Language)
(High-level Language)
(Low-level Language)
2.1
0 1
(Machine Language)
5
(Machine Language)
.. 1952
61H (61 )
AL x86/IA-32
28/04/2010
22 of 229
10110000 01100001
(Assembly Language)
.. 1952
(Assembly Language)
(Mnemonic code)
x86/IA-32 61H
AL
1011000 01100001
MOV
AL , 61H
(Assembler)
MOV AL , 61H
(High-level Language)
.. 1960
(Third-generation Language)
BASIC, COBOL, FORTRAN C
C Test
#include <stdio.h>
main() {
printf( Test );
}
28/04/2010
23 of 229
(Very high-level Language)
4 (Fourth-generation Language)
4GLs
(Procedural Language)
SELECT First Name, Last Name
FROM Employees
WHERE City = Bangkok
COBOL
(Query Language)
SQL (Structured Query Language)
(Natural Language)
5 (Fifth generation Language)
5GLs
28/04/2010
24 of 229
(Knowledge base system)
SQL
SELECT First Name , Last Name
FROM Employees
WHERE City = BANGKON
(Source Program) (Source Code)
(Executable
Program)
(Assembler) 2.1
2.1
(Interpreter)
(Compiler)
2.2
28/04/2010
25 of 229
Interpreter
Compiler
2.2
2.2
(BASIC)
28/04/2010
26 of 229
(FORTRAN)
:
:
(COBOL)
.. 1960 Business Oriented Language
(Structure Program)
IDENTIFICATION DIVISION.
Program-Id. Hello-World.
*
ENVIRONMENT DIVISION.
*
DATA DIVISION.
*
PROCEDURE DIVISION.
Para1.
DISPLAY Hello, world..
*
Stop Run
28/04/2010
27 of 229
:
(OOP)
:
(PASCAL)
1970
Blaise Pascal
(Turbo Pascal)
Program HelloWorld(output);
Begin
Writeln(Hello, World!)
End.
:
28/04/2010
28 of 229
:
:
(C++)
Class
(Object-oriented programming)
#include <iostream>
int main()
{
std::cout << Hello World! << std::endl;
return 0;
}
:
:
(VISUAL BASIC)
BASIC
28/04/2010
29 of 229
: GUI Windows
:
(JAVA)
JAVA JAVA
JAVA
JAVA
28/04/2010
30 of 229
COBOL
Microsoft Access
Access
Visual Basic JAVA Visual Basic
Access
PDA
2.3
1.
2.
3.
4.
28/04/2010
31 of 229
1.
2. (Input/Output Specification)
3. (Process Specification)
2.1 3
1.
1.1
1.2 0
2.
2.1
2.2 3
2.3
3.
3.1
3.2
2.3
28/04/2010
32 of 229
1
2
3
2.3
(Algorithm)
(Pseudo-code) (Flowchart)
(syntax)
(Bug)
(debug)
28/04/2010
33 of 229
1. Syntax Error
Coding Error
2. Logic Error
1. User Document User guide
2. Program Document Technical Reference
(Maintainance)
1
1.
. .
.
.
2.
. Basic
.
.
.
3.
. Basic
.
.
.
4.
.
. Window
28/04/2010
34 of 229
.
.
5.
.
.
. .
6.
. .OBJ
. .BAS
. .C
. .CPP
7.
.
.
.
.
8.
.
.
.
.
9.
.
.
.
.
10.
. bug
. syntax error
. logic error
. coding error
2
.1. 80486
.2.
.3.
.4. Windows
.5.
3
1.
28/04/2010
35 of 229
.
2.
.
.
3. (Assembler)
..
..
4.
..
...
5. (Source Code)
..
..
6.
..
..
7.
..
...
8.
28/04/2010
36 of 229
3
3.1
#) $&!& 5#)/(&)1 () .1$#)/(0
1+
&% .1#)&%# !$ /.&%6 #) $
0
1+/(/.53/ ).2.#) !&
&# ()$&
".
# F
+". !".& //(."
+5 (0
1+&
/( .5&F".3)+#/"
+6/.5"!'/( 5&03#) /./(
28/04/2010
37 of 229
1.
!
( $&0
1+53 .)! /."("% "
3'+4( "%
0.(
"
"4 /.".)(,-&
53!/$2$($!"- G #
03
&
2.
#$%&'
,4
03$2 $!# $!0$3 203 /.
%H(
"
+)
# $& $2.) (% "!"-
3. %
'%
()
4. %
%
(+
)
,
''-%
" 53 "$&-. I
I "
(3 (Module)
).) G . )
.)
%$&03
"% 5#$ !/
.$&&
)4"
)/(
5.
!/(
#3!"- $6
/$"
+
# 50)
.$+6 !$&("
)$%#"(O)##3 2. /##3()/./(
4-
#) /5
!)#3 ( "(O)3
6. '
'
%
,4
") 1!) ).(6 !$&/.5$2/$
/.2. . 6 ")/ .
6
/ /
$#) &%
+))
#)
).# )#&..
(Program Development Life Cycle:
PDLC)
() 6 #(
1. #"&+" (Requirement Analysis & Feasibility Study)
2. # 0 /#,-& (Algorithm Design)
3. #(!#) (Program Coding)
4. #(
/# (Program Testing & Debugging)
5. ##)
(Documentation)
6. #
!4 (Program Maintenance)
28/04/2010
38 of 229
"&+"
0 /#,-&
#)
(
/#
#)
!4
3.2 - 123'
$
-345-
28/04/2010
39 of 229
&/(
$2
%
.$2 / #3!# &% ".)3.$&.
0 ( $2 (0
1+
10)./
1
)5!(
##0 1 ,-&
%$&/(0
1+
()
'.#3. G
#/6
$
O$( 0#!
G )./
5&
0
#3&)".
#3#/". 0 &%
28/04/2010
40 of 229
/
".#
."
+
4)./ +
4".41#
O
#3#
&
, 2%
, !2!6
$
2% Hours , ".)26
$ 2% PayRate
10
!&(1"!'
".41
=
!2 x .2
#0
1.
2.
&
, 2%
, !2! , ".)2
3. "!'
".41
=
Hours x PayRate
4. (0
1+ &
2% ".41
5.
!
28/04/2010
41 of 229
(%
6 3.2 #) ,-&()"
+$&
(
"."!'/(
$76)
/
"!'#3
+
4)./ ".
3
#)O
#3#
"3#
(h) #
(r)
3
#)
10
1"!'
=
x 2 x "3
#0
1.
2.
"."3 ".#
3. "!'". = x r2 x h
4. (".
O
5.
!
28/04/2010
42 of 229
(%
6 3.3 #)
%)0
# 2"
+ ()$& ("
(
$76)
/
+" 0
(#
+
4)./ (" (# ."
#3#
&! (ID) ,2%name , "
28/04/2010
43 of 229
(%
6 3.4 "&+,-& #)#
#!"..#
(0"%
+
$76)
#3#
#".$&)3.$2% number1 , number2 number3
#3+
4 0$&2% total
10
.".#!".
#".
(0##
5#)/((
Add_three_number
1. Read number1 , number2 , number3
2. total = number1 + number2 + number3
3. Printf total
END
(%
6 3.5 "&+,-& #)!&
&".i)#4'&O3! ()
".
4'&O334( 4'&O3!4(#!6#/ $& (".4'&O3i)O
$76)
#3#
".4'&O334()3.$ 2% max_temp
4'&O3!4()3.$ 2% min_temp
#3+
4 ".4'O3i)O
()$2 2% avg_temp
10
1.
".4'&O344( 4'&O3.4(
2. &".i)()() avg_temp = (max_temp + min_temp)/2
3. (". avg_temp O
5#)/((
Find_average_temperature
1. READ max_temp , min_temp
2. avg_temp = (max_temp + min_temp) / 2
3. Output avg_temp to the screen
END
28/04/2010
44 of 229
3.3 $7
&
10 # 0
%$&/(0
1+3
,88
+%$36
(
1
#3) #0 (0
1+ ()
F
+&%
/{+
6/( &6
#3)$&.
%!/0 (0
1+ 2.%) G /.
&(#30 11( )$2()/ 2. $#)" #)
&% ).0#)/(( 0O
./
!"#$"
#$"
28/04/2010
45 of 229
" &
%
(!(
1.
'.3
7500
9000
2.&.. 2.
!(
#3
".#3
28/04/2010
1
5
2
10
3
15
4
20
46 of 229
5
25
6
30
7
35
6!+,8+;!6
(
(
#34() 2 1 (
1. % #3!# $(&".//./((
!(
#(
)3.5(
#3 ( (
.#3#&(&%)!.0
&(
. ". .
".//./(6 0#3)
2. $21!&( 1
%
"..#3&(&%) 1"!(
)3.&6
(0
1+$ .
"%#3)4()0.0 &
.)4()60
'#) ()O
".) G ./
)
+
-
(
10 INPUT X
20 IF X < 0 THEN GOTO 60
".# F
+ 6
/$ X " 1 ".
5#$ X ".(
$&!"!$
( 60 .5
/.$2.$&!"!$
( 30 ./
30 Y = X + 200
!".)3.$ X #'
200 6
$ Y
40 PRINT X , Y
(".)3.$ X Y
50 GOTO 10
)
/!"!$
( 10 "%
".# F
+
$&. 6
/$ X (".(53 ()".$&.)
60 PRINT pGOOD BYEq
5".
# F
+ ".
6!"!$
( 60
"% ("!. GOOD BYE
&
70 END
$&&)4(!
). O
"!$
( 20 $2!&
(
.#3#&(&%) ()
(
.5 #".
&)".#3#"
&( ((/)
(
60
%
+"!. pGOOD BYEq I
!&
)../ &
2%. LC (
28/04/2010
47 of 229
)
+
10 INPUT X , LC
20 Y = X + 200
30 PRINT X , Y
40 IF LC = 1 THEN
GOTO 10
ELSE
GOTO 50
50 PRINT pGOOD BYEq
60 END
-
(
".##6
$ X LC 5&".$ LC 1
(.#3)/.&(
!".$ X
200 6
/$ Y
(0
1+". X Y O
5&".)3.$ LC 1 $&/!"!
( 10 .5
/.$2. $&/!"!
( 50 /
( 10
%
#3"./
3.4
6!+,89$7
45-
# ,-&"(
#3&) G 24( 5&(
#324(() !
536/./(&)".#!
#!/().53
$ .#"# (!$ .##
0 &(#0(
(#/((3/(.).0(
($#$(
28/04/2010
48 of 229
(%
6 3.6
#1$ ,-&#)+$). 3.4
$76)
). 3.4 0(
Add_three_number
1. Read number1 , number2 , number3
2. total = number1 + number2 + number3
3. Printf total
END
#1!/((
1. #(
4#24( ()$&24( ". 10,20 30 #24(
". 40,41 42
8;!
8;!6+
number1
10
40
number2
20
41
number3
30
42
2. "("!
#0()
8;!
Total
60
8;!6+
123
28/04/2010
49 of 229
4.
0
1+## 2 # 3 (60 123) ./(0
1+
&%/.()(3 Total
(%
6 3.7 5&
$&
#3#!6 F
+! 9 $&
.#&.". #
#
3)+ ()$& (0
1+O
$76)
,-&(.5"&+/((
+$6
!#
3)+
&736
(!#
,
3)+O
8)
8
#3# F
+
6'
$&
X #
# F
+
num_p
!
num_n
!
num_z
!3)+
$7
&
!&
,-&$&
#3# F
+". ()1(
!&(1"(
5#!
$&
1 #
!
5#!
$&
1 #
!
28/04/2010
50 of 229
!&
10 5&$# 6 ("!. p5 X ".3)+q / "!
6/(.(
%5&0.# 4 5 /(6 (.#".3)+)3. !&
10
5!/((
1. )).#3# 1 24( 5&# 9 ". 13,7,-4,0,-5,2,6,-2 35
2. "("!
0() /(!
5 !
3 3)+ 1
8;!
5
3
1
num_p
num_n
num_z
-
(8)
!,
8 (X)
num_p
num_n
num_z
1
2
3
4
2
3
4
2
3
4
5
2
3
4
5
6
2
3
4
5
2
3
4
2
3
13
13
13
7
7
7
-4
-4
-4
-4
0
0
0
0
0
-5
-5
-5
-5
2
2
2
6
6
0
0
0
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
3
3
3
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
28/04/2010
51 of 229
-
(8)
!,
8 (X)
num_p
num_n
num_z
4
2
3
4
5
2
3
4
2
7
6
-2
-2
-2
-2
35
35
35
-
4
4
4
4
4
4
4
5
5
5
2
2
2
2
3
3
3
3
3
3
1
1
1
1
1
1
1
1
1
1
4.
."!
$# 7
"!
" (/ (. 053 I
5!/
.//(
"!
)3..
28/04/2010
52 of 229
%�(
((. !# (
1.
.! #"%"
+ )1. pDesk Checkingq 1
. 5$&0
1+/(&%/.
#0(
(
()!&(#324(&# ".# . G #) / .
().
#"%"
+ 12.2.)(#0(
(/(
.. #"% ./
2.
()"%"
+ %0.
$# 6. #"%
"
+
#34
"!
# "%"
+! Syntax Error
&%#0(
(/)'+ 50("%"
+
+#.#0(
( 5/(
"!
6".#$/(. .53 %(
5!
#3#"%"
+
%!0./
3.5 ,,
8( $3
$#) "
+ '#) )3.&) ' I#
#) )$2(
8( ,,,%
(Top-Down Programming)
0#)
8( ,,!
3 (Modular Programming)
#) )!. G ).)#(6&) .
). (3 (Module) I5!/((3% G !$&5& "0(
(6
5 /#(30(
(/(.)# !&
$#) (3& !&"
"4!
&( .)(3$($2. (3 .(35)(3).) G /( .(3
."
"4/)(3% G /(()
#) $ '(3 ).)O"% ).)O)$ (Internal
Subroutine) ).)03#) #)#
%.&# & .5)$2/(
O&"% ).)O) (External Subroutine) I ).)6
/$/
(library)
5)$2/( 5!/$2$% G /(
28/04/2010
53 of 229
&
..
..
..
CALL
SUBPRO_A
SUBPRO_A
RETU
RN
&
..
..
..
CALL
SUBPRO_A
SUBPRO_A
CALL
sub_B
28/04/2010
54 of 229
8( $; (Object-Oriented Programming)
#) $&.!. G 54 &%6 ()
.
6"%/ $2!/ % $2 65!6$2/(
#(/(
)/( $#) !6. G
%!&
)$2$O)&
28/04/2010
55 of 229
,,^_ -!6
(,6
6 1 %"!
534(
)&#
28/04/2010
56 of 229
6. (
"53##
,-&"!
#3).24(
. ).) 1 24(
#. ).) 2 24(
". #
'#,-&
. 4'5//($,-& G
7. #$("%"&++
4
. "&+. !
"%4.$(
#. "&+.0
1+/0
". "&+3
+0
1+
. 534#
8. ."(.0
#3#/0". &
O$(
. !&.)2O
)+!(
#. 2%#)"
". !&.)"+/.!(
. 534#
9. &4$(2% $0
. %06
#3
#. - '+$&#$.)$0
". 5!/$2$##) /(
. 534#
10.
#3#/4) 0()&
O$(
. &".(%i)#
#.
)%"%&%#&4(
".
#3
4""
. 534#
6 2 !"%&) 5 "%&) �(
.......1. #)"!1
)10!$&52.)#) /(.)#
.......2. 5#3##
'2(##().)&%!6
}}3. "&+,-&(3+
4
#
4(
.......4. 10/."&)#
.......5. '
+"%
+ 5%.+
4
28/04/2010
57 of 229
6 3
"!5./
1.
"&)#"&+,-&
...................................................................................................................................................
..................................................................................................................................................
.................................................................................................................................................
2.
"&)#"&+3
28/04/2010
58 of 229
(Algorithm)
(Flowchart) 4
(Psudocodes)
5.1
(Algorithm)
(pseudo
code)
(Reserve Word)
END
28/04/2010
59 of 229
Algorithm
1. = 0
2. = 0
3. ()
4. 0
= +
3
5
5. =
6.
7.
Algorithm Avarage_Sum
1. count = 0
2. sum = 0
3. INPUT (value)
4. IF value > 0 THEN
count = count +1
sum = sum + value
GOTO 3
ELSE GOTO 5
5. avarage = sum / count
6. OUTPUT (avarage)
7. END
28/04/2010
60 of 229
1. X
2. Y
3. ARRAY = (X * Y)/2
4.
START
END
1.
2.
3.
4.
READ X
READ Y
Compute ARRAY = (X * Y)/2
Print ARRAY
5.2
28/04/2010
61 of 229
X = 4
4 X
X=Y+Z
Y Z
X
X = X+1
X 1
2 + 3 x 4 14 2 x 3 + 4
10
+
*
/
** ^
3
3
2
2
1
(Boolean)
28/04/2010
62 of 229
=
<
>
<=
>=
<>
5.1 1
A > 0 ( )
1
( )
2
A>0
A <> 0 ( )
2
( )
1
A <> 0
2
(True) (False)
28/04/2010
63 of 229
1 A
2 B
2
T
T
F
F
T
F
T
F
(A) OR (B)
T
T
T
F
NOT (A)
F
F
T
T
AND
OR
NOT
5.2 2 AND
(X > 10) AND (X <= 15) ( )
1
( )
2
1
2 X 10
15
5.3 2 OR
(X > 50) OR (X <= 60) ( )
1
( )
2
28/04/2010
64 of 229
X 50 60
5.3
READ X,Y
; X Y
Read student_name
Get system_date
Read number_1, number_2
; student_name
; system_date
; number_1,number_2
28/04/2010
65 of 229
WRITE DATA1
PRINT A, B
; DATA1
; A B
Compute Calculate
Compute
ARRAT = (X * Y)/2
ARRAT X Y
Compute
Profit = Price Cost
INIT SET
INIT A, B : INTEGER
; A B
INIT Y
:REAL
; Y
SET A = 8
; A 8
5.4 7%
ComputeTotal
init total, price, vat : real
init vat = 0.07
read price
compute total = price + (price x 0.07)
print price, total
end
28/04/2010
66 of 229
5.4
IF IF-THEN-ELSE ENDIF
(Statement)
IF
THEN
;
1
;
ELSE
2
;
ENDIF
;
5.5 number 0
0
CASE num OF
1 : PRINT 11111
2 : PRINT 22222
3 : PRINT 33333
ENDCASE
28/04/2010
67 of 229
10
5.5
1.
FOR , DO ENDFOR IN STEPS OF
IN STEPS OF
FOR
Statement
ENDFOR
2. REPEAT UNTIL
REPEAT
Statement_1
.
UNTIL (Condition)
3. WHILE - ENDWHILE
WHILE (Condition)
Statement_1
.
ENDWHILE
5.6 1 10 repeat-until
Print number 1 to 10
init num : integer
/** num
init num = 1
/** num 1
repeat
/** ( num = 10)
print num
/** num 1 - 10
increase num (num+1) /** num 1
until num > 10
/** num 10
28/04/2010
68 of 229
11
5.7 1 10 while
Print number 1 to 10
init num : integer
/** num
init num = 1
/** num 1
while (num <= 10)
/** num 10
print num
/** num 1 10
increase num (num + 1) /** num 1
endwhile
/**
5.8 1 + 2 + 3 + .+100
while 1
100
Sum_number_1_100
init i, sum : integer
/** i sum
init i = 0
/** i 0
init sum = 0
while (i <= 100)
/** i 100
compute sum = sum + i
/** i sum sum
compute i = i + 1
/** i
endwhile
print sum
/** sum
5.9 1 10 for
Number 1-10
init num : integer
/** num
for num = 1 to 10 do
/** num = 1 10
print num
/** num 1 - 10
endfor
/**
5.6
CALL
28/04/2010
69 of 229
12
CALL
LABEL
GOTO
START :
AB1:
Statement_1
..
.
GOTO AB1
END
1.
.
.
.
.
2.
.
.
.
.
3.
. READ
. FOR
. MEM
. CALL
4.
NUMBER
28/04/2010
70 of 229
13
. 1 100
. 1 100
. 100
.
5. 5 INCRAESE NUM
. NUM
. NUM
. NUM NUM
.
2
1.
.
.
2.
.
.
..
.
3.
.
.
4. 10
28/04/2010
71 of 229
C++ C#
6.1
.c
(Integrated Development
Environment : IDE) DEV C++
C++
28/04/2010
72 of 229
DEV C++
1.
2. Editor Screen
File
New Source File Editor Screen
Untitled1
2. Source File
3.
Editor Screen
28/04/2010
73 of 229
6.2
(C/C++)
5
1. (Preprocessor directives)
2. (Global declarations)
3. (The main() function)
4. (Uses-defined function)
5. (Program comments)
(Preprocessor directives)
(directive) #
(Header Part) Directive
#include
#include
#include stdio.h
#include Pro1.c
stdio.h
Pro1.c
#include <>
include main()
#define
28/04/2010
#define NAME VALUE
#define END 20
END 20
#define A 5*6+3
A 5*6+3
74 of 229
(Global declarations)
(main() function)
(Semi colon ;)
main() { }
main()
#include stdio.h
main()
{
function();
/* */
}
function()
/* function
{
return ;
/*
}
(Program comments)
/* */ //
C
28/04/2010
75 of 229
#include stdio.h
main()
{
}
1.
2. (Statement)
{ }
main
printf()
#include stdio.h
main()
{
printf(COMPUTER);
}
COMPUTER
printf() (string)
stdio
( standard input output) stdio.h
#include (directive) stdio.h (header file)
(comments)
/* */
/* PROGRAM BY TEERAWAT PRAKOBPHON */
28/04/2010
76 of 229
Dev-C++
1. File -> New ->Source File
2.
3.
28/04/2010
77 of 229
4. Save
4. pro1.cpp ( C++
cpp)
1.
2.
1
28/04/2010
78 of 229
getch() conio.h
conio.h
6. printf
COMPUTER getch()
include
28/04/2010
79 of 229
6.3
stdio.h
#include
printf()
printf()
print format
printf()
printf( ,)
printf(control string,variable list,..);
variable list control string
6.1
/* PROGRAM BY TEERAWAT PRAKOBPHON */
#include stdio.h
main()
{
printf(COMPUTER\n);
\n
printf(\n\nCOMPUTER);
\n
printf(%d\n,20);
}
RUN
n (\n)
\n COMPUTER
(backslash)
28/04/2010
80 of 229
tab 8
hh
Backslash
\n
\t
\xhh
\a
\\
6.1 printf() 3 %d
(format code) % format specification
%d
%u
%f
%e
%c
%s
%%
%o
%x
(Char)
(string)
%
printf
printf(%s %d %f %c \n,Sam,14,-8.76,X);
variable list
variable list control
string
Sam 14 -8.760000 X
8.76
printf %
f
28/04/2010
81 of 229
4.567800 4.568 4.57 4.6
scanf()
printf() ASCII
controlstring
&
control string
int num;
/* num */
scanf(%d,&num);
Enter
num control string %d control string
printf()
int i,j;
/* i j */
scanf(%o %x,&i,&j);
printf(%o %x,i,j);
2
%o
char str[80];
printf(Enter a string : );
scanf(%s,str);
/* & */
printf(Here s your string: %s,str);
28/04/2010
82 of 229
str 80
str
RAM
ASCII Unicode
#include stdio.h
int
feet ,inches;
main()
{
feet = 6;
inches = feet * 12;
printf(Height in inches is %d ,inches)
}
Height in inches is 72
int feet inches
Integer () main()
feet 6
feet 12 inches printf
printf
(Comma)
%d
28/04/2010
83 of 229
63 63
1 ,
(,)
Data Type
, ..;
Variable Name, . ;
int
feet , inches;
Integer
-32767 +32767 ( )
A Z 0 9
(Reserved Word)
=
variable = expression;
expression
assignment (=)
(+) (-)
* div /
28/04/2010
84 of 229
int x;
x = 5;
/* x */
/* 5 x */
int x, y;
y = 3;
x = y + 5;
/* x y */
/* y 3 */
/* y 5 x */
printf
%
x = 7;
printf(%d,x);
printf(%d,x + 9);
7
16
yards = 8;
feet = yards * 3;
printf(%d yards is , yeads);
printf(%d feet , feet);
yards = 8;
feet = yards * 3;
printf(%d yards is \n,yeads);
printf(%d feet \n, feet);
8 yards is
24 feet
printf(Yes \n);
printf(No \n);
printf(Yes \n);
printf(\n);
printf(No \n);
28/04/2010
85 of 229
printf(Yes);
printf(No);
Yes
No
Yes
YesNo
No
#include stdio.h
int
nickels,dimes,TotCenter;
main()
{
nickels = 3;
dimes = 7;
TotCenter = (nickels * 5) + (dimes * 10);
printf(%d nickels and %d dimes \n,nickels,dimes);
printf(= %d cents \n,TotCenter);
}
4
- (simple type)
- (string type)
- (structure type)
- (pointer type)
28/04/2010
86 of 229
(ordinal type) (Real
Data Type)
char
unsigned char
signed char
int
unsigned int
-128 127
0 255
-128 .. 127
-2,147,483,648 .. 2,147,483,647
0 .. 4,294,967,296
1
1
1
4
4
int Turbo-C 2 -32768
+32767 DEV-C 4
127
char
28/04/2010
87 of 229
\t
\a
1 tab
Beep
float
double
long double
8
10
(string type)
255
NULL (\0) COMPUTER 9
O M P
U T
\0
28/04/2010
88 of 229
nickels
3
Dimes
7
TotCentr
85
#include stdio.h
int
x;
main()
{
x = 6;
x = 8;
printf(X is %d,x);
}
x
6
8
x 6 8 x
x 8 printf 8
(=)
num1 =
5;
num2 =
14;
14
num1 =
num2;
printf(%d,num1);
14
printf(%d,num2);
num1 5 num2 14
num1 num2 num1
14
.
count = count +1 { count }
.
sum = sum + x { sum x x }
.
num = 3 * num { num 3 }
28/04/2010
89 of 229
6.4
int y;
/* y */
y = 5 * 3 + 2*2;
/* y */
printf(Output = %d ,y);
Output = 19
5 3 2 2
y
+
*
/
%
++
--
(Addition)
(Subtraction)
(Multiplication)
(Division)
(Modulus)
(Increment)
(Decrement)
6.2
#include stdio.h
#include conio.h
main()
{
int x,y;
x = 3;
y = 2;
printf(x + y = %d\n,x+y);
printf(x * y = %d\n,x*y);
printf(%d + %d = %d\n,x,y,x+y);
printf(5 + 3 = %d\n,5 + 3);
getch();
}
28/04/2010
90 of 229
y = x + 5;
y = x 2;
y = x * 3;
y = x / 2;
y = 5 % 3;
y++;
y--;
x y
x 3 y 2 printf()
%d printf()
DEV-C
1
2
3
4
5
()
++ , -
*, / , %
+,
=
y = 7+8*2
8 2 16 7 y 23
y = 5%2 + 14/3 6
1. 5 % 2 = 1
2. 14/3 = 4.66
3. 1 + 4.66 = 5.66
4. 5.66 6 = -0.33
y -0.33
28/04/2010
91 of 229
6.5
1. (Single Selection)
2. (Double Selection)
.
.
2 (true) (false)
( )
1 2
(Relation Operators)
28/04/2010
92 of 229
==
!=
<=
>=
>
<
(Equal)
(Not Equal)
(Less Than or Equal)
(Greater Than or Equal)
(Greater Than)
(Less Than)
6 == 3
6 != 3
6 <= 3
6 >= 3
6>3
6<3
(Logical Operator)
&&
||
!
(AND)
(OR)
(NOT)
(5 == 4 + 1) && (18 <= 6 * 4)
if
if()
{
}
28/04/2010
93 of 229
if (x >= 10) printf(A\n);
printf(B);
x
x = 12
A
B
x=8
x = 15
A
B
sc 80
100 A if
if()
{
1;
}
else
{
2;
}
25
25
x
28/04/2010
94 of 229
1. x
2. x 25
2.1
3.
X > 25
X<0
X>0
28/04/2010
95 of 229
4 3
1.
2. X
3. X > 0
3.1
3.2 X < 0
3.2.1
3.2.1
4.
1
1. (7%2) + (8%5) % 10
. 3
. 4
. 5
. 6
2.
. >
. =
. >=
. !=
3. if
. m = 4.5
. x = = 8
. A > B
. C > D
4. x 20 30
. if((x >= 20) && (x <= 30))
. if((x >= 20) || (x <= 30))
. if(20 < x < 30)
. if((x <= 10) && (x >=20))
5.
.
.
28/04/2010
96 of 229
.
.
6. if else
.
.
.
.
7.
. do..while
. do..while for
. for
. for while
2
1.
2. if
3. +, -, *
%
28/04/2010
97 of 229
7
(Sequential Connection)
8.1
1. (Single Selection)
2. (Double Selection)
if()
else
if()
{
}
8.1
2 (true) (false)
28/04/2010
98 of 229
( )
1 2
3
,
8.1
Y
X > 10
READ A, B
X=X+5
X=A*B
WRITE X
8.1
28/04/2010
99 of 229
1. X > 10
1.1 A B
1.2 X A B
1.3 X X 5
1.4 X
2.
8.2
IF IF-THEN-ELSE ENDIF
IF THEN
ELST ENDIF
8.1
1. X > 10
1.1 A B
1.2 X A B
1.3 X X 5
1.4 X
2.
IF X > 10 THEN
READ A, B
X=A*B
ELSE
X = X + 5;
WRITE X
ENDIF
8.1
28/04/2010
100 of 229
IF X > 10 THEN
READ A, B
X=A*B
ELSE
X = X + 5;
WRITE X
ENDIF
#include stdio.h
#include conio.h
main()
{
int A, B, X;
printf(X = );
scanf(%d, &X);
if (X > 10)
{
scanf(%d,&A);
scanf*%d,&B);
X = A+B;
}
else
{
X = X+5;
printf(%d,X);
}
getch();
}
X 5
X 5 X X 10
X 12 IF
A B
8.1
0
-1
28/04/2010
101 of 229
8.2
#include <stdio.h>
#include <conio.h>
main()
{
int x;
printf(INPUT X : );
scanf(%d,&x);
if (x < 0)
x = x * -1;
printf(%d,x);
getch();
}
X<0
X = X * (-1)
8.2
8.2
A B
A B
A > B
A
28/04/2010
102 of 229
8.3
#include <stdio.h>
#include <conio.h>
main()
{
int A, B;
printf(INPUT A : );
scanf(%d,&A);
printf(INPUT B : );
scanf(%d,&B);
if (A > B)
printf( %d is larger, A);
else
printf(%d is larger , B);
getch();
}
8.3
8.3
80
A
70
B
60
C
50
D
50 F
28/04/2010
103 of 229
8.4
X >= 80
X >= 60
X >= 50
X >= 70
Grade = A
Grade = B
Grade = C
Grade = D
Grade = F
Grade
8.4
80
A
70 80
B
C
60 70
50 60
D
50
F
8.5
28/04/2010
104 of 229
#include <stdio.h>
#include <conio.h>
main()
{
int x;
char Grade;
printf(Input Score );
scanf(%d,&x);
if (x >= 80)
Grade = 'A';
if ((x >= 70)&&(X < 80))
Grade = 'B';
if ((x >= 60)&&(x < 70))
Grade = 'C';
if ((x >= 50)&&(X < 60))
Grade = 'D';
if (X < 50)
Grade = 'F';
printf(Grade = %c,Grade);
getch();
}
X >= 70
X<80
Grade = B
X >= 60
X<70
Grade = C
X >= 50
X<60
Grade = F
Grade
8.5
28/04/2010
Grade = A
X >= 80
105 of 229
Grade = D
8.3
IF
IF <Condition> THEN
< >
T
ELSE
< >
F
ENDIF
8.6
8.6 IF
ELSE
IF
8.2
40 1.5
(PayType), (Hours)
1.
2. 40
3. 40
= 40 + 1.5
4. 40
5.
28/04/2010
106 of 229
10
T
F
T
IF
Hours > 40
Pay = Rate *
(40 + 1.5 *
(Hours 40))
Pay =
Rate * Hours
8.7
IF
T
ELSE
F
Pay = Salary
ENDIF
IF..THEN..ELSE
IF
8.8
28/04/2010
107 of 229
11
8.8
9.6
8.3
Age < 16
Age >= 16 Age <65
Age >= 65
Charge (%)
7
10
5
28/04/2010
108 of 229
12
A
IF
Age < 16
Charge = 7
IF
Age >= 16
AND
Age < 65
Charge = 10
T
IF
Age >= 65
Charge = 5
8.9
IF
T
ELSE
T
F
F
ENDIF
28/04/2010
109 of 229
13
IF
8.10
8.10
8.3 65
3 3
2
8.3
1000
30
30
28/04/2010
600
400
200
800
60
40
20
80
110 of 229
14
30 1000
1000
Sex = F
400
age > 30
1400
1000
age > 30
200
sex = F
1200
X Y
X Y
28/04/2010
111 of 229
15
IF-THEN-ELSE-ENDIF
X 14 Y 20
Y X
X Y
14
20
20
20
20
20
20
X Y
Y X
14
14
14
20
14
14
TEMP
X TEMP
Y X
TEMP Y
28/04/2010
TEMP
14
14
20
20
20
20
20
14
14
14
14
112 of 229
16
X
swap
IF X < Y THEN
MOVE X TO TEMP
MOVE Y TO X
MOVE TEMP TO Y
ENDIF
8.4
(char_1, char_2, char_3)
swap (temp)
temp char_1
char_1 = char_2
char_2 = temp
1.
READ char_1, char_2, char_3
2.
IF char_1 > char_2 THEN
temp = char_1
char_1 = char_2
char_2 = temp
ENDIF
3.
IF char_2 > char_3 THEN
temp = char_2
char_2 = char_3
char_3 = temp
ENDIF
28/04/2010
113 of 229
17
4.
1.
char_1
char_2
char_3
K
B
G
2.
char_1
char_2
char_3
z
s
a
B
G
K
a
s
z
3.
1
2
3
4
5
1
2
3
4
5
char_1
char_2
char_3
k
b
B
K
G
z
s
S
Z
A
S
4. 3 2
28/04/2010
114 of 229
Temp
k
k
z
z
18
8.5 2
1. 5%
2. 2%
(Price), (choice),
1 5%
2
(Month) x (Price) x 2%
1.
2. (Price)
3. (Choice)
4. Choice = 1
4.1 = Price (Price *(5/100))
4.2 (Month)
4.3 = Price x Month x (2/100)
4.4 = (Price + ) / Month
5.
6.
28/04/2010
115 of 229
19
Price
Choice
Choice = 1
= (Price + )/Month
8.11 8.5
8.4
IF..THEN..ELSE
IF
CASE, OF ENDCASE 8.12
28/04/2010
116 of 229
20
NO case
CASE = ?
Case 1
Case3
Case 2
Case 4
8.12
(CASE) CASE CASE
CASE CASE
8.6
H
P
C
S
Commission
Rate
Sales Commission
Salary
CASE
1.
28/04/2010
CASE code
H
P
OF
:
:
117 of 229
21
C
S
ENDCASE
2. EXIT
:
:
Pay = Commission
Pay = Salary
code
8.13
code
CASE of
code
= H
Pay = Rate *
Hours
= P
Pay = Rate *
Pieces
= C
Pay = Commission
* Sales
= S
Pay = Salary
8.13
switch .. case
switch (variable)
{
case constant_1 :
case constant_2 :
case constant_3 :
.
28/04/2010
statement;
break;
statement;
break;
statement;
break;
.
118 of 229
22
NOTE
switch 4
1. switch if switch
if
2. switch
3. switch
4. default
switch (year)
{
case 1 :
case
2:
case
3:
case
4:
default :
printf(Fershman\n);
break;
printf(Sophomore\n);
break;
printf(Junior \n);
break;
printf(Senior\n);
break;
printf(Nonmatriculated\n);
}
year 1 Freshman
3 Junior 1,2,3,4 Nonmatriculated
switch selector
default
28/04/2010
119 of 229
23
switch
case
case
selector = const 1
selector = const 2
selector = const 3
selector = const n
default
8.14 switch..case
num case switch
switch(num)
{
case 4
: ; break;
/* */
case 2.5
: ; break;
/* */
/* */
case m
: ; break;
case 2
: ; break;
/* */
default
:
}
28/04/2010
120 of 229
24
8.5
8.4
0
-1
8.15
#include stdio.h
#include conio.h
main()
{
printf(INPUT X : );
scanf(%d,&x);
if (x < 0)
x = x * -1;
printf(%d\n,x);
getch();
}
X<0
X = X * (-1)
8.15
28/04/2010
121 of 229
25
8.5
A B
A B
A > B
A
B
8.16
#include stdio.h
#include conio.h
main()
{
int A, B;
printf(INPUT A : );
scanf(%d,&A);
printf(INPUT B : );
scanf(%d,&B);
if (A > B)
printf(A, is larger)
else
printf(B, is larger);
getch();
}
8.16
28/04/2010
122 of 229
26
8.6
80
A
70
B
60
C
50
D
50 F
8.17
X >= 80
X >= 60
X >= 50
X >= 70
Grade = A
Grade = B
Grade = C
Grade = D
Grade = F
Grade
8.17
28/04/2010
123 of 229
27
80
A
70 80
B
60 70
C
50 60
D
50
F
8.18
#include <stdio.h>
#include <conio.h>
main()
{
int x;
char Grade;
printf(Input Score );
scanf(%d,&x);
if (x >= 80)
Grade = 'A';
if ((x >= 70)&&(X < 80))
Grade = 'B';
if ((x >= 60)&&(x < 70))
Grade = 'C';
if ((x >= 50)&&(X < 60))
Grade = 'D';
if (X < 50)
Grade = 'F';
printf(Grade = %c,Grade);
getch();
}
X >= 70
X<80
Grade = B
X >= 60
X<70
Grade = C
X >= 50
X<60
Grade = F
Grade
8.18
28/04/2010
Grade = A
X >= 80
124 of 229
Grade = D
28
1
1.
.
.
.
.
2.
.
.
.
.
3. IF ELSE
.
.
.
.
4.
.
.
.
.
5. IF
.
.
.
.
6.
. IF ,THEN,ELSE
. CASE
. IF,THEN
.
28/04/2010
125 of 229
29
7 10
START
READ A, B
N
A >= B
A=A+B
B=A*B
PRINT B
END
7.
.
.
.
.
8.
. B A B
. A A B
. B
.
9. A 3 B 2
.
. A 5
. 2
.
28/04/2010
126 of 229
30
10. 9
. 2
. 2
. 6
.
2
. 1.
. 2.
. 3.
. 4.
. 5.
3
1.
.
2.
X
Y
Y X
M X Y
M X Y
M
28/04/2010
127 of 229
31
3. 2 X 7 Y 3
4.
0 49 F
50 59 D
60 69 C
70 79 B
80 100 A
28/04/2010
128 of 229
9.1
3
A
1.
2.
3.
Instruction
Instruction
F
GoTo
9.1
IF
<Condition(s)>
28/04/2010
129 of 229
3 3
for
for (initialization; condition; increment or decrement)
{
}
initialization
condition
increment
decrement
int i;
/* I */
for ( i = 1; i <= 5; i++)
printf(%d ,i);
i 1 i 5
i I
1 2 3 4
5
do-while
do {
}while (condition)
condition
while
28/04/2010
130 of 229
while (condition)
{
}
condition
9.2
9.2
1 10
1.
2. X = 1
3. X 10
X
X
4.
9.2
10.2
28/04/2010
131 of 229
FOR, DO ENDFOR
FOR
ENDFOR
9.1 1
10
10
sum
I
sum = 0
I = 1
I 10
4.1 sum sum I I
4.2 I
5. sum
6.
1.
2.
3.
4.
9.3 9.1
28/04/2010
132 of 229
9.1 (Sum)
(I) sum
I I
10 10
9.1 0 100 10
(Max), (Min)
Max Mix
10
I 10
Max = 0 Min = 100
x
Max Min
x
1.
2. I = 1
3. Max = 0, Min = 100;
4. I 10
4.1 x
4.2 x Min
Min = x
4.3 x Max
Max = x
4.4 I
5. Max Min
6.
28/04/2010
133 of 229
I=0
Max = 0, Min = 100
I <= 10
X < Min
Min = X
X > Max
Max = X
Max,
Min
#include stdio.h
#include conio.h
main()
{
int I, X, Max, Min;
Max = 0;
Min = 100;
for(I = 1; I <= 10; I++)
{
printf(Input Num %d ,I);
scanf(%d,&X);
if (X < Min)
Min = X;
if (X > Max)
Max = X;
}
printf(Max = %d, Min = %d\n, Max, Min)
getch();
}
9.4 10
28/04/2010
134 of 229
10
9.3
REPEATUNTIL
DOUNTIL 9.5
REPEAT
.
.
UNTIL ()
9.5
28/04/2010
135 of 229
do..while
do
{
statement;
}while(condition);
do condition while
9.2
int counter = 1;
/* counter 1 */
do {
printf(%d ,counter);
}while(++counter <= 10); /* counter 10 */
1 counter
10
1 2 3 4 5 6 7 8 9 10
9.3 100
100
int num;
/* num */
do{
printf(Input Number );
scanf(%d,&num); /* num */
printf(/n);
/* */
}while(num > 100);
/* num 100 */
28/04/2010
136 of 229
9.2
0
0
(sum)
0
(x) sum
x 0
0
1.
start
2. sum 0
3. X
4. X 0
sum = 0
4.1 sum = sum + X
4.2 X
5. sum
INPUT X
6.
START
sum = sum + x
INIT SUM = 0
READ X
do
SUM = SUM + X
READ X
while (X !=0)
PRINT X
INPUT X
NO
X=0
YES
END
9.6 9.2
Display sum
STOP
28/04/2010
137 of 229
10
#include stdio.h
#include conio.h
main()
{
int x, sum;
sum = 0;
printf(INPUT DATA );
scanf(%d,&x);
do {
sum = sum + x;
printf(INPUT DATA );
scanf(%d,&x);
}while(x != 0);
printf(Sum = %d\n,sum);
getch();
}
0
9.4
0
28/04/2010
138 of 229
11
Sum
Counter
(Sum) (Counter)
9.7
START
1.START
2.Sum = 0
3.Counter = 0
4.READ Age
5 DO
Sum = Sum + Age
Counter = Counter+1
READ Age
WHILE Age != 0
6.Average = Sum/Counter
7.Print Average
8. STOP
Sum = 0
Counter = 0
Enter
Age
Sum = Sum+Age
T
Counter =
Counter+1
Enter
Age
Until
Age = 0
T
Average =
Sum/Counter
Print
Average
END
9.7
28/04/2010
139 of 229
12
9.4
WHILEENDWHILE
9.8
x 1 10
1.
2. x 0
3. x < 10
x
x
4.
F
While
<Condition>
T
Instruction
Instruction
9.8
9.5
(fahrenheit) (Celsius) 15
28/04/2010
15
140 of 229
13
c_temp = (f_temp 32)*(5/9)
c_temp
f_temp
temp_count
1.
2. temp_count
3. temp_count 15
3.1 f_temp
3.2 c_temp c_temp = (f_temp - 32) * 5/9
3.3 c_temp
3.4 temp_count
4.
1.
2.
3.
4.
5.
6.
7.
8.
START
INIT temp_count = 0
WHILE temp_count < 15
READ f_temp
c_temp = (f_temp 32) * 5/9
PRINT c_temp
temp_count = temp_count + 1
ENDWHILE
STOP
1. 32 50
f_temp
32
50
2.
c_temp
28/04/2010
10
141 of 229
14
3.
temp_count
1,2
3
4
5
6
7
3
4
5
6
7
WHILE
f_temp
c_temp
32
0
1
50
10
4. c_temp 3 2
28/04/2010
142 of 229
15
9.5
C=B
C=B
Instruction
IF
C <= E
Instruction
Instruction
C=C+S
Instruction
C=C+S
IF
C <= E
F
.
9.9
C , B , E S
.
. WHILE
REPEAT-UNTIL .
28/04/2010
143 of 229
16
Counter = Begin
REPEAT
Instruction
Instruction
Counter = Counter + S
UNTIL Counter > End
Counter = Begin
Instruction
Instruction
Counter = Counter + S
Counter = Begin
WHILE Counter <= End
Instruction
Instruction
ENDWHILE
Counter = Begin
T
Counter <=
End
F
Instruction
Instruction
Counter = Counter + S
28/04/2010
144 of 229
17
1.
WHILE
READ DATA
WHILE DATA !=
.
.
READ DATA
ENDWHILE
2.
WHILE FOR
9.5
9.6
200 asterisk (*)
200 *
= +
= *
=
> 200 *
28/04/2010
145 of 229
18
number1
number2
sum, product, average
WHILE
AND
IF *
1.
2. sum
3. Prompt
4. number1 number2
5. number1 number2
5.1 sum = number1 + number2
5.2 produce = number1 * number2
5.3 average = sum / 2
5.4 sum 200
5.4.1 sum, * ,product, average
START
INIT sum = 0
Prompt for number1, number2
READ number1, number2
WHILE NOT(number1 = 0 AND number2 = 0)
sum = number1 + number2
product = number1*number2
average = sum/2
IF sum > 200 THEN
Display sum, *, product, average
ELSE
Display sum, product, average
28/04/2010
146 of 229
19
ENDIF
Prompt for number1, number2
READ number1, number2
ENDWHILE
STOP
9.7
S U S
, , , , (full-time :F/T)
(part-time : P/T) U
S Records
number
name
address
age
gender
attendance_pattern
U
Record
S
number, name address
S
28/04/2010
147 of 229
20
Print_student_records
Print STUDENT LIST heading
READ student record
WHILE more records exist
IF student record = S record THEN
Print student_number, name, address
ENDIF
READ student record
ENDWHILE
END
START
Print
heading
Read
student
record
More
Records?
T
T
F
Record = S?
STOP
Print
Student
details
Read
Student
record
9.10 9.7
28/04/2010
148 of 229
21
9.8 9.7
, ,
9.7
1 IF
Produce_part_time_female_list
Print PART TIME FEMALE STUDENTS heading
READ student record
WHILE more records
IF student record = S record THEN
IF attendance_pattern = P/T THEN
IF gender = female THEN
Print student_number, name,
address, age
ENDIF
ENDIF
ENDIF
READ student record
ENDWHILE
END
2 IF
Produce_part_time_female_list
Print PART TIME FEMALE STUDENTS heading
READ student record
WHILE more records
IF student record = S record THEN
IF (attendance_pattern = P/T
AND gender = female) THEN
Print student_number, name,
address, age
ENDIF
ENDIF
READ student record
ENDWHILE
END
28/04/2010
149 of 229
22
3 IF
Produce_part_time_female_list
Print PART TIME FEMALE STUDENTS heading
READ student record
WHILE more records
IF student record = S record
AND attendance_pattern = P/T
AND gender = female) THEN
Print student_number, name, address, age
ENDIF
READ student record
ENDWHILE
END
9.11 9.8
28/04/2010
150 of 229
23
9.9 9.8
total_students
total_selected_students
Produce_part_time_female_list
Print PART TIME FEMALE STUDENTS heading
Set total_students to zero
Set total_selected_students to zero
READ student record
WHILE records exist
IF student record = S record THEN
increment total_students
IF (attendance_pattern = P/T
AND gender = female) THEN
increment total_selected_students
Print student_number, name,
address, age
ENDIF
ENDIF
READ student record
ENDWHILE
Print total_students
Print total_selected_students
END
28/04/2010
151 of 229
24
START
Print
heading
Read
student
record
More
Records?
T
T
Print_total_
students
Record = S?
Increment
total_
students
Student
=P/T&female
Print_total_
Selected_
students
F
STOP
Increment
total_selected_
students
Print
Student
details
Read
Student
record
28/04/2010
152 of 229
25
1
1.
. 1
. 2
. 3
. 4
2.
. FOR
. REPEAT
. LOOP
. UNTIL
3.
.
.
.
.
4.
.
.
.
.
5.
.
.
.
.
6.
.
.
.
.
7.
. REPEAT.UNTIL
. WHILE.ENDWHILE
. UNTILGO
.
8.
.
.
.
.
9. REPEATUNTIL
.
.
.
.
10. WHILEENDWHILE
.
.
.
.
28/04/2010
153 of 229
26
3
1.
.
2.
I 0
I < 5
X
Y
Y X
M X Y
M X Y
I 1
M
3. 2 I
4. 10
5.
7 x 1
=
7
7 x 2
=
14
7 x 3
=
21
.
7 x 12
=
84
6. 5
28/04/2010
154 of 229
10
(array)
(element) (cell)
(index number)
8
X[1]
20
X[2]
35
X[3]
84
X[4]
21
X[5]
45
X[6]
65
X[7]
71
X[8]
39
X
[ ]
X[1..8] X[3]
X 3
X[2]
X[2] + X[3]
X[1+3]
X[5] + 1
2 35
2 3 35 + 84 119
4 21
5 1 46
X[5] :=
45;
45 X 5
Print X[6])
X 6
28/04/2010
155 of 229
0 1
10.1.
9 9
Age
7 Age(7)
1
2
3
4
5
6
7
8
9
Array
Age
12
9
11
12
9
14
13
8
16
Age(1)
Age(2)
Age(3)
Age(4)
Age(5)
Age(6)
Age(7)
Age(8)
Age(9)
28/04/2010
156 of 229
10
Fine_sum_10_number
Set sum to zero
FOR I = 1 TO 10 DO
READ x
sum = sum + x
ENDFOR
PRINT x
END
/** 10
/** x
/**
/**
10
x
10.1
array , Sum
number_of_elements
Find_sum_of_elements
Set sum to zero
FOR index = 1 to number_of_elements
sum = sum + array(index)
ENDFOR
Print sum
END
28/04/2010
/** sum 0
/**
157 of 229
10.2 10.1
Find_element_average
Set sum to zero
FOR index = 1 to number_of_elements
sum = sum + array(index)
ENDFOR
average = sum / number_of_elements
Print average
END
10.3
8
1 8
largest_element
Find_largest_element
Set largest_element to array(1)
FOR index = 2 to number_of_elements
IF array(index) > largest_element THEN
largest_element = array(index)
ENDIF
ENDFOR
Print largest_element
END
28/04/2010
158 of 229
10.4
10.3 smallest_element
Find_smallest_element
Set smallest_element to array(1)
FOR index = 2 to number_of_elements
IF array(index) < smallest_element THEN
smallest_element = array(index)
ENDIF
ENDFOR
Print smallest_element
END
10.5
10.4 10.5
Find_range_of_elements
Set smallest_element to array(1)
Set largest_element to array(1)
FOR index = 2 to number_of_elements
IF array(index) < smallest_element THEN
smallest_element = array(index)
ELSE
IF array(index) > largest_element THEN
largest_element = array(index)
ENDIF
ENDIF
ENDFOR
Print the range as smallest_element followed by largest_element
END
28/04/2010
159 of 229
10.2
A1
B1
C2
D2
A1
B1
C2
D2
C2
3
28/04/2010
160 of 229
month_table
Initialise_month_table
month_table(1) = January
month_table(2) = February
:
:
month_table(12) = December
END
10.6
, ,
ID
A001
A002
A003
B001
B002
ID
H
R
Total
28/04/2010
20
30
27
32
19
161 of 229
50
45
75
30
40
Total
Total H
R 100
FOR I = 1 TO 100 DO
Total(I) = H(i) x R(I)
ENDFOR
100
H R Total
Total
10.7
While
1
Read_values_into_array
Set max_num_elements to required value
/*
Set index to zero
Read first input value
WHILE (input values exist) AND (index < max_num_elements)
index = index + 1
array(index) = input value
/*
Read next input value
/*
ENDWHILE
IF (input values exit) AND index = max_num_elements THEN
Print Array size too small
ENDIF
END
28/04/2010
162 of 229
WHILE 9999
Read_values_into_variable_array
Set max_num_elements to required value
Set index to zero
Read first input value
WHILE (input values NOT = 9999) AND (index < max_num_elements)
index = index + 1
array(index) = input value
/*
Read next input value
ENDWHILE
IF index < max_num_elements THEN
index = index + 1
array(index) = 9999
ELSE
Print Array size too small
ENDIF
END
9999
Write_values_of_array
FOR index = 1 to number_of_elements DO
Print array(index)
ENDFOR
END
28/04/2010
163 of 229
10
10.9
18
18
18
FOR
scores
Process_exam_scores
Set total_score to zero
FOR index = 1 to 18
Prompt operator for score
Get scores(index)
total_score = total_score+scores(index)
ENDFOR
Compute average_score = total_score / 18
FOR index = 1 to 18
Display scores(index)
ENDFOR
Display average_score
END
28/04/2010
164 of 229
11
10.9
28/04/2010
165 of 229
12
100 number
number
10.9
integer_total
integer_count
Process_integer_array
Set integer_total to zero
Set integer_count to zero
ENDFOR
integer_average = integer_total / 100
28/04/2010
166 of 229
13
10.10
28/04/2010
167 of 229
14
10.3
1
[ ]
type
var_name[size]
[]
type var_name size
A
10
int A[10];
0
150 200
A
A[0] =
150;
A[9] =
200;
10.1 4
#include "stdio.h"
#include "conio.h"
main()
{
int A[4];
/* 4 */
int sum = 0;
/* */
28/04/2010
168 of 229
15
scanf()
10.9
10
10
#include "stdio.h"
#include "conio.h"
main()
{
int number[10], i, sum = 0;
/* */
for(i = 0; i<10; i++)
{
10
printf("Input Number %d : ",i);
scanf("%d",&number[i]);
}
for(i = 0; i<10; i++)
28/04/2010
169 of 229
16
10
#include "stdio.h"
#include "conio.h"
main()
{
int number[10], i, sum = 0;
for(i = 0; i<10; i++)
{
printf("Input Number %d : ",i);
scanf("%d",&number[i]);
sum = sum + number[i];
}
printf("The average is %.2f\n",sum/10.0);
getch();
}
2
2
2 4 4
2 2
28/04/2010
170 of 229
17
int AB[2][3]
AB 6 (2 x
3)
0
1
2
int sqr[3][3]
=
{
1 , 2 , 3,
4,5,6,
7,8,9
};
sqr 2 sqr[0][0]
1 sqr[0][1] 2 sqr[0][2] 3
10
1
5. 10.6
1.
2.
3.
4.
2
1. 10
2. 1
28/04/2010
171 of 229
18
3. A 20 20
4.
10,000
5 %
10 %
10,000 30,000
30,000
15 %
5. 40
3
6. 5
28/04/2010
172 of 229
1.
()
BCPL
B
Basic Combined
Programming
Language
PDP-7
(UNIX)
.. 2513
C
.. 2515
!
(SP2) / ..
1
2
'''
() ) *
3
(SP2) / ..
(SP2) / ..
28/04/2010
(SP2) / ..
173 of 229
4?)-Q@
04);
? ) *
-Q@
1
# include <stdio.h>
int main (void )
{
printf(%Hello, Good morning. \n+);
}
(SP2) / ..
! 2
&&' 2**)-$(1
# include <stdio.h>
main ( )
{
float point;
printf("\n\nPut your score in\n");
scanf("%f", &point);
printf("Your score is %f point\n\n", point);
}
;=@@
A)1
1. .4)3T1 .A.? 4@6
2. ;.
:C.04);
?)d
U >:C04 Q
3.
I:C.04 );
? underline e_g <A.
4. ;.
:C. reserved words C int, float, etc.
Note: 4
</=4>:567
I;W
Q @ )34)C04
Q<A.?<
@
8 4@6 Q=C04 QBQ @ )@I.:C.
JQ8834) 4@6 )@
9
(SP2) / ..
Q883.4 J=Q=3A
(SP2) / ..
10
Q883.4
J=;04C
A34) Q ) * @(;A<.:567
char
C
A34) 4@6;044@3
int
C
AB( W
@
short
C
AB( W
@
long
C
AB( W
? 2
unsigned
C
A34)=3<
A04);
?
float
C
A=3
B9AU
?
double
C
A=3
B9AU
?
? 2
28/04/2010
Unsigned
Float
Double
11
LMDJFN=9@A HG
HGJFN;FQ ASCII I8: 1 JFNE>;UCD7N7JHVENMDW 0 YZW 255
LMDJFN=9VENMDW ]32768 YZW 32767
LMDJFN=9VENMDW ]32768 YZW 32767
LMDJFN=9
VDd 2000 =:D7
Unsigned short = 2 LMDJFN=9VENMDW 0 YZW 65535
Unsigned long = 4 LMDJFN=9VENMDW 0 YZW 4000 =:D7
4
I8:LDM JFN=9hCD=FW 10x 8h x ALDM VENMDW ]37 YZW +38
8
LNDY<J:;W9;WJFN=9UVALDM B<W9Z7?
(SP2) / ..
12
174 of 229
# include <stdio.h>
main ( )
{
char reply;
reply = myn;
ooooooo
13
(SP2) / ..
14
4?)-Q@
@:;.4@3;/
U6;04;89
# include <stdio.h>
main ( )
{
char newline;
newline = m\nn;
printf(%Hello, Good morning. %c+,newline);
printf(%Hello, Good morning.\n+);
15
(SP2) / ..
(;8=3
B9AU
?
1Q<A. 2 Q8804 float Q=
double -A? double @W8<A. 2 34) float
(;8))
?U > .4)@
=4?A:@@W8
@:C.@@W8:JQ881 04@W8Q884W@-/7> A) 4?) 4<1
=3
9,000,000,000
345,000
0.00063
0.00000924
28/04/2010
16
17
QA)Q88
?U >
Q884W@-/7>
9
9.0*10
9.0e9
3.45*105
3.45e5
6.3e-4
6.3*10-4
9.24*10-6
9.24e-6
(SP2) / ..
18
175 of 229
&' 2**)-$(#-
);
?IT) 4@3;=? *
@48@3.4
7T)@( Q;=? *
@W8
@:5671?@ 4?>
(array) A)13.4
J=Q88
)04 4?>34) 4@3 4)
04);
?34)4?>04 [ ] JQ88@@(;A
)BT)
=@6dA)1
char name[30];
;
?IT) Q name C
A char
? 30 4@6
-A?@W8 4?> @@W81B@W8?)@=<8 > Q=<8 >9A.?
@W8; null 04 \0 A)1B@W8<A.B
)/?) 29 4@6
(SP2) / ..
height = 172.5;
printf(%Mr. Surasak is %d years old+,age);
printf(% and tall %f cms.\n+,height);
}
19
(SP2) / ..
valuea = 4;
sum = count + valuea;
printf(%Total value is %d.\n+,sum);
4>@
> B(3.4
J=
/
/>
JQ88
@(;A
:89
@/
/>
}
b==/v>B@c3.4
: Total value is 5.
(SP2) / ..
28/04/2010
20
21
(SP2) / ..
22
(SP2) / ..
24
176 of 229
JQ8834) scanf ( )
-%*)/%
@Q-44 4>)d
U >(;8567
+
@8@
@=8
*
@Jd
/
@;
%
@;4U6 (-
AJ=)
25
(SP2) / ..
@=?C
A34)3.4
J=
+,-
++n
- -n
(<A.-A?89C
A .4)@=?5?:04);
? ( )
Q=.);. Q;043.4
J= .4)@=?Q=)C
A
int cost;
cost = 2.7+4.5;
cost = (int)2.7+(int)4.5;
27
(SP2) / ..
point = 44;
;
?IT) @@(;A:;.@8 Q point :;.
@8 44
3.4.6 +
-9,.+:9;+<:9;
> ;04 >=
@@ ;04
@@@8
< ;04 <=
.4?@ ;04 .4?@@8
==
@8
!=
<
@8
28/04/2010
28
(SP2) / ..
/
n 4@ 1
=A n =) 1
float money;
.4)@=? Q float < integer (<A.A)1
(int) money;
(SP2) / ..
26
point == 44;
. 78 "-(/#(,, point ,!,$ 44 .: ,
29
(SP2) / ..
30
177 of 229
if
+
-9,.+:9;+<:9;;;=
&&
||
!
JQ8834)()
if ()04<3)
() .4)( I.)04<31B
);
4?)C
if (score >= 80)
grade = eAg; /* simple statement */
;04
if (math >= 60 && eng >= 55)
{ grade = eSg; /* compound statement */
printf(wYour grade is %c\nx,grade);
}
(SP2) / ..
Q= (and)
;04 (or)
<
(not)
34)
/B>?8?8C
) @
/B> 1 &&
/B> 2 B
)
04
/B>1)4)B
)
/B> 1 | |
/B> 2 B
)
04
/B>:A
/B>;T)
B
);04 1)4)
/B>1B
)
!
/B>?8?8
B
)
04
/B>?8?8WB
(SP2) / ..
31
if >.. else >..
+
-9,+=@<:
+:9;+<:9;+
AB ? :
JQ8834)()
if (();04
/B>)04<3)
() .4)(
04)04<31B
)
else () .4)(
04)04<31<
B
)
4?)C
if (value1 > value2)
min = value2;
else
min = value1;
(SP2) / ..
C
33
34
4?)
switch (ch)
{
case m1n :
printf(%Red\n+);
case m2n :
printf(%Blue\n+);
case m3n :
printf(%Yellow\n+);
default :
printf(%White\n+);
}
JQ88()
switch (
/B>)
{
case label1 : statement1;
case label2 : statement2;
'''''..
'''''..
default
: statementn;
}
28/04/2010
x = (y< 0) ? -y : y;
(SP2) / ..
.;+
AB9 E < : switch break
(SP2) / ..
32
35
(SP2) / ..
36
178 of 229
4?)
switch (ch)
{
case m1n : printf(%Red\n+);
break;
case m2n : printf(%Blue\n+);
break;
case m3n : printf(%Yellow\n+);
break;
default : printf(%White\n+);
}
(SP2) / ..
J while
JQ88
while (
/B>)04<3)
{
()=J;
''''
''''.
}
37
(SP2) / ..
J for
38
;;;<:
.;+
AB<:
: do while
JQ88
for (
/B> 1 ;
/B> 2 ;
/B> 3 )
{
()48;
''.
}
():C.:@89
:;.
@48();=? * 48
-A?
/B> 1 04@@(;A
.:;.@8 Q:C.:@48
/B> 2 @?8?8 @4B48I.)04<334)
/B>
B
)B
@()
()48
/B> 3 ():@
@(;AB=?Q=)<:Q =48
(SP2) / ..
JQ88
do
statement;
while (
/B>)04<3);
C
39
40
goto labels
() goto @48A.? 2 04
- () goto ():;.@-AA<?) (Q;)@(;A
-A?B@(;AC04 ?@ label name
- C04 (label name) @(;A (Q;)()B@-AA
<()
break
:C.
04 .4)@:;.@()
I;=9A44@B@=JQ=@-AA
<?)()4?J4@=J -A?<
.4) B48)04 <3:A *
continue
(SP2) / ..
num = 2;
do
{
num++;
printf(wNow no is %d\nx,num);
} while (num == 10)
(SP2) / ..
28/04/2010
compound statements
41
(SP2) / ..
42
179 of 229
(Function)
43
(SP2) / ..
44
(SP2) / ..
45
(SP2) / ..
()
B<A.-Q@
@48A.?H^)@>C 4 H^)@>C
04
H^)@>C;=@
H^)@>C@83.4
J=
H^)@>C:@8@=3
H^)@>CQA)b==/v>
(SP2) / ..
28/04/2010
46
47
Object
file
link
link
Library file
link
Execute
file
Object
file
(SP2) / ..
48
180 of 229
4.1 JQ8834)H^)@>C
Q88 1
Q88 2
C
A3.4
J=0 C04H^)@>C ( @@U Q )
{
@@U Q5?:H^)@>C ;
();
return (3.4
J= 4. )@)@=8);
}
(SP2) / ..
49
(SP2) / ..
(SP2) / ..
52
28/04/2010
50
#include <stdio.h>
double InputDouble ( )
{
double x;
printf ( w\nInput real value : w );
scanf ( w%.2f x, &x );
return ( x );
}
(SP2) / ..
JQ8834)H^)@>C ( 4)
53
(SP2) / ..
54
181 of 229
4.2
(SP2) / ..
#include <stdio.h>
double InputDouble ( );
double SumDouble ( double , double );
void PrintOut ( double );
55
(SP2) / ..
56
void main ( )
{
double a1, a2, sumVal;
a1 = InputDouble( );
a2 = InputDouble( );
sumVal = SumDouble ( a1, a2 );
PrintOut ( sumVal );
}
(SP2) / ..
57
(SP2) / ..
58
4.3 @?@:C.H^)@>C
a1 .4)
C
A double 04)B@B )
0@=8
B@H^)@>C
C
A double
@?@:C.H^)@>C
@ 0 B:C.
JQ88A) 4<1
a1 = InputDouble ( );
double InputDouble ( );
(SP2) / ..
28/04/2010
59
(SP2) / ..
60
182 of 229
10
PrintOut( sumVal );
:C.J@8-- <
double InputDouble ( );
)1.'2'(,<=% $0: , ,
(SP2) / ..
61
(SP2) / ..
62
(SP2) / ..
63
(SP2) / ..
main ( )
a1
a2
sumVal
()
main ( )
step1
a1 = InputDouble(
InputDouble( );
28/04/2010
a1
a2
sumVal
step2
a2 = InputDouble(
InputDouble( );
InputDouble ( ) x
InputDouble ( ) x
(SP2) / ..
64
65
(SP2) / ..
66
183 of 229
11
()
Step3
()
main ( )
sumVal=SumDouble(a1,a2)
sumVal=SumDouble(a1,a2)
main ( )
a1
a2
sumVal
PrintSum ( )
sumDouble ( ) x
y
(SP2) / ..
67
69
(SP2) / ..
4.3
28/04/2010
PrintSum(sumVal);
PrintSum(sumVal);
68
(SP2) / ..
step4
(SP2) / ..
(SP2) / ..
a1
a2
sumVal
70
71
(SP2) / ..
72
184 of 229
12
void main ( )
{
x = 10;
printf ( wmain (start) -> x : %d\nx, x );
func1 ( );
printf ( wmain (after func1) -> x : %d\nx, x );
func2 ( x );
printf ( wmain (after func2) -> x : %d\nx, x);
func3 ( );
printf ( wmain (after func3) -> x : %d\nx, x);
}
(SP2) / ..
>*!3
main (start) -> x : 10
func1 -> x : 20
main (after func1) -> x : 20
func2 -> x : 30
main (after func2) -> x : 20
func3 -> x : 10
main (after func3) -> x : 20
73
(SP2) / ..
74
5.1 LND<:@FNI
AhNFG LWB:DW
(4,3)
(0,0)
75
(SP2) / ..
Member
-9+N =
-,O9 struct .
PA
..QR
;
<:
9JO9 struct
<:
9JO9 struct
+:9
-=
QR
(SP2) / ..
28/04/2010
76
-9+N .+P
B struct .= A-PA
A--:=.A--S struct
R
;-A:
77
(SP2) / ..
78
185 of 229
13
@@(;A
.:;.@8 Q3.4
J=Q88-).)
@
@U
Q883.4
J=
-).)
@
@U
Q
3.4
J=Q88
-).)
@4.)IT)
C
@5?: Q3.4
J=Q88-).)
struct_name.member
79
(SP2) / ..
4?)
;
?; 9
C
@34)3.4
J=5 struct 4BB
Q5:A@W<A. 1)3.4
J=/012 Q=
53.4
J=40 * C 4?> Q=?)@U
Q34)3.4
J=5 struct <A.4@A.?
struct rect {
struct point pt1;
struct point pt2;
};
struct rect screen;
int co_x;
co_x = screen.pt1.x
(SP2) / ..
28/04/2010
80
4?)
pt2
pt1
81
;@ .4)@@W83.4
J=
34);=?
A)J
I(@@U
Q<A.A)1
(SP2) / ..
82
@@U
Q883.4
J=
-).)
@@U
Q3.4
J=
Q88-).)
@4.)IT)
C
@
83
(SP2) / ..
84
186 of 229
14
4?)
struct alignment {
int num1;
char ch;
int num2;
} example;
member
num1 ch
0
2
Byte Offset
num2
3
4?) 5.1
85
(SP2) / ..
86
@?@:C.)H^)@>C
87
(SP2) / ..
-9+N
9:R+^.N<:
;N9J R<:
:
+:
9 A- _9.N R<:
:
+:
9-+^,+-<
;
.N9J R<:
:
+:
9-. 1 .N9J R<:
:
+:
9-. +^ 0
28/04/2010
88
89
(SP2) / ..
90
187 of 229
15
JFN6A?=V;DKhK
(Pointer and Array)
int i;
400
402
404
i = 10;
10
400
402
404
<
@A 6.1 D@79:;<=~7E7MNhLNDUCD9;WJFN
V}@>?7D7
(SP2) / ..
91
(SP2) / ..
92
6.2 D
VDJFN
V}@JFN6A?
p
10 i
10 i
p 40 36
0 0
i
10 40
0
<
@A 6.2 D@79:;<=~7E7MNhLNDUCD9;WJFN
V}@JFN6A?
(SP2) / ..
93
(SP2) / ..
4?)
int *ip;
@@U Q ip :;. Q/4?> 4>
C1<?) Q5 int
28/04/2010
94
95
(SP2) / ..
96
188 of 229
16
int
int
ip
y
*ip
y
ip
*ip
iq
x = 1, y = 2;
*ip, *iq;
= &x;
= *ip;
= 0;
= 5;
= &y;
= 3;
= ip;
<
@A 6.3 DCDE78LMD=VD;MD7LMDJFN
JFN6A?
(SP2) / ..
400
402
ip
400
iq
400
402
ip
500
iq
502
97
(SP2) / ..
400
402
500
ip
400
500
502
iq
ip = &x;
(SP2) / ..
400
402
y = *ip;
(SP2) / ..
*ip = 0;
400
402
400
500
400 500
ip
iq
502
iq
28/04/2010
98
502
99
ip
(SP2) / ..
int x = 1, y = 2;
int *ip, *iq;
101
100
y = 5;
502
(SP2) / ..
102
189 of 229
17
400
402
ip
402
500
iq
ip = &y;
400
402
402
500
iq
402
502
(SP2) / ..
402
402
iq
(SP2) / ..
04)B@567
@)4@
>:;.@8H^)@>CQ88
By Value Q=H^)@>C
I0 (return) <A.
/?);T) ;@ .4)@:;.H^)@>C
@
=?Q=)Q=0@=8
?)H^)@>C ?@:C.
@@;T) B .4)(/4?> 4>3.
C?
105
(SP2) / ..
28/04/2010
104
4?) 6.1
(SP2) / ..
500
502
103
iq = ip;
ip
400
ip
(SP2) / ..
*ip = 3;
502
106
#include <stdio.h>
void swap (int *, int *);
107
(SP2) / ..
108
190 of 229
18
void main ( )
{
int x = 5, y = 10;
printf(wBefore swap : x = %dx, x, w, y = %d\nx, y);
swap ( &x, &y);
printf(wAfter swap : x = %dx, x, w, y = %d\nx, y);
}
(SP2) / ..
109
(SP2) / ..
110
in main ( )
x
y
4>@
>5/4?> 4>BC?
:;.H^)@>C
I=? :;.@8 Q)3.
<A. 04)B@4>@
>1 B@W8Q4AA
34) Q)3.
04
@=?Q=)
34)4>@
>b Dereferencing Operator ( * )
34) Q)3.
BIJ@=?/.4
@
:
in swap ( )
px
py
<
@A 6.4 B8WLNDBFF7K9;WDBMW;DKN7@KGG;h7KJ;KFGyzWK6F7
(SP2) / ..
111
(SP2) / ..
112
)%%
4>?>53.4
J=@W8C9A34)3.4
J=
5A?@
@:C.@8@() 4. )()
@8 QC
AA?@;=?
@()
;
04@ C Q34)@UT@65?:;.4)
20 . 4>?>:567B(;=@@34)
/4?> 4>3.
:C. @():A * 34)4>?>
I:C./4?> 4>3.
Q
int table[10];
@@(;A4>?>C04 table 4>?>5
int
C
@1);
A 10 1)Q table[0], table[1],
table[2], ... , table[9]
C
@5?:4>?>B
0
4 Q=
C
@ 9A.?B4?J ( Q;)34)3A
@U<.=8A.? 1
(SP2) / ..
28/04/2010
113
(SP2) / ..
114
191 of 229
19
'78# 1%%
table
table[0] table[1] table[2]
table[9]
115
(SP2) / ..
116
4?)
I4.)IT)
C
@9@ 5?:
4>?>4?)4
5?:3483 34)3A
<A.@U4>?><. Q @:C.4>?>
@B
@3.IT)
C
@:=@6d<-A?:C.
Q5 int
C?
(SP2) / ..
117
(SP2) / ..
4?)
#!-')($
:;. i, j, k Q5 int
(SP2) / ..
28/04/2010
118
119
(SP2) / ..
120
192 of 229
20
C
@34)4>?>4B53.4
J=
/012:A * @W<A. ;044B3.4
J=5
Enumeration C
#define TSIZE
10
#define NAMESIZE 20
#define ADDRSIZE 30
enum month { JAN, FEB, MAR, APR, MAY,
JUN, JUL, AUG, SEP, OCT,
NOV, DEC }
# include <stdio.h>
#define SIZE 5
main ( ) {
int k;
int table[SIZE];
for (k = 0; k < SIZE; k++)
scanf (w%dx, &table[k]);
for (k = SIZE-1; k >= 0; k--)
printf (w%d\nx, table[k]);
}
(SP2) / ..
121
(SP2) / ..
122
(SP2) / ..
123
(SP2) / ..
pa
a
a[0]
a[1]
a[2]
a[9]
28/04/2010
124
125
(SP2) / ..
126
193 of 229
21
pa
pa+
1
pa+
2
a
a[0]
a[1]
a[2]
a[9]
127
(SP2) / ..
(SP2) / ..
129
(SP2) / ..
28/04/2010
128
130
131
(SP2) / ..
132
194 of 229
22
133
(SP2) / ..
134
4?)
4@B@1?) 4BB@U/
4>5?:
H^)@>C strlen <A.: 2 =@6d 04 char *s Q88:
4?) ;044BB:C. char s[
s[ ] @W<A. -A?<B
:C.:=@6dQ@ /C?:J.<A. s
Q/4?> 4> Q=?)
I):A34)
4>?>:;.Q@H^)@>C@W<A. -A?<
B( .4))
C
@
Q@@W<A.C@
f (&a[2])
;04 f (a+2)
@)Q4AA34)
C
@ a[2] :;.@8
H^)@>C f @@UH^)@>C f
I(<A.-A?
@@U
f (int arr[ ]) { ......... }
;04 f (int *arr) { ............ }
(SP2) / ..
(SP2) / ..
135
136
28/04/2010
137
(SP2) / ..
138
195 of 229
23
139
(SP2) / ..
4?)
wI am a stringx
04
@:C. )
)B
@/01 :;?
B(
@8
?34))
)8@A.? 1 04)B@
=@6d@@W83.4
J=53.4
:
;?
B(B
@ 4@6 null ;04 e\0g 4.?
4/04:;.J.B9A
19A34)3.4
J= @B4)/01
A)@=B;
04@B4)/0134)3.4
J=5
4>?> 4>?>34) char
a m
141
(SP2) / ..
28/04/2010
142
)
)/8;W<A.
4<A.Q@3.4
:C.:H^)@>C printf ( ) C
printf ( wHello, world\nx );
H^)@>C printf ( ) B8/
4>
/4?> 4>C1<?)Q4AA34)3.4
J= ( Q;)
.34)4>?> Q=(3.4
1QA)44@
)49@d>QA)3.4
J=
2
\0
s t r i n g
(SP2) / ..
140
143
(SP2) / ..
144
196 of 229
24
145
(SP2) / ..
pmessage
H e l l
o ,
w o r l
\0
amessage H e l l
o ,
w o r l
\0
147
(SP2) / ..
4?) 6.6
28/04/2010
146
4?) 6.7
148
149
(SP2) / ..
150
197 of 229
25
4?)
@d@)4@
> Q s t r u c t B<
;
@8 s t r u c t
3A:;l 04)B@9@1)) Q struct B@(
Q :;
3T1
:H^)@>C 7T)B(:;.C.Q==04)/01;?
B( B:C./4?> 4>3.
C?Q@.^l;1
-A?)Q4AA34) Q struct
?)H^)@>C7T)84@
>
/4?> 4> 4@
>BC1<?)Q4AA
.34)
Q struct BC?:;.@()W3T1Q==04);?
B(
.4?=) Q
) .4))04;@
@=?Q=)4@
>
/4?> 4>C14?J : Q struct )
?)H^)@>CB=?
-A?4 -
(SP2) / ..
@4.)IT)
C
@
I(<A.-A?4.)
(*pp).x ;04 (*pp).y
151
(SP2) / ..
@4.)IT)
C
@4B3?4@=@6d;T)-A?:C.
04);
? ->
p /4?> 4> JQ88
@:C.A)1
p->member-of-structure
B
IQ=)-?@:C./4?> 4>
4.)
C
@34) struct B@ 4?)3.)8<A.
printf ( worigin is (%d, %d)\nx, pp->x, pp->y);
;
?; 9
) .4))04 (*pp).x B<
;
04@8 *pp.x 04)B@04);
? . B
=(A8
(lJ)@ * (B@Q=
;
? *pp.x B;
04@8@4.)
*(pp.x) 7T)B(:;.@
A
b
A/=A3T1
(SP2) / ..
153
(SP2) / ..
4?)
28/04/2010
154
152
155
(SP2) / ..
156
198 of 229
26
Q88 1
typedef struct {
int day;
int month;
int year;
} Date;
Date today;
Date *ptrdate;
(SP2) / ..
Q88 3
typedef struct {
int day;
int month;
int year;
} Date;
typedef Date *PtrDate;
PtrDate ptrdate;
(SP2) / ..
Q88 2
@@U
Q883.4
J=
-).)
struct date {
int day;
int month;
int year;
} *ptrdate;
@@U
Q3.4
J=
Q88-).)
@@U
Q pointer C1<
?) -).)
157
(SP2) / ..
@@U
Q883.4
J=
-).)
@@U5
Q pointer C1<?)
-).)
@@U
Q pointer C1<
?) -).)
159
(SP2) / ..
4?) 6.8
@4.)IT)
C
@34)-).)b Q/4?> 4>
ptrdate->day = 7;
if ( ptrdate->day == 31 && ptrdate->month == 12 ) .....
scanf ( w%dx, &ptrdate->year );
28/04/2010
160
#include <stdio.h>
struct date { /*date template */
int day;
int month;
int year;
};
typedef struct date Date;
typedef Date *PtrDate;
@4.)IT)
C
@-).)-A?:C.04 );
? ->
(*ptrdate).day = 7;
if ( (*ptrdate).day == 31 && (*ptrdate).month == 12 ) .....
scanf ( w%dx, &((*ptrdate).year) );
(SP2) / ..
158
161
(SP2) / ..
162
199 of 229
27
163
(SP2) / ..
164
Person staff[STAFFSIZE];
(SP2) / ..
165
(SP2) / ..
166
@?@:C.)
C
@8) :4>?>34)
-).)bH^)@>C
@?@:C.)
C
@9@ :4>?>34)
-).)bH^)@>C
@:C.3.4
J=
C
@Q = B4.)IT)-A?@4.)
b88AC;
044>?>< C H^)@>C
:C.:@/
/>C04
C
@89 B?@:C.-A?
print_person ( staff[k] );
28/04/2010
167
(SP2) / ..
168
200 of 229
28
@@(;A
.:;.@84>?>34)
-).)
@@(;A
.:;.@84>?>34)-).)
I(<A.
-A?
Person staff[ ] = { { wBloggsx, wJoex, MALE, 21 },
{ wSmithx, wJohnx, MALE, 30 },
{ wBlackx, wMaryx, FEMALE, 25 } };
(SP2) / ..
169
(SP2) / ..
Q ;@/
:;.@W83.4
J=Q48
34)
@UT@69@ B .4):C.4>?>;=?
3.
@?3.4) 4?)C@@W83.4
J=
Q4834)@UT@6 2 -A?
Q
4834)@481)
1 5 1)
#define NUMBER_OF_PAPERS 5
int student [ NUMBER_OF_PAPERS ];
/* int student[5]; */
student[0] student[1] student[2] student[3] student[4]
24.1 16.0
(SP2) / ..
171
(SP2) / ..
B
4)4>?> 2
:=@6d@48A.?
QI(row) Q=4=
>(column) -A?3.4
J=4.)4
)
Q@;
?IT) QI Q=3.4
J=IA
04 4=
>
marks[2][8]
marks[0][0]
marks[0][1]
marks[row][column]
marks[0][8]
marks[1][8]
marks[1][0]
marks[0][2]
marks[2][0]
row
28/04/2010
172
$0! 1
2
3
4 5
5.6 8.5 12.6 24.1 16.0
&
6.0 7.2 15.0 25.0 18.0
(SP2) / ..
170
column
173
(SP2) / ..
174
201 of 229
29
*$6)&' 2*
#include<stdio.h>
main()
{ float score[10][3];
int i,j;
printf(wPlease put score\nx);
for(i=0;i<10;i++)
for(j=0;j<3;j++)
scanf(w%fx,&score[i][j]);
}
175
(SP2) / ..
176
.,(( /3
28/04/2010
177
202 of 229
30
Stacks
Stack: what is it?
Applications
Implementation(s)
Computer Algorithm :
Analysis and Design
Krisana Chinnasarn, Ph.D.
Assistant Professor of Computer Science
Web: http://www.cs.buu.ac.th/~krisana
Stacks and Queues
Reading: Chap.3 Weiss
What is a stack?
B
top A
C
top B
A
D
top C
B
A
top
E
D
C
B
A
top
D
C
B
A
Array-based Stack
Implementation
Stack Applications
Real life
Pile of books
Plate trays
28/04/2010
203 of 229
Stack Implementation:
CreateS, isEmpty, isFull
Push
List-based Stack
Implementation: Push
Pop
10
Algorithm Analysis
Pop
push
pop
isEmpty
isFull
11
28/04/2010
O(?)
O(?)
O(?)
O(?)
12
204 of 229
Towers of Hanoi
13
14
Towers of Hanoi
Towers of Hanoi
15
16
Towers of Hanoi
Towers of Hanoi
17
28/04/2010
18
205 of 229
Towers of Hanoi
Towers of Hanoi
19
20
Towers of Hanoi
21
22
Queue
Problem complexity 2n
64 gold discs
Given 1 move a second
23
28/04/2010
24
206 of 229
Queue Applications
rear
front
B
A
C
rear B
front A
D
rear C
B
front A
rear
25
Array-based Queue
Implementation
As with the array-based stack implementation,
the array is of fixed size
J1
J1
J1
J2
J2
J2
J3
J3
J3
front
26
-1
-1
-1
-1
0
1
rear
D
C
front B
queue is empty
Job 1 is added
Job 2 is added
Job 3 is added
Job 1 is deleted
Job 2 is deleted
Implementation 2
27
28
Implementation 1:
createQ, isEmptyQ, isFullQ
Implementation 1:
enqueue
29
28/04/2010
30
207 of 229
Implementation 1:
dequeue
Implementation 2:
Wrapped Configuration
EMPTY QUEUE
[3]
[2]
[2]
[3]
J2
[1]
[4]
[0]
[1]
[5]
front = 0
rear = 0
J3
[4]
J1
[0]
[5]
front = 0
rear = 3
32
[3]
J2
[1]
FULL QUEUE
[2]
[3]
J8
J3
J9
J4 [4][1] J7
J1
J5
[0]
[5]
front =0
rear = 5
[4]
J6
[0]
J5
[5]
front =4
rear =3
34
List-based Queue
Implementation: Enqueue
28/04/2010
36
208 of 229
Algorithm Analysis
Dequeue
enqueue
dequeue
size
isEmpty
isFull
37
28/04/2010
O(?)
O(?)
O(?)
O(?)
O(?)
38
209 of 229
Computer Algorithm :
Analysis and Design
Church of
House of
House of
Supreme
Commons
Lords
Court
Cabinet
England
Ministers
Metropolitan
Council
police
Rural District
County Borough
Council
Council
Definition of Tree
Terminology
node (13)
degree of a node
leaf (terminal)
nonterminal
parent
children
sibling
degree of a tree (3)
ancestor
level of a node
height of a tree (4)
Level
3
2
E
2
0
40
30
L
2 1
F
30
A
C
G
2
31
0
3
H
30
D
I
30
33
4
28/04/2010
210 of 229
Tree Properties
Property
Number of nodes
Height
Root Node
Leaves
Interior nodes
Number of levels
Ancestors of H
Descendants of B
Siblings of E
Right subtree
A
C
G
H
Representation of Trees
Value
List Representation
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
The root comes first, followed by a list of sub-trees
data
link n
A Tree Node
data
left child right sibling
B
O
F
E
O
O
K
O
9
10
Tree ADT
Tree Implementation
typedef struct tnode {
int key;
struct tnode* lchild;
struct tnode* sibling;
} *ptnode;
- Create a tree with three nodes (one root & two children)
- Insert a new node (in tree with root R, as a new child at level L)
- Delete a node (in tree with root R, the first child at level L)
query methods
size() returns the number of nodes in the tree
isEmpty() - returns true if the tree is empty
elements() returns all elements
isRoot(p), isInternal(p), isExternal(p)
11
28/04/2010
12
211 of 229
Tree Traversal
Preorder
Preorder
Postorder
Algorithm preOrder(v)
visit node v
for each child w of v do
recursively perform preOrder(w)
Recursive definition
PREorder:
visit the root
traverse in preorder the children (subtrees)
POSTorder
traverse in postorder the children (subtrees)
visit the root
13
14
Postorder
Preorder Implementation
public void preorder(ptnode t) {
ptnode ptr;
display(t->key);
for(ptr = t->lchild; NULL != ptr; ptr = ptr->sibling) {
preorder(ptr);
}
}
postorder traversal
Algorithm postOrder(v)
for each child w of v do
recursively perform postOrder(w)
visit node v
15
16
Postorder Implementation
Binary Trees
17
28/04/2010
18
212 of 229
Example
E
F
G
H
19
20
Samples of Trees
BinTree makeBT(bt1, item, bt2)::= return a binary tree
whose left subtree is bt1, whose right subtree is bt2,
and whose root node contains the data item
Bintree leftChild(bt)::= if (IsEmpty(bt)) return error
else return the left subtree of bt
element data(bt)::= if (IsEmpty(bt)) return error
else return the data in the root node of bt
Bintree rightChild(bt)::= if (IsEmpty(bt)) return error
else return the right subtree of bt
1
B
B
Skewed Binary Tree
C
D
E
5
4
21
22
Prove by induction.
k
2i 1 = 2 k 1
i =1
23
28/04/2010
213 of 229
C
E
C
D
E
A [1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
.
[16]
Sequential
Representation
B
D
[1]
[2]
[3]
[4]
(1) waste space
(2) insertion/deletion [5]
[6]
problem
[7]
[8]
A
[9]
A
B
-C
---D
-.
E
26
A
B
C
D
E
F
G
H
I
Linked Representation
typedef struct tnode *ptnode;
typedef struct tnode {
int data;
ptnode left, right;
};
data
left
data
right
left
27
28
D
C
28/04/2010
right
inorder traversal
A/B*C*D+E
infix expression
preorder traversal
+**/ABCDE
prefix expression
postorder traversal
AB/C*D*E+
postfix expression
level order traversal
+*E*D/CAB
30
214 of 229
32
(using queue)
34
if (ptr) {
printf(%d, ptr->data);
if (ptr->left)
enqueue(front, &rear,
ptr->left);
if (ptr->right)
enqueue(front, &rear,
ptr->right);
}
else break;
+*E*D/CAB
}
on the left
from below
on the right
}
35
28/04/2010
36
215 of 229
eulerTour(node v) {
perform action for visiting node on the left;
if v is internal then
eulerTour(v->left);
perform action for visiting node from below;
if v is internal then
eulerTour(v->right);
perform action for visiting node on the right;
}
counter = 0
increment counter each time node is visited on the left
#descendants = counter when node is visited on the right
counter when node is visited on the left +
1
38
Application: Evaluation of
Expressions
+
*
*
D
C
/
B
inorder traversal
A/B*C*D+E
infix expression
preorder traversal
+**/ABCDE
prefix expression
postorder traversal
AB/C*D*E+
postfix expression
level order traversal
+*E*D/CAB
39
40
41
28/04/2010
42
216 of 229
Application:
Propositional Calculus Expression
A variable is an expression.
If x and y are expressions, then x, xy,
xy are expressions.
Parentheses can be used to alter the normal
order of evaluation ( > > ).
Example: x1 (x2 x3)
X1
Node Structure
value
X3
X1
43
data
X3
X2
left
Postorder Eval
right
45
46
28/04/2010
217 of 229
Contents
1. Definitions and Representations
Computer Algorithm :
Analysis and Design
Applications
What is a Graph?
V= {a,b,c,d,e}
JFK
E= {(a,b),(a,c),(a,d),
(b,e),(c,d),(c,e),
(d,e)}
CS16
electronic circuits
LAX
STL
HNL
DFW
FTL
Terminology:
Adjacent and Incident
Terminology:
Degree of a Vertex
n 1
e=(
d
0
28/04/2010
)/2
218 of 229
Terminology:
Path
Examples
0
3
3
3 1
2 3
6
1
1 G2 1
in:1, out: 1
in: 1, out: 2
in: 1, out: 0
3G1
directed graph
in-degree
out-degree
path: sequence of
vertices v1,v2,. . .vk such
that consecutive
vertices vi and vi+1 are
adjacent.
3
3
b
c
c
e d
d
abedc
G3
e
bedc
More Terminology
simple path: no repeated vertices
a
b
bec
c
e
cycle: simple path, except that the last vertex is the same as
the first vertex
a
connected
not connected
b
acda
c
e
10
Subgraphs Examples
0
1
2
3
G1
0
1
(i)
0
1
(i)
(iii)
(b) Some of the subgraph of G3
tree
3(iv)
(ii)
28/04/2010
(ii)
(iii)
(a) Some of the subgraph of G1
G3
More
tree
for est
tree
tree
(iv)
11
12
219 of 229
Connectivity
More Connectivity
n = #vertices
m = #edges
For a tree m = n - 1
n=5
m=4
If m < n - 1, G is
not connected
n=5
m=3
n=5
m = (5 4)/2 = 10
13
14
head
15
16
Graph Representations
objects: a nonempty set of vertices and a set of undirected edges, where each
edge is a pair of vertices
functions: for all graph Graph, v, v1 and v2 Vertices
Graph Create()::=return an empty graph
Graph InsertVertex(graph, v)::= return a graph with v inserted. v has no
incident edge.
Graph InsertEdge(graph, v1,v2)::= return a graph with new edge
between v1 and v2
Graph DeleteVertex(graph, v)::= return a graph in which v and all edges
incident to it are removed
Graph DeleteEdge(graph, v1, v2)::=return a graph in which the edge (v1, v2)
is removed
Boolean IsEmpty(graph)::= if (graph==empty graph) return TRUE
else return FALSE
List Adjacent(graph,v)::= return a list of all vertices that are adjacent to v
Adjacency Matrix
Adjacency Lists
17
28/04/2010
18
220 of 229
Adjacency Matrix
3
1
0
1
1
1
1
0
1
0 1 0
1 0 1
0 0 0
G2
G1
symmetric
undirected: n2/2
directed: n2
7
0
1
0
0
0
0
1 1
0 0
0 0
1 1
0 0
0 0
0 0
0 0
0 0 0 0 0
1 0 0 0 0
1 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 1 0 1 0
0 0 1 0 1
0 0 0 1 0
G4
19
1
1
1
0
1
20
n 1
j =0
j =0
#define MAX_VERTICES 50
typedef struct node *node_pointer;
typedef struct node {
int vertex;
struct node *link;
};
node_pointer graph[MAX_VERTICES];
int n=0; /* vertices currently in use */
21
0
1
1
0
0
0
1
0
3
3
3
2
0
2
G3
Some Operations
6
7
2
2
1
1
G1
0
1
2
3
0
1
2
3
22
1
2
0
1
2
3
4
5
6
7
1
0
0
1
5
4
5
6
G4
degree
2
3
3
2
of edges in a graph
determined in O(n+e)
out-degree
6
7
23 nodes
An undirected graph with n vertices and e edges ==> n head nodes and 2e list
28/04/2010
24
221 of 229
1. Definitions and
Representations
Representations
2
1
Adjacency Matrix
Adjacency Lists
Adjacency Multi-lists
3
1
G=(V, E, W)
2 1
weight
Vertex
LLLL
Greedy
2
3
NIL
NIL
O(eloge)
n2logn Why?
(2) Choose n1 edges from the sorted list of E
as long as the current selected edge does not
form a cycle with the previously-chosen edges
NIL
O(eloge + eG(n))
Kruskals algorithm !!!
Any Better way ?
3
NIL
25
26
Observation
A
1
3
15
1 3
20
2
5
C
G = (V, E, W)
27
28
B
1
C
2
29
28/04/2010
5
2
30
222 of 229
B
1
C
2
B
1
4
3
VT = { A }
VT = { A, C }
VF = { C, B, D }
VF = { B, D, E }
CE = { { A, C }, { A, B }, { A, D } }
3
1
4
CE = { { A, D }, { C, B }, { C, E } }
7
3
2
31
VT = { A, B, C }
B
1
C
2
B
1
4
3
32
5
2
VT = { A, B, C, D }
VF = { D, E }
VF = { E }
CE = { { B, D }, { B, E } }
1
CE = { { D, E } }
5
33
B
1
C
2
4
3
34
while vF do ;
e : = min - weight edge in CE ;
Update vT, vF, CE ;
end {while}
5
2
VT = { A, B, C, D, E }
VF =
No sorting !!!
Why this algorithm works ?
CE =
35
28/04/2010
36
223 of 229
x
y
[Proof]
T' T
T= (V, ET)
w
v
y
i) {x, y} ET trivial
ii) {x, y} ET
See the figure.
37
38
Time complexity
Kruskal
Dijkstra & Prim
2. { Main loop ; x has just been brought into the tree. Update fringe and candidates. }
while VT V and not stuck do
3.
{ Replace some candidate edges. }
for each fringe vertex y adjacent to x do
Update
if W(xy) < W(the candidate edge e incident with y) then
candidate
xy replaces e as the candidate edge for y ;
edges
end { if }
end{for}
4.
{ Find new fringe vertices and candidate edges. }
for each unseen y adjacent to x do
Add new fringe vertices
y is now a fringe vertex and xy is a candidate ;
and edges. (candidate)
end { for } ;
5.
{ Ready to choose next edge. }
if there are no candidates then stuck := true { no spanning tree} ;
else
6.
{ Choose next edge. }
Find a candidate edge e, with minimum weight ;
x := the fringe vertex incident with e.
Add x and e to the tree.
{ x and e are no longer fringe and candidate. }
Choose new edges.
end { if }
end { while }
39
40
(m=1)
(m=k)
28/04/2010
O (e + n 2 )
O(e + n log n)
O (e log e + eG (e))
41
O(|E|+n2)
42
224 of 229
fringeLink
B
3
6
7
G
3 2 C
F 5 1
I 4 H
2
6
8
2
E 1 D
4
2
A
3
parent
A
B
C
D
E
F
G
H
I
Lower Bound
adjacencyList
fringeWgt
status
(|E|)
Why ?
(1) a trivial lower bound O(|V| + |E|) = O(|E|)
(2) adversary argument
Every edge is required to be examined at least once.
Why ?
Suppose that some edge e is not examined at all.
(see below)
intree
nil
F
2
4
A
B
intree
fringe
unseen
(Adjacency lists
are not shown.
Nodes are
assumed to be in
alphabetical order
within each list.)
unseen
nil
F
C
H
7
3
3
1
A
A
G
G
fringe
intree
2
12 e
fringe
4
fringe
14
G = (V, E)
10 15
w(f) 2
fE/{e}
11
12
7
9
5
43
44
45
46
Why ?
Dijkstras Algorithm
1
Step 0
{ initialization }
v1 := 0
vj := w1j, j = 2, 3, 4,, n
p := {1}, T := {2, 3, 4,, n}
Step 1
{ Designation of permanent of label }
find k such that vk =min
{ vj }
j T
T := T \{k}
P := P U {k}
if T = , stop ( if k = d, stop )
Step 2
{ Revision of Tentative label }
vj := min{ vj, vk + wkj ) j T
go to step 1.
2
4
s1 d4
w12 = 1, w23 = 3, w24 = 2, w34 =2
v1 := 0, v2 = 1, v3 = , v4 =
P := {1}, T := {2, 3, 4}
min{vj} = min{v2, v3, v4}
1
jT
k=2
Is k=4 ? No !!!
T := {2,3,4}/{2} = {3,4}
P := P U{2} = {1,2}
v3 = min{v3, v2+w23} = min{, 1+3} = 4
v4 = min{v4, v2+w24} = min{, 1+2} = 3
min{vj} = min{v3, v4} = min{4, 3} = v4
T := {3,4}/{4} = {3}
P := P U{4} = {1,2,4}
Is k=4 ? Yes !!!
jT
k=4
47
28/04/2010
48
225 of 229
start
8 Queen Problem
Recursion
Q
y'
x'
Q
Q
Q
(1,1)
(2,3)
(3,5)
(1,2) (1,3)
(1,4) (1,5) (1,6) (1,7) (1,8)
(2,4)
(3,6)
(3,7)
(3,8)
y
(4,2)
(4,7)
(4,8)
e
(5,4)
min
(6,4)
(6,4)
(7,6)
(7,6)
49
50
C
H
E
A
A
B
B
C
1
C
1
2
C
1
1
2
1
C
1
2
1
2
1
Depth First Search
F
I
6
B
2
3 C
E
Depth First Search
A 7
1
52
A
F
I
6
F 6
C
3
H 5
4
1
4
3
7 C
E
Breadth First Search
DFS tree
1 A
3 B4
C
7
H
2
5
E
6
I
BFS tree
E
procedure BFS(AdjList : HeaderList; v : VertexType)
var
Q: Queue
w: VertexType
begin
Q:=
visit and mark v; insert v in Q;
while Q do
x := Front(Q);
for each unmarked vertex w adjacent to x do
visit and mark w;
insert w in Q;
end{for};
end{while}
end{BFS}
28/04/2010
54
226 of 229
B
2
undirected graphs
G
C
H
A 7
1
C
3
H 5
4
I
6
E
Tree edges
Back edges
Cross edges
Descendant edges
(forward)
Tree edges
Back edges
(no other types)
why not in
undirected graph?
down
right
55
56
start
destination
G
D
B
A
57
B DFS on B
A
Call DFS on G
C
B
A
DFS on C
Call DFS on D
B
A
Return to call on B
G
destination
start
BFS Process
rear
front
A
Initial call to BFS on A
Add A to queue
rear
rear
front
rear
front
D C
Dequeue A
Add B
Dequeue B
Add C, D
rear
front
D
Dequeue C
Nothing to add
front
G
Dequeue D
Add G
59
28/04/2010
227 of 229
10
,
Computer Algorithm: Analysis and Design,
, 2553
!"#$, #$%&'()*
+,& -. C,
% & $ ', 2553.
!"#$, #/0,1, ,
% & &
), 2552
28/04/2010
228 of 229
!" 11/2553
(")
(") *)+,-. )
)
)
********************
!
"#$"
% !%"!&' ()*+,-!.
/
,%/+$"%1.
2.
3.
4.
5.
6.
7.
8.
9.
( ./ /
(
.7
(
.(,
( $
( &%,?
: /+(
.
A8!
: /+(
./
..
( (!
,
%
:+;<%
,&,
88
"!
:
/
%C,?
(<
(.
((.34)
(
8)
( "
)
(
!,)
(
!,)
()
(
8)
()
()
28/04/2010
229 of 229