470 likes | 599 Vues
This lecture covers fundamental concepts in data structures. We'll explore primitive data types such as integers, characters, and floating points, defining their properties and operations. We'll delve into variables, their memory allocation, and how to manipulate them. Arrays will be analyzed as a powerful data structure for processing large data sets, including operations on one-dimensional and two-dimensional arrays. By understanding these concepts, students will build a strong foundation for programming and data management.
E N D
CS 111 Fundamental Data Structure อ. ดร. ชุรี เตชะวุฒิ Instructor: Lecture: M Th 14:30 – 15:45 CSB308 Tu F 8:00 – 9:15 CSB309 Tu F 9:30 – 10:45 CSB309
Outlines 1) Primitive data types 2) Variables 3) Arrays 4) Strings
2 values On/Off Light switch Computer type Size=2 Operation: turn on & turn off 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
int char float pointer array structure Primitive Data Types • 2 categories: Standard types atomic eg. Derived types complex structures using standard types eg.
Primitive Data Types : Integer • int (Integer) • Number without fraction part • Integral number int long int short int Define a number of bytes allocated for an integer by the compiler.
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 double long double Float types are defined from smallest to largest e
Name_1 Name_2 Variables • Variables are names for memory locations. • Each memory locations (or word) in a computer has an address. 000...000 … … 111...111 Addresses Memory
score Variables • e.g. score integer value of a score received in a test (88). 000...000 … … 88 111...110 111...111 Addresses Memory
code B char code; i 14 int i; balance 1000000000 long balance; float payRate; payRate 15.25 pi 3.1414926536 double pi; Program Memory Variables 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.
#include <stdio.h> void main ( ) { } char code; int m,n,result; result=m+n; ... Program Variables Declaration Operation e
Arrays • Imagine: Processing 20 numbers read – process – print Name_0 Name_1 … … … Name_ 18 Name_ 19 Memory 20 individual variables
Arrays • Processing large amount of data (e.g. 200, 2000 numbers) • Need a powerful data structure called ARRAY. • Properties of arrays: • 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 0 Number 1 Number[0] … Number[1] Number 18 … Number 19 Numbers Number[18] Number[19] Subscript form Numbers Index form
Loops can be used to: • read/write elements • add/subtract/multiply/divide elements • more complex processing such as calculating average indexes Arrays • Processing the array by using a powerful set of programming constructs called LOOPS. • 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?
0 0 ... 0 score[0] score[1] score[4] 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 >> While and For loops<<
Arrays • Draw a flow chart of processing an array of 20 numbers. - READ - PROCESS - PRINT
Arrays START for i=0 to 19 READ number [i] PROCESS 20 numbers for i=0 to 19 PRINT number [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
First dimension (rows) 0 1 2 3 0 1 2 Second dimension (columns) Arrays • Properties of two-dimensional arrays: • Same as one-dimensional arrays. • But, the indexes represent rows and columns.
Arrays User’s view row 0 row 1 00 01 02 10 11 12 [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] row 2 20 21 22 [2][1] [2][2] [2][0] 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 SUM0 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: หาค่าที่มากที่สุดและค่าที่น้อยที่สุดของเลขจำนวนเต็มชุดหนึ่งไม่ทราบจำนวน โดยเลขชุดนี้เป็นเลขที่มีจำนวนหลักไม่เกิน 3 หลัก แบบฝึกหัดจากเอกสารประกอบการสอน CS111อ.ดร.วัชรี จำปามูล
Simple problems : Exercise • ปัญหา 1: หาค่าที่น้อยที่สุดของเลข 3 จำนวน Start Let MIN0 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 MAX0 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] >=MAXThen 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) ต้องการเรียงข้อมูลในลิสต์ให้มีลำดับจากน้อยไปมาก นั่นคือเมื่อเรียงสำเร็จจะได้ลิสต์เป็น(-5, 1, 2, 8, 9) • การเปลี่ยนแปลงของข้อมูลในลิสต์เป็นดังนี้ 1 -5 2 8 9 -51 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 8 1 -5 2 9 18 -5 2 9 1-58 2 9 1 -5 28 9 ตัวอย่างจากเอกสารประกอบการสอน CS111อ.ดร.วัชรี จำปามูล
Bubble Sort : Example x[0] x[1] ……… x[2] x[3] x[4] x
Bubble Sort : Algorithm Module Bubble_Sort (x[ ], N) { X เป็นลิสต์หรือชุดของข้อมูลที่ต้องการเรียงจากน้อยไปมาก โดยมีจำนวนข้อมูลทั้งสิ้น N จำนวน } Exchanged True /* ตัวบันทึกว่าได้ทำการสลับค่า */ I 1 /* I เป็นตัวเก็บตำแหน่งของมูลที่กำลังสนใจ */ 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 EndWhile EndModule จากเอกสารประกอบการสอน CS111อ.ดร.วัชรี จำปามูล
Bubble Sort : Algorithm(using array) Module Bubble_Sort (x[ ], N) { X เป็นลิสต์หรือชุดของข้อมูลที่ต้องการเรียงจากน้อยไปมาก โดยมีจำนวนข้อมูลทั้งสิ้น N จำนวน } Exchanged True /* ตัวบันทึกว่าได้ทำการสลับค่า */ I 1 /* I เป็นตัวเก็บตำแหน่งของมูลที่กำลังสนใจ */ 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 EndWhile EndModule จากเอกสารประกอบการสอน CS111อ.ดร.วัชรี จำปามูล
Operations on Elements : Example • สมมติเรามีเมตริกซ์ ขนาด 2x3 ต้องการคูณข้อมูลในเมตริกซ์ให้มีค่าเพิ่มขึ้นสองเท่า • นั่นคือเมื่อคูณเสร็จจะได้เมตริกซ์ เป็น
Operations on Elements : Algorithm Module Multiply_Elements (x[ ][ ], M, N) { X เป็น 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 EndWhile EndModule
Operations on Elements : Example • สมมติเราต้องการบวกข้อมูลในสองเมตริกซ์ขนาด 3x3 • นั่นคือเมื่อบวกเสร็จจะได้เมตริกซ์ เป็น
Operations on Elements : Algorithm Module Add_Two_Matrixes (x[][], y[][], M, N) { X และ Y เป็น array ที่ต้องการบวกมีขนาด M*N Z เป็น array ที่ใช้เก็บผลบวกมีขนาด 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 EndWhile EndModule e
Operations on Elements : Exercise จงเขียนรหัสลำลองเพื่อเปรียบเทียบค่าของสองเมตริกซ์ต่อไปนี้ M1= M2= ผลที่ได้จะแสดงเมตริกซ์ที่ค่าของสมาชิกแสดงผลของการเปรียบเทียบ ถ้า สมาชิกใน M1 มีค่ามากกว่า สมาชิกใน M2 ให้แทนค่าด้วย 1 ถ้า สมาชิกใน M1มีค่าเท่ากับ สมาชิกใน M2 ให้แทนค่าด้วย 0 ถ้า สมาชิกใน M1มีค่าน้อยกว่า สมาชิกใน M2ให้แทนค่าด้วย -1 ผลลัพธ์ที่ได้คือ
Strings • Strings A sequence of characters. • Implementations Arrays (1 dimension) e.g. computer
Strings : Example • สมมติเราต้องการเปรียบเทียบ String สองชุดที่มีความยาวเท่ากัน ที่ได้จากการ input ผ่านแป้นพิมพ์ ตัวอย่างเช่น • ให้เปรียบเทียบโดยแสดงผลว่า String ทั้งสองชุดนั้นคือ String ตัวเดียวกันหรือไม่
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
Strings : Algorithm Module Input_data (X[], Size) { X เป็น array ที่เก็บ string ที่ได้จากการ input มีจำนวนเท่ากับSize} I 0 While (I < Size) Read X[I] I I + 1 EndWhile EndModule
Strings : Algorithm Module Compare_data (X[], Y[], Size) {X และ Y เป็น arrays ที่เก็บ string ที่ได้จากการ input มีจำนวนเท่ากับSize} Identical true /* เป็นตัวบันทึกว่า เปรียบเทียบแล้วเหมือนกัน */ I 0 /* เป็นตัวเก็บตำแหน่งอักขระที่ต้องการเปรียบเทียบ */ 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 เป็นตัวแปรบอกผลการเปรียบเทียบ } If (Same_String = true) then Display “Both strings are identical.” Else Display “Both strings are NOT identical.” Endif EndModule
Strings : Exercise จงเขียนรหัสลำลองเพื่อเปลี่ยนลำดับ (reverse)ของstring ใดๆ ที่รับเข้ามาจากการป้อนข้อมูลเข้า แล้วแสดงผลลัพธ์เป็น stringที่ทำการ reverseแล้ว เช่น STUDENT ผลลัพธ์ที่ได้คือ TNEDUTS หมายเหตุ: ให้ใช้วิธี calling pseudo code end