Instructor:
-
Upload
charlotte-walton -
Category
Documents
-
view
15 -
download
2
description
Transcript of Instructor:
Instructor:
M Th 14:30 – 15:45 CSB308
Tu F 8:00 – 9:15 CSB309
Tu F 9:30 – 10:45 CSB309
Lecture:
CS 111
อ. ดร. ชุ�ร� เตชุะวุ�ฒิ�
Fundamental Data Structure
Outlines
1) Primitive data types
2) Variables
3) Arrays
4) Strings
Primitive Data Types
Data type
Defines a set of values and a set of operations that can be applied to those values.
The set of values for each type = the domain of the type
Light switch
Computer type
2 values
On/Off
Size=2
Operation: turn on & turn off
Primitive Data Types
Standard types atomic
Derived types complex structures
using standard types
2 categories:
int char float
pointer array structureeg.
eg.
Primitive Data Types : Integer
int (Integer)
Number without fraction part
Integral number
int
Define a number of bytes allocated for an integer by the compiler.
short int
long int
Primitive Data Types : Character
char (Character)
A character is any value that can be represented in the computer’s alphabet.
ASCII alphabet.
Primitive Data Types : Floating Point
float (Floating point)
Number with a fraction part
float
Float types are defined from smallest to largest
long doubledouble
e
Variables
Variables are names for memory locations.
Each memory locations (or word) in a computer has an address.
Addresses
111...111
000...000
Memory
…
…
Name_1
Name_2
Variables
e.g. score integer value of a score received in a test (88).
Addresses
111...111
000...000
Memory……111...110score 88
Variables
char code;
int i;
long balance;
float payRate;
double pi;
Program
B
14
1000000000
15.25
3.1414926536
code
i
balance
payRate
pi
Memory
Variable’s type
Variable’s identifier
Variables
The type defines:
the range of the data that can be stored on the variable and
the number of bytes that can be allocated for that variable.
Variables
#include <stdio.h>
void main ( )
{
}
char code;
int m,n,result;
...
Program
result=m+n;
Operation
Declaration
e
Arrays
Imagine:
Processing 20 numbers read – process – print
Name_ 19
Name_0
Memory
…Name_1
Name_ 18…
…
20 individual variables
Arrays
Processing large amount of data (e.g. 200, 2000 numbers)
Properties of arrays:
Need a powerful data structure called ARRAY.
Fixed-size Sequenced collection of elements of the same data type Referring to the elements in the array as the 1st element, the 2nd element and so forth (subscripts). Naming
Arrays
Number 19
Number 0
Numbers
…
Number 1
Number 18
Subscript form
……
Number[19]
Number[0]
Numbers…
Number[1]
Number[18]
……
Index form
Arrays
Processing the array by using a powerful set of programming constructs called LOOPS.
Loops can be used to:
read/write elements add/subtract/multiply/divide elements
more complex processing such as calculating average
How can you write an instruction so that one time it refers to the 1st elements of an array and the next time it refers to another element? indexes
Arrays
How to use LOOPs for initializing elements in the array ?
Example: Initialize zero value to every elements in the array named SCORE
Let I 0
Repeat
Set 0 to score[I]
Add 1 to I
Until I = 5
0 0 0 ...score[0] score[1] score[4]
>> While and For loops <<
Arrays
Draw a flow chart of processing an array of 20 numbers.
- READ
- PROCESS
Arrays
START
for i=0 to 19for i=0 to 19
READ number [i]
READ number [i]
PROCESS 20 numbers
PROCESS 20 numbers
for i=0 to 19for i=0 to 19
PRINTnumber [i]
PRINTnumber [i]
STOP
Arrays
One-dimensional arrays:
Data are organized linearly in only one direction.
(Previously described)
Two-dimensional arrays:
Data are stored in rows and columns (table).
Multidimensional arrays
Arrays
Properties of two-dimensional arrays:
Same as one-dimensional arrays.
But, the indexes represent rows and columns.
01
23
0 1 2
First dimension
(rows)
Second dimension (columns)
Arrays
00 01 02
10 11 12
20 21 22
User’s view
01 0200 11 1210
21 2220
row 0 row 1
row 2
[0][0] [0][1] [0][2] [1][0] [1][1] [1][2]
[2][0] [2][1] [2][2] Memory’s view
Arrays : Implementation
One dimensional array
Simple problems
Bubble sort
Binary search
Merge Two dimensional array
Operation on elements
Simple problems : Example
• สมมติ�คะแนนสอบของน กศึ�กษา 10 คน ม�ดั งน��56 78 45 41 52 60 49 65 67 64
• ติ�องการทราบจำ�านวนน กศึ�กษา ท��ม�คะแนนเก�นค�าเฉลี่��ยว�เคราะห์#
ผลี่ลี่ พธ์#ท��ติ�องการ: จำ�านวนน กศึ�กษาท��ม�คะแนนเก�นค�าเฉลี่��ย (COUNT)
ข�อม'ลี่ท��ก�าห์นดัให์�: คะแนนสอบของน กศึ�กษา 10 คน (A(10)) ข�อม'ลี่ท��ติ�องทราบ: จำ�านวนน กศึ�กษา (N)
ผลี่รวมคะแนน = ผลี่บวกคะแนนสอบของน กศึ�กษาท �งห์มดั(SUM)
ว�ธ์�ค�านวณ: ค�าเฉลี่��ย = ผลี่รวมคะแนน / จำ�านวนน กศึ�กษา (MEAN)
Simple problems : Algorithm
Start Let SUM 0 Let I 1 Let N 10
/* Read score to array */ Repeat Read A[I] Add 1 to I Until I = N
/* Find summation of scores */ For I = 0 to N Read A[I] Add A[I] to SUM EndFor
Simple problems : Algorithm
/* Find average scores */ Compute MEAN SUM / N
/* Find No. of students */ For I = 1 to N If A[I] > MEAN Then Add 1 to COUNT EndIf EndFor
/* Display result */ Display “No. of student is”, COUNT
End
Simple problems : Exercise
• จำงใช้�ติ วแปรช้-ดั (arrays) เพ.�อเข�ยนอ ลี่กอร�ท�ม ผ งงานแลี่ะรห์ สลี่�าลี่องของป/ญห์าติ�อไปน��ป/ญห์า 1: ห์าค�าท��มากท��ส-ดัแลี่ะค�าท��น�อยท��ส-ดัของเลี่ข 3 จำ�านวนป/ญห์า 2: ห์าค�าท��มากท��ส-ดัแลี่ะค�าท��น�อยท��ส-ดัของเลี่ข N จำ�านวนป/ญห์า 3: ห์าค�าท��มากท��ส-ดัแลี่ะค�าท��น�อยท��ส-ดัของเลี่ขจำ�านวนเติ2มช้-ดัห์น��งไม�ทราบจำ�านวน โดัยเลี่ขช้-ดัน��เป4นเลี่ขท��ม�จำ�านวนห์ลี่ กไม�เก�น 3 ห์ลี่ ก
แบบฝึ�กหั�ดจากเอกสารประกอบการสอน CS111 อ.ดร.วุ�ชุร� จ�าปามู�ล
Simple problems : Exercise
• ป/ญห์า 1: ห์าค�าท��น�อยท��ส-ดัของเลี่ข 3 จำ�านวน
Start Let MIN 0 Let I 1 Let N 3
/* Read number to array */ Repeat Read NUM[I] Add 1 to I Until I = N
Simple problems : Exercise
/* Compare numbers in array */ Let MIN NUM[0] Repeat Read NUM[I] If NUM[I] <= MIN Then Let MIN NUM[I] EndIf Add 1 to I Until I = N
/* Display result */ Display “Minimum number is”, MIN
End
Simple problems : Exercise
• ป/ญห์า 1: ห์าค�าท��มากท��ส-ดัของเลี่ข 3 จำ�านวน
Start Let MAX 0 Let I 1 Let N 3
/* Read number to array */ Repeat Read NUM[I] Add 1 to I Until I = N
Simple problems : Exercise
/* Compare numbers in array */ Let MAX NUM[0] Repeat Read NUM[I] If NUM[I] >= MAX Then Let MAX NUM[I] EndIf Add 1 to I Until I = N
/* Display result */ Display “Maximum number is”, MAX
End
Bubble Sort : Example
• สมมติ�เราม�ลี่�สติ# (8, 1, -5, 2, 9) ติ�องการเร�ยงข�อม'ลี่ในลี่�สติ#ให์�ม�ลี่�าดั บจำากน�อยไปมาก น �นค.อเม.�อเร�ยงส�าเร2จำจำะไดั�ลี่�สติ#เป4น (-5 , 1, 2, 8, 9)
• การเปลี่��ยนแปลี่งของข�อม'ลี่ในลี่�สติ#เป4นดั งน��
8 1 -5 2 9
1 8 -5 2 9
1 -5 8 2 9
1 -5 2 8 9
1 -5 2 8 9
-5 1 2 8 9
-5 1 2 8 9
-5 1 2 8 9
-5 1 2 8 9
-5 1 2 8 9
-5 1 2 8 9
-5 1 2 8 9
ต�วุอย่�างจากเอกสารประกอบการสอน CS111 อ.ดร.วุ�ชุร� จ�าปามู�ล
Bubble Sort : Example
x[3]
x[0]
x
x[1]
x[4]
8
1
-5
2
9
x[2]
8
1
-5
2
9
1
8
-5
2
9
1
-5
8
2
9
1
-5
2
8
9
………
-5
1
2
8
9
Bubble Sort : AlgorithmModule Bubble_Sort (x[ ], N){ X เป4นลี่�สติ#ห์ร.อช้-ดัของข�อม'ลี่ท��ติ�องการเร�ยงจำากน�อยไปมาก โดัยม�
จำ�านวนข�อม'ลี่ท �งส��น N จำ�านวน } Exchanged True /* ติ วบ นท�กว�าไดั�ท�าการสลี่ บค�า */ I 1 /* I เป4นติ วเก2บติ�าแห์น�งของม'ลี่ท��ก�าลี่ ง
สนใจำ */ While (I < N and Exchanged) Exchanged False J 1 While (J < N) If (X[J] > X[J+1]) then Temp X[J] X[J] X[J+1] X[J+1] Temp
Exchanged True Endif J J + 1 EndWhile I I + 1 EndWhileEndModule
จากเอกสารประกอบการสอน CS111 อ.ดร.วุ�ชุร� จ�าปามู�ล
Bubble Sort : Algorithm (using array)Module Bubble_Sort (x[ ], N){ X เป4นลี่�สติ#ห์ร.อช้-ดัของข�อม'ลี่ท��ติ�องการเร�ยงจำากน�อยไปมาก โดัยม�
จำ�านวนข�อม'ลี่ท �งส��น N จำ�านวน } Exchanged True /* ติ วบ นท�กว�าไดั�ท�าการสลี่ บค�า */ I 1 /* I เป4นติ วเก2บติ�าแห์น�งของม'ลี่ท��ก�าลี่ ง
สนใจำ */ While (I < N and Exchanged) Exchanged False J 0 While (J < N) If (X[J] > X[J+1]) then Temp X[J] X[J] X[J+1] X[J+1] Temp
Exchanged True Endif J J + 1 EndWhile I I + 1 EndWhileEndModule
จากเอกสารประกอบการสอน CS111 อ.ดร.วุ�ชุร� จ�าปามู�ล
Operations on Elements : Example
• สมมติ�เราม�เมติร�กซ์# ขนาดั 2x3 ติ�องการค'ณข�อม'ลี่ในเมติร�กซ์#ให์�ม�ค�าเพ��มข��นสองเท�า
3 1 5
0 7 8
• น �นค.อเม.�อค'ณเสร2จำจำะไดั�เมติร�กซ์# เป4น6 2 10
0 14 16
Module Multiply_Elements (x[ ][ ], M, N){ X เป4น array ท��ติ�องการค'ณ 2 โดัยม�จำ�านวนข�อม'ลี่ท �งส��น M*N จำ�านวน } I 0 While (I < M)
J 0 While (J < N)
Temp X[I][J] * 2 X[I][J] Temp
J J + 1 EndWhile I I + 1 EndWhileEndModule
Operations on Elements : Algorithm
Operations on Elements : Example
• สมมติ�เราติ�องการบวกข�อม'ลี่ในสองเมติร�กซ์#ขนาดั 3x3
4 1 2
3 0 2
7 2 6
• น �นค.อเม.�อบวกเสร2จำจำะไดั�เมติร�กซ์# เป4น
3 2 2
1 5 6
2 5 1
7 3 4
4 5 8
9 7 7
Module Add_Two_Matrixes (x[][], y[][], M, N){ X แลี่ะ Y เป4น array ท��ติ�องการบวกม�ขนาดั M*N Z เป4น array ท��ใช้�เก2บผลี่บวกม�ขนาดั M*N} I 0 While (I < M)
J 0 While (J < N)
Temp X[I][J] + Y[I][J] Z[I][J] Temp
J J + 1 EndWhile I I + 1 EndWhileEndModule
Operations on Elements : Algorithm
e
จำงเข�ยนรห์ สลี่�าลี่อง เพ.�อเปร�ยบเท�ยบค�าของสองเมติร�กซ์#ติ�อไปน�� 24 12 3
8 7 1
4 5 55
1 2 3
8 23 4
8 9 5
M1= M2=
ผลี่ท��ไดั�จำะแสดังเมติร�กซ์#ท��ค�าของสมาช้�กแสดังผลี่ของการเปร�ยบเท�ยบถ้�า สมาช้�กใน M1 ม�ค�ามากกว�า สมาช้�กใน M2 ให์�แทนค�าดั�วย 1ถ้�า สมาช้�กใน M1 ม�ค�าเท�าก บ สมาช้�กใน M2 ให์�แทนค�าดั�วย 0ถ้�า สมาช้�กใน M1 ม�ค�าน�อยกว�า สมาช้�กใน M2 ให์�แทนค�าดั�วย -1
1 1 0
0 -1 -1
-1 -1 1
ผลี่ลี่ พธ์#ท��ไดั�ค.อ
Operations on Elements : Exercise
Strings
Strings
A sequence of characters.
Implementations
Arrays (1 dimension)
c o m p u t e r
e.g. computer
Strings : Example
• สมมติ�เราติ�องการเปร�ยบเท�ยบ String สองช้-ดัท��ม�ความยาวเท�าก น ท��ไดั�จำากการ input ผ�านแป8นพ�มพ# ติ วอย�างเช้�น
F R I D A Y
• ให์�เปร�ยบเท�ยบโดัยแสดังผลี่ว�า String ท �งสองช้-ดัน �นค.อ String ติ วเดั�ยวก นห์ร.อไม�
M O N D A Y
Strings : Example
Start /* this is a calling pseudo code */
Call input_data(A[ ], N)
Call input_data(B[ ], N)
Call Compare_data(A[ ], B[ ], N)
Call Display_result(Identical)
End
Module Input_data (X[], Size){ X เป4น array ท��เก2บ string ท��ไดั�จำากการ input ม�จำ�านวนเท�าก บSize} I 0 While (I < Size) Read X[I] I I + 1 EndWhileEndModule
Strings : Algorithm
Module Compare_data (X[], Y[], Size){X แลี่ะ Y เป4น arrays ท��เก2บ string ท��ไดั�จำากการ input ม�จำ�านวน
เท�าก บSize} Identical true /* เป4นติ วบ นท�กว�า เปร�ยบเท�ยบแลี่�วเห์ม.อนก น */ I 0 /* เป4นติ วเก2บติ�าแห์น�งอ กขระท��ติ�องการเปร�ยบเท�ยบ
*/ While (I < Size and Identical) If (X[I] = Y[I]) then Identical true else Identical false Endif I I + 1 EndWhile Call Display_result(Identical)EndModule
Strings : Algorithm
Module Display_result(Same_String) { Same_String เป4นติ วแปรบอกผลี่การเปร�ยบเท�ยบ } If (Same_String = true) then Display “Both strings are identical.” Else Display “Both strings are NOT identical.” EndifEndModule
Strings : Algorithm
จำงเข�ยนรห์ สลี่�าลี่อง เพ.�อเปลี่��ยนลี่�าดั บ (reverse) ของ string ใดัๆ ท��ร บเข�ามาจำากการป8อนข�อม'ลี่เข�า แลี่�วแสดังผลี่ลี่ พธ์#เป4น string ท��ท�าการ reverse แลี่�ว
เช้�น STUDENT
ผลี่ลี่ พธ์#ท��ไดั�ค.อ TNEDUTS
ห์มายเห์ติ-: ให์�ใช้�ว�ธ์� calling pseudo code
end
Strings : Exercise