งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Chapter 10 Arrays Dept of Computer Engineering Khon Kaen University.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Chapter 10 Arrays Dept of Computer Engineering Khon Kaen University."— ใบสำเนางานนำเสนอ:

1 Chapter 10 Arrays Dept of Computer Engineering Khon Kaen University

2 178110: Computer Programming (II/2546) 2 แนะนำ อาร์เรย์ คือ ลำดับของออปเจ็กต์ประเภทเดียวกัน ออปเจ็กต์ดังกล่าวเรียกว่า สมาชิก (elements) ของอาร์เรย์ และมีหมายเลขกำกับเป็น 0, 1, 2,.... ตัวเลขกำกับเหล่านี้เรียกว่า index หรือ subscript ของอาร์เรย์ อาร์เรย์อาจเขียนอยู่ในรูปแบบ a0, a1, a2,... เช่นเดียวกับในคณิตศาสตร์ ตัว index จะเป็นตัวบอกถึงตำแหน่งที่อยู่ของ สมาชิกภายในอาร์เรย์ ซึ่งทำให้เราสามารถเข้าถึง สมาชิกของอาร์เรย์ได้โดยตรง

3 178110: Computer Programming (II/2546) 3 แนะนำ (ต่อ) อาร์เรย์ที่มีชื่อว่า a นั้นก็จะมี a[0] เป็นชื่อของ สมาชิกที่ตำแหน่ง 0 และ a[1] เป็นชื่อของ สมาชิกของอาร์เรย์ ณ ตำแหน่ง 1 ฯลฯ โดยทั่วไป สมาชิกของตัวที่ i ของอาร์เรย์จะอยู่ ณ ตำแหน่งที่ _i-1__ อาร์เรย์ที่มีจำนวนสมาชิก n ตัว ก็จะมีชื่อเป็น a[_0_], a[_1_], a[_2_],..., a[__n-1__];

4 178110: Computer Programming (II/2546) 4 แนะนำ (ต่อ) ปกติแล้วมักจะแสดงแผนภาพแทนอาร์เรย์ด้วย ลำดับของช่องเก็บที่ต่อเนื่องกัน ปกติอาร์เรย์จะถูกจัดเก็บอยู่ในพื้นที่ หน่วยความจำที่ต่อกันไปเช่นเดียวกับแผนภาพ ข้างบนนี้ด้วย การให้หมายเลขกำกับดัชนีของอาร์เรย์สำหรับ สมาชิกตัวที่ i - 1 เป็น i นั้นเรียกว่า zero- based indexing คือ เริ่มต้น index ด้วย 0 11.1133.3355.55 a 012

5 178110: Computer Programming (II/2546) 5 Example 1 int main() { int s1, s2, s3, s4; s1 = 1; s2 = 2; s3 = 3; s4 = 4; }

6 178110: Computer Programming (II/2546) 6 Example 2 int main() { const int SIZE = 4; int s[SIZE]; for (int i = 0; i < SIZE; i++) s[i] = i + 1; }

7 178110: Computer Programming (II/2546) 7 การประมวลผลอาร์เรย์ อาร์เรย์เป็นตัวแปรที่เรียกว่า composite object กล่าวคือมันจะประกอบด้วยสมาชิก จำนวนหลาย ๆ ตัวที่มีค่าเป็นอิสระจากกัน ซึ่ง ต่างจากตัวแปรปกติประเภทพื้นฐานที่เราพบ เห็นมาแล้วซึ่งเรียกว่า scalar object โปรแกรมคอมพิวเตอร์ที่ใช้งานทั่วไปมักจะมีการ ใช้อาร์เรย์เสมอทั้งนี้เนื่องจากว่าเราสามารถใช้ ชื่อเพียงชื่อเดียวแล้วกำกับด้วยค่าของดัชนี สำหรับตัวแปรจำนวนมากแทนที่จะต้องใช้ชื่อ จำนวนมาก ๆ

8 178110: Computer Programming (II/2546) 8 การประมวลผลอาร์เรย์ (ต่อ) type array-name[array-size]; เมื่อ type คือ ประเภทของสมาชิกของอาร์เรย์ และ array-size คือจำนวนสมาชิกของอาร์เรย์ การประกาศตัวแปรใน Example2 คือ int s[SIZE]; เป็นการประกาศตัวแปร a ให้เป็นอาร์เรย์ที่มีสมาชิก SIZE ตัว และสมาชิกมีประเภทเป็น int Can SIZE be a floating point number? No Can SIZE be a negative number or a zero? No

9 178110: Computer Programming (II/2546) 9 การกำหนดค่าเริ่มต้นให้อาร์เรย์ การกำหนดค่าเริ่มแรกให้อาร์เรย์สามารถทำได้โดยการ ใช้ initializer list เพียงตัวเดียวได้ ดังเช่น float a[] = { 22.2, 44.4, 66.6, 88.8 };หรือ float a[4] = { 22.2, 44.4, 66.6, 88.8 }; ค่าภายในลิสต์จะถูกกำหนดค่าให้กับสมาชิกของ อาร์เรย์เรียงลำดับกันไปโดยค่าแรกจะถูกกำหนดให้แก่ สมาชิกหมายเลขลำดับที่ 0 และต่อ ๆ ไป ขนาดของอาร์เรย์จะถูกกำหนดให้เท่ากับจำนวนค่าใน ลิสต์โดยอัตโนมัติ ดังนั้นจากข้างบนจึงเป็นการประกาศให้ a เป็นอาร์เรย์ ขนาดทีมีกี่สมาชิก

10 178110: Computer Programming (II/2546) 10 Example 3 #include using namespace std; int main() { // tests simple array processing: float a[] = { 22.2, 44.4, 66.6 }; int size = sizeof(a)/sizeof(float); for (int i=0; i<size; i++) cout << "\ta[" << i << "] = " << a[i] << endl; return 0; } What are the values of size, sizeof(a), sizeof(float)? 4, 12, 4

11 178110: Computer Programming (II/2546) 11 Example 4 #include using namespace std; int main() { // tests simple array processing: float a[7] = { 22.2, 44.4, 66.6 }; return 0; } What are the values of a[3], a[4], …, a[6]? 0

12 178110: Computer Programming (II/2546) 12 การกำหนดค่าเริ่มต้นให้อาร์เรย์ ( ต่อ ) float a[3] = { 2.2, 4.4, 6.6, 8.8 }; Does the above statement cause an error? ค่าเริ่มแรกของสมาชิกของอาร์เรย์กำหนดให้เป็นศูนย์ทั้งหมดได้ ด้วยการใช้ initializer list ว่าง float a[] = { 0, 0, 0}; float a[3] = { 0,}; float a[3] = { 0, 0, 0,}; What are the values initialized for this declaration? float a[3]; - Unknown, depending on a compiler and a machine being used

13 178110: Computer Programming (II/2546) 13 Example 5 int main() { const int SIZE = 2; float a[SIZE]; for (int i = 0; i < SIZE; i++) cout << “a[“ << i << “] = “ << a[i] << endl; } What are the output? a[0] = 3.58732e-43 a[1] = 1.4013e-45

14 178110: Computer Programming (II/2546) 14 การกำหนดค่าเริ่มต้นให้อาร์เรย์ ( ต่อ ) float a[7] = { 2.2, 4.4, 6.6 }; float b[7] = { 3.3, 5.5, 7.7 }; b = a; Can we do the above? No, we cannot copy the array that is allocated statically Can we do “b[5] = a[2];” Yes, we can copy each individual member of an array

15 178110: Computer Programming (II/2546) 15 การกำหนดค่าเริ่มต้นให้อาร์เรย์ ( ต่อ ) Can we do these below statements? float a[7] = { 2.2, 4.4, 6.6 }; float b[7] = a; // ERROR: arrays cannot be used as initializaters! C++ ไม่อนุญาติให้ใช้อาร์เรย์ตัวหนึ่งไปเป็นตัว กำหนดค่าเริ่มแรกให้กับอาร์เรย์อีกตัวหนึ่ง

16 178110: Computer Programming (II/2546) 16 Example 6 #include using namespace std; int main() { const int SIZE=4; float a[SIZE] = { 33.3, 44.4, 55.5, 66.6 }; for (int i=0; i<7; i++) cout << "\ta[" << i << "] = " << a[i] << endl; return 0; } Will the above program generate error? No, but it will display un-initialized values

17 178110: Computer Programming (II/2546) 17 Example 6 Output a[0] = 33.3 a[1] = 44.4 a[2] = 55.5 a[3] = 66.6 a[4] = 1.74477e-39 a[5] = 6.01484e-39 a[6] = 1.4013e-45

18 178110: Computer Programming (II/2546) 18 Example 7 #include using namespace std; int main() { float a[] = { 22.2, 44.4, 66.6 }; a[30] = 88.8; return 0; } Does this generate an error? Yes, it is a run-time error

19 178110: Computer Programming (II/2546) 19 การส่งผ่านอาร์เรย์ให้ฟังก์ชัน ประโยคคำสั่ง float a[] (จะตามด้วย initializer list หรือไม่ก็ตาม) ซึ่งบอกข้อมูลอยู่ 2 อย่าง ชื่อของอาร์เรย์ คือ a และสมาชิกของอาร์เรย์ มีประเภทเป็น float ความจริงสัญลักษณ์ a นั้นเป็นตัวเก็บตำแหน่งที่ อยู่ในหน่วยความจำ (memory address) ของ สมาชิกตัวแรกของอาร์เรย์ a คือ a[0] ซึ่งก็ได้ สารสนเทศที่คอมไพล์เลอร์ต้องการครบถ้วน แล้ว ดังนั้นจึงไม่จำเป็นต้องบอกขนาดของ อาร์เรย์ให้คอมไพล์เลอร์อีก และและในการส่ง อาร์เรย์เป็นพารามิเตอร์ของฟังก์ชันนั้น C++ ก็ ต้องการสารสนเทศเช่นเดียวกับที่คอมไพล์เลอร์ ต้องการ

20 178110: Computer Programming (II/2546) 20 Example 8 int sum(int a[], int n); int main() { // tests using array parameters: int a[] = { 11, 33, 55, 77 }; int size = sizeof(a)/sizeof(int); cout << "sum(a,size) = " << sum(a,size) << endl; return 0; } int sum(int a[], int n) { // sum of the first n elements of a: int sum=0; for (int i=0; i<n; i++) sum += a[i]; return sum; }

21 178110: Computer Programming (II/2546) 21 การส่งผ่านอาร์เรย์ให้ฟังก์ชัน ค่าของชื่ออาร์เรย์ (ในที่นี้คือ a) ก็คือตำแหน่ง หน่วยความจำของสมาชิกตัวแรก (คือ a[0]) ในอาร์เรย์ ฟังก์ชันจะใช้ตำแหน่งหน่วยความจำ นั้นในการเข้าถึงหรือเปลี่ยนแปลงค่าภายใน อาร์เรย์ ดังนั้นการส่งผ่านอาร์เรย์จึงมีผลเหมือนกับ pass by reference หรือ pass by value? Pass by reference ฟังก์ชันสามารถปรับเปลี่ยนค่าของสมาชิกใน อาร์เรย์ได้ ค่าที่ส่งนั้นเป็นตำแหน่ง หน่วยความจำซึ่งทำให้ฟังก์ชันมีสารสนเทศ พอเพียงที่จะเข้าถึงสมาชิกของ อาร์ เรย์ได้

22 178110: Computer Programming (II/2546) 22 Example 9 int main() { // prints the address stored in an array variable: int a[] = { 22, 44, 66, 88 }; cout << "a = " << a << endl; // a contains the memory address of a[0] for (int i = 0; i < 4; i++) cout << "Address of a[" << i << "] = " << &a[i] << " " << endl; return 0; }

23 178110: Computer Programming (II/2546) 23 Example 9 Output a = 0012FF7C Address of a[0] = 0012FF7C Address of a[1] = 0012FF80 Address of a[2] = 0012FF84 Address of a[3] = 0012FF88

24 178110: Computer Programming (II/2546) 24 ตำแหน่งของสมาชิกในอาร์เรย์ float a[n]; // กำหนดให้ float ใช้เนื้อที่ 4 ไบต์ a[1] อยู่ห่างจาก a[0] ไป 4 ไบต์ a[2] อยู่ห่างจาก a[0] ไป 8 ไบต์ a[n] อยู่ห่างจาก a[0] ไป _____ ไบต์ ถ้า a[0] เริ่มที่ตำแหน่ง 0x0064fdbc (คือ 6,618,556 ในเลขฐานสิบ) ดังนั้น ค่าตำแหน่งของ a[3] คือ 0x 0064fdc8 (คือ 6,618556 + 12 = 6,618,568) ค่า 12 เรียกว่าค่า offset ของสมาชิก a[3] ชื่ออาร์เรย์ (คือค่าตำแหน่งหน่วยความจำ) เป็นค่าคงที่

25 178110: Computer Programming (II/2546) 25 กรรมวิธีการค้นหาเชิงเส้นตรง กรรมวิธีการค้นหาเชิงเส้นตรง (The Linear Search Algorithm) กรรมวิธีที่ดูจะง่ายที่สุดในการค้นหาออป เจ็กต์ใด ๆ ในอาร์เรย์คือการเริ่มต้นจาก สมาชิกตัวแรก และตรวจสอบไปทีละตัว จนกว่าจะพบออปเจ็กต์ที่ต้องการ กรรมวิธีเช่นนี้เรียกว่า Linear Search

26 178110: Computer Programming (II/2546) 26 Example 10 int search(int x, int a[], int n) { // linear search: for (int i=0; i<n; i++) if (_a[i] == x_) return _i__; return n; } int main() { int a[] = { 22, 44, 66, 88, 44, 66, 55 }; cout << “search(44,a,7) = " << search(44,a,7) << endl; } Output: search(44,a,7) = 1 What is the output of search(50,a,7)?

27 178110: Computer Programming (II/2546) 27 Example 11 void search(int& found, int& location, int a[], int n, int target) { found = location = 0; while (_!found_______ && _location < n_) found = (a[location++] == target); --location; } int main() {int a[] = {55, 22, 99, 66, 44, 88, 33, 77}, found, loc; search(found, loc, a, 8, 44);  found = ? loc = ? Search(found, loc, a, 8, 100);  found = ? loc = ?

28 178110: Computer Programming (II/2546) 28 กรรมวิธีการจัดเรียงแบบ Bubble กรรมวิธีการจัดเรียงแบบ Bubble (The Bubble Sort Algorithm) เราสามารถใช้กรรมวิธีการค้นหาที่มี ประสิทธิภาพมากกว่าการค้นหาด้วยการใช้การ ค้นหาเชิงเส้นตรง โดยการจัดเรียงข้อมูลใน อาร์เรย์ก่อน การทำงานของ Bubble Sort จะมีการ เปรียบเทียบค่าของคู่ของข้อมูลที่อยู่ติดกันและ ทำการสลับให้ข้อมูลที่มีค่ามากกว่าไปอยู่ ทางด้านขวา(ในกรณีที่เรียงจากน้อยไปหามาก)

29 178110: Computer Programming (II/2546) 29 Bubble Sort Testing Program int main() { // tests the sort() function: x float a[] = {55.5, 22.5, 99.9, 66.6}; int num_a = sizeof(a)/sizeof(float); print(a,num_a); sort(a,num_a); print(a,num_a); } What is the output?

30 178110: Computer Programming (II/2546) 30 Printing An Array float a[] = {55.5, 22.5, 99.9, 66.6}; print(a,4): 55.5, 22.5, 99.9, 66.6 void print(float a[], int n) { for (int i=0; _i < (n-1)____; __i++______) cout << _a[i]_____ << “_,__ "; cout << _a[n-1]______ << endl; }

31 178110: Computer Programming (II/2546) 31 Bubble Sort Bubble sort needs swap function // exchange the values of x and y void swap(_float&_ x, _float&_ y) { float temp = x; x = _y_; y = _temp_; }

32 178110: Computer Programming (II/2546) 32 Bubble Sort (Cont.) void sort(float a[], int n) { // bubble sort: for (int i=1; i<n; i++) // bubble up max{a[0..n-i]}: for (int j=0; j<n-i; j++) if (_____ > ________) swap(________, ________); }

33 178110: Computer Programming (II/2546) 33 กรรมวิธีการค้นหาแบบไบนารี่ กรรมวิธีการค้นหาแบบไบนารี่ (The Binary Search Algorithm) การค้นหาค่าที่ต้องการด้วยวิธีไบนารี่ใช้กรรมวิธี ที่เรียกว่า "divide and conquer" กล่าวคือจะ แบ่งอาร์เรย์ออกเป็นสองส่วนแล้วให้ความสนใจ เฉพาะกับส่วนที่คาดว่าจะมีค่าที่ต้องการค้นหา อยู่ การใช้การค้นหาแบบไบนารี่มีประสิทธิภาพ มากกว่าแบบเชิงเส้นทั้งนี้เพราะว่าการทำงานใน ลูปแต่ละรอบนั้นจะสามารถลดจำนวนข้อมูลลง ไปครึ่งหนึ่ง

34 178110: Computer Programming (II/2546) 34 Binary Search Testing Program int main() { // tests the search() function: int a[] = { 22, 33, 44, 55}; cout << “search(44,a,4) = " << search(44,a,4) << endl; cout << “search(60,a,4) = " << search(60,a,4) << endl; } Output: search(44,a,7) = 2 search(60,a,7) = 4

35 178110: Computer Programming (II/2546) 35 Binary Search int search(int x, int a[], int n) { // precondition: a[0] <= a[1] <= … <= a[n-1] int lo=0, hi=n-1, i; while (lo <= hi) { i = (lo + hi)/2; if (a[i] == x) return i; if (a[i] < x) ___________; // continue search in a[i+1..hi] else ___________; // continue search in a[lo..i-1] } return n; // x was not found in a[0..n-1] }

36 178110: Computer Programming (II/2546) 36 Example 12 // ฟังก์ชันที่เช็ดว่าในอาร์เรย์มีการจัดเรียงลำดับจาก น้อยไปมากหรือไม่ // If the members in the array are sorted in an increasing order, the function returns true. Otherwise, it returns false bool isNonDecreasing(int a[], int n) { for (int i=1; i <n; i++) if ( _______ < _______) return false; return true; }

37 178110: Computer Programming (II/2546) 37 Assert() Function assert() is the function that can validate whether the input argument of the function is true or false We use to make sure that some condition must be true before continuing executing the program and if that condition is false, then the program should exit

38 178110: Computer Programming (II/2546) 38 The use of assert() bool search(int x, int a[], int n) { // PRECONDITION: a[0] <= a[1] <=... <= a[n-1]; assert(isNondecreasing(a,n)); int lo=0, hi=n-1, i; while (lo <= hi) {…} return n; // x was not found in a[0..n-1] }

39 178110: Computer Programming (II/2546) 39 Using Enumeration with Array ข้อดีของการใช้ค่าคงที่ใน enumeration ให้ เป็นดัชนีในอาร์เรย์ คือ ตัวคำสั่งที่ใช้ใน โปรแกรมสามารถอธิบายความหมายของตัวมัน เองได้ ตัวอย่างเช่น การใช้ดัชนีของอาร์เรย์เป็น enumeration ที่มี symbolic constant เป็นชื่อ วันในสัปดาห์ และใช้ลูปของ for ดังนี้ for (int day = SUN; day <= SAT ; day++)

40 178110: Computer Programming (II/2546) 40 Example 13 #include using namespace std; int main() { enum Day { SUN, MON, TUE, WED, THU, FRI, SAT }; float high[SAT+1] = {88.3, 95.0, 91.2, 89.9, 91.4, 92.5, 86.7}; for (int day = SUN; day <= SAT; day++) cout << "The high temperature for day " << day << " was " << high[day] << endl;}

41 178110: Computer Programming (II/2546) 41 Output of Example 13 The high temperature for day 0 was 88.3 The high temperature for day 1 was 95 The high temperature for day 2 was 91.2 The high temperature for day 3 was 89.9 The high temperature for day 4 was 91.4 The high temperature for day 5 was 92.5 The high temperature for day 6 was 86.7 How to translate 0 to “Sun”, 1 to “Mon”,…?

42 178110: Computer Programming (II/2546) 42 การกำหนดนิยาม type (Type Definitions) Enumeration type เป็นวิธีการหนึ่งในการกำหนด "type" ขึ้นเองของผู้เขียนโปรแกรม เช่น enum Color { RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET }; เป็นการกำหนดนิยามให้กับ "type" Color ซึ่งสามารถประกาศตัวแปรให้มีประเภทเป็น Color พดภำ กปกป Color shirt = BLUE; Color car[ ] = _ { GREEN, RED, BLUE, RED }; float wavelength[VIOLET + 1] = { 420, 480, 530, 570, 600, 620 };

43 178110: Computer Programming (II/2546) 43 Type Definitions C++ ยังอนุญาตให้มีการตั้งชื่อใหม่ให้กับ type ที่มีอยู่ แล้วได้ด้วยโดยใช้คำสงวน typedef เพื่อเป็นการ ประกาศชื่อใหม่ให้กับประเภทที่ระบุโดยมีรูปแบบ typedef type alias; เมื่อ type เป็นประเภทที่ต้องการระบุถึงและ alias เป็นชื่อใหม่ที่ต้องการใช้ เช่นถ้าต้องการใช้ประเภท ข้อมูลในแบบเดียวกับภาษาปาสคาล เราอาจกำหนด ชื่อใหม่ให้ดังนี้ typedef long Integer; typedef double Real;

44 178110: Computer Programming (II/2546) 44 Typedef Examples ใช้ typedef ในการกำหนดชื่อ type ใหม่ typedef long Integer; typedef double Real; จากนี้ก็สามารถประกาศตัวแปรประเภท Integer และ Real แทน long int และ double ได้ Integer n = 22; const Real pi = 3.141592653589793; Integer frequency [64];

45 178110: Computer Programming (II/2546) 45 Type Definitions รูปแบบคำสั่งสำหรับ typedef ของประเภท อาร์เรย์ คือtypedef element-type alias[ ]; จะเห็นว่าจำนวนของสมาชิกใน อาร์เรย์ไม่ได้เป็นส่วนหนึ่งของประเภทของมัน Example typedef float Sequence[] This indicates that there is a new type called ‘sequence’ which is a type of an array which its member has type float

46 178110: Computer Programming (II/2546) 46 Bubble sort and typedef typedef float Sequence[]; void sort(Sequence,int); void print(Sequence,int); void swap(float&, float& ); int main() { Sequence a = {5.5, 2.5, 9.9, 6.6, 4.4, 8.8, 3.3, 7.7}; print(a,8); sort(a,8); print(a,8); }

47 178110: Computer Programming (II/2546) 47 Multidimensional Arrays อาร์เรย์ที่กล่าวมาแล้วเป็นอาร์เรย์แบบมิติเดียว (one-dimensional) หมายความว่ามันเป็นแบบ เรียงลำดับไปแบบเชิงเส้นตรง แต่ความจริงแล้ว ประเภทของสมาชิกของอาร์เรย์เป็นได้หลาย ประเภทรวมทั้งเป็นประเภทอาร์เรย์ด้วย อาร์เรย์ของอาร์เรย์เรียกว่าอาร์เรย์หลายมิติ (multi-dimensional array) อาร์เรย์ของอาร์เรย์หนึ่งมิติเรียกว่าอาร์เรย์สองมิติ อาร์เรย์ของอาร์เรย์สองมิติเรียกว่าอาร์เรย์สามมิติ

48 178110: Computer Programming (II/2546) 48 Multidimensional Arrays รูปแบบอย่างง่ายในการประกาศแอร์เรย์หลาย มิติทำได้โดยใช้รูปแบบ: double a[32] [10] [4]; ซึ่งเป็นแอร์เรย์สามมิติและมีมิติเป็น 32, 10 และ 4 ประโยคคำสั่งa [25] [8] [3] = 99.99 เป็น การกำหนดค่า 99.99 ให้กับสมาชิกของแอร์เรย์ ที่มีดัชนีตำแหน่งที่ (25, 8, 3)

49 178110: Computer Programming (II/2546) 49 Example 14 // การอ่านและพิมพ์ค่าแอร์เรย์ 2 มิติ #include using namespace std; void read(int a[][5]); void print(const int a[][5]); int main() { int a[3][5]; read(a); print(a); }

50 178110: Computer Programming (II/2546) 50 การอ่านและพิมพ์ค่าแอร์เรย์ 2 มิติ void read(int a[][5]) { cout << "Enter 15 integers, 5 per row:\n"; for (int i=0; i<3; i++) { cout << "Row " << i << ": "; for (int j=0; j<5; j++) cin >> a[i][j]; }

51 178110: Computer Programming (II/2546) 51 การอ่านและพิมพ์ค่าแอร์เรย์ 2 มิติ void print(const int a[][5]) { for (int i=0; i<3; i++) { for (int j=0; j<5; j++) cout << " " << a[i][j]; cout << endl; }

52 178110: Computer Programming (II/2546) 52 Output of Example 14 Enter 15 integers, 5 per row: Row 0: 4 7 3 1 4 Row 1: 6 5 3 9 7 Row 2: 8 2 4 4 5 4 7 3 1 4 6 5 3 9 7 8 2 4 4 5

53 178110: Computer Programming (II/2546) 53 Input Parameters as Arrays ในลิสต์ของพารามิเตอร์ของฟังก์ชันไม่ต้องระบุ มิติของแอร์เรย์ที่เป็นมิติแรก ขณะที่ต้องระบุ ค่าของมิติที่สองของแอร์เรย์ ทั้งนี้เนื่องจาก a จะถูกจัดเก็บในรูปของแอร์เรย์มิติเดียวที่มี สมาชิก 5 ตัว จำนวน 3 แอร์เรย์ คอมไพล์เลอร์ไม่จำเป็นจะต้องรับรู้ว่าจะมี จำนวนแอร์เรย์ (ที่มีสมาชิก 5 ตัว) อยู่จำนวนกี่ แอร์เรย์ แต่มันจำเป็นต้องรู้ว่ามีแอร์เรย์ที่มี สมาชิก 5 ตัวเพราะว่ามันจะได้เอาไปคำนวณได้ จำนวนของอาร์เรย์ที่มีสมาขิกอยู่ 5 ตัว

54 178110: Computer Programming (II/2546) 54 Input Parameters as Arrays เมื่อมีการส่งผ่านแอร์เรย์หลายมิติไปยังฟังก์ชัน เราไม่จำเป็นต้องระบุดัชนีของมิติแรก แต่จะต้อง ระบุดัชนีของมิติอื่น ๆ ที่ตามมา เพราะว่าดัชนี ของมิติแรกสามารถคำนวณได้จากพื้นที่ทั้งหมด ของอาร์เรย์และดัชนีของมิติอื่น ๆ

55 178110: Computer Programming (II/2546) 55 Example 15 #include using namespace std; const int NUM_STUDENTS = 3; const int NUM_QUIZZES = 5; typedef int Score[NUM_STUDENTS][NUM_QUIZZES]; void read(Score); void printQuizAverages(Score); void printClassAverages(Score);

56 178110: Computer Programming (II/2546) 56 Example 15 (Cont.) int main() { Score score; cout << "Enter " << NUM_QUIZZES << " scores for each student:\n"; read(score); cout << "The quiz averages are:\n"; printQuizAverages(score); cout << "The class averages are:\n"; printClassAverages(score); }

57 178110: Computer Programming (II/2546) 57 Example 15 (Cont.) void read(Score score) { for (int s=0; s<NUM_STUDENTS; s++) { cout << "Student " << s << ": "; for (int q=0; q<NUM_QUIZZES; q++) cin >> score[s][q]; }

58 178110: Computer Programming (II/2546) 58 Example 15 (Cont.) void printQuizAverages(Score score) { for (int s=0; s<NUM_STUDENTS; s++) { float sum = 0.0; for (int q=0; q<NUM_QUIZZES; q++) sum += score[s][q]; cout << "\tStudent " << s << ": " << sum/NUM_QUIZZES << endl; } }

59 178110: Computer Programming (II/2546) 59 Example 15 (Cont.) void printClassAverages(Score score) { for (int q=0; q<NUM_QUIZZES; q++) { float sum = 0.0; for (int s=0; s<NUM_STUDENTS; s++) sum += score[s][q]; cout << "\tQuiz " << q << ": " << sum/NUM_STUDENTS << endl; }

60 178110: Computer Programming (II/2546) 60 Example 15 Features Define array size with constant variables Define a new type using typedef printQuizAverages() is the function to print the average scores of all quizzes per student printClassAverages() is the function to print the average scores of all students per quiz

61 178110: Computer Programming (II/2546) 61 Example 16 #include using namespace std; int numZeros(int a[][4][3], int n1, int n2, int n3); int main() { int a[2][4][3] = { { {5,0,2}, {0,0,9}, {4,1,0}, {7,7,7} }, { {3,0,0}, {8,5,0}, {0,0,0}, {2,0,9} } }; cout << "This array has " << numZeros(a,2,4,3) << " zeros\n"; }

62 178110: Computer Programming (II/2546) 62 Array a in Example 16 การกำหนดค่าเริ่มแรกให้กับแอร์เรย์ที่ประกอบด้วยแอร์ เรย์ที่มีแอร์เรย์ขนาดเท่ากับ 2 ของแอร์เรย์ขนาด 4 ของแอร์เรย์ขนาด 3 ทำให้จำนวนสมาชิกทั้งหมดที่ได้ เป็น 24 ตัว เราสามารถกำหนดค่าเริ่มแรกของแอร์เรย์ได้อีกวิธีคือ int a[2] [4] [3] = { 5, 0, 2, 0, 0, 9, 4, 1, 0, 7, 7, 7, 3, 0, 0, 8, 5, 0, 0, 0, 0, 2, 0, 9 }; หรือ int a[2] [4] [3] = { { 5, 0, 2, 0, 0, 4, 4, 1, 0, 7, 7, 7, }, { 3, 0, 0, 8, 5, 0, 0, 0, 0, 2, 0, 9 } }; แต่จะดูยุ่งยากกว่าการกำหนดค่าเริ่มแรกให้เป็นลักษณะสาม มิติดังในตัวอย่าง Example 16 ซึ่งใช้ลูปซ้อน 3 ระดับใน การประมวลผลอาร์เรย์ 3 มิติ

63 178110: Computer Programming (II/2546) 63 Example 17 เขียนโปรแกรมของฟังก์ชัน int frequency (float a [ ], int n, int x) นับจำนวนครั้งที่เกิดตัวเลข x จากแอร์เรย์ที่มีสมาชิก อยู่แล้ว n ตัว และให้คืนค่าเป็นความถี่ของ x ใน a #include int frequency(float [], int, int); int main(){ float a[] = {561, 508, 400, 301, 400, 599}; int n = 6, x; cout << "Item: "; cin >> x; cout << "The frequency of item " << x << " is " << frequency(a, n, x) << endl; return 0; }

64 178110: Computer Programming (II/2546) 64 Example 17 (Cont.) int frequency(float a[], int n, int x) { int count = 0; for (int i = 0; _______; ______) if (__________) _________; return count; }


ดาวน์โหลด ppt Chapter 10 Arrays Dept of Computer Engineering Khon Kaen University.

งานนำเสนอที่คล้ายกัน


Ads by Google