1 / 32

Array

Array. Array : ตัวแปรอาร์เรย์. ตัวแปรอาร์เรย์แบบต่างๆ การประกาศตัวแปรแบบอาร์เรย์ 1 มิติและ 2 มิติ การประกาศและกำาหนดข้อมูลให้กับตัวแปรแบบอาร์เรย์ การเข้าถึงข้อมูลในตัวแปรอาร์เรย์ ตัวแปรอาร์เรย์ก์กับข้อความ ตัวอย่างการใช้งานตัวแปรอาร์เรย์. ตัวแปรอาร์เรย์แบบต่างๆ.

etoile
Télécharger la présentation

Array

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Array 30-010-104 Computer Programming

  2. Array : ตัวแปรอาร์เรย์ • ตัวแปรอาร์เรย์แบบต่างๆ • การประกาศตัวแปรแบบอาร์เรย์ 1 มิติและ 2 มิติ • การประกาศและกำาหนดข้อมูลให้กับตัวแปรแบบอาร์เรย์ • การเข้าถึงข้อมูลในตัวแปรอาร์เรย์ • ตัวแปรอาร์เรย์ก์กับข้อความ • ตัวอย่างการใช้งานตัวแปรอาร์เรย์ 30-010-104 Computer Programming

  3. ตัวแปรอาร์เรย์แบบต่างๆตัวแปรอาร์เรย์แบบต่างๆ • อาร์เรย์ 1 มิติ เก็บข้อมูลเพียงชั้นเดียว หรือแถวเดียว Array Variable ชื่อ ar เก็บข้อมูลชนิดตัวเลขทศนิยม 8 จำนวน • อาร์เรย์ n มิติเก็บข้อ้อมูลได้ห้หลายชั้น 30-010-104 Computer Programming

  4. address การประกาศตัวแปรอาร์เรย์แบบ 1 มิติ type array_name [ n ] ; • type คือชนิดของตัวแปรที่จะสร้างขึ้น • array_name คือชื่อของตัวแปรอาร์เรย์ • n ขนาดของตัวแปรที่จะสร้างขึ้น int number [3]; ตัวอย่างตัวแปรอาร์เรย์แบบ integer โปรแกรมจะจองหน่วยความจำขนาด 2 × 3 Bytes ดังแสดงในรูป 30-010-104 Computer Programming

  5. การประกาศตัวแปรอาร์เรย์แบบ 2 มิติ • ตัวอย่างอาร์เรย์ชนิด float ขนาด 3 แถว 2 คอลัมน์ 6 × 4 bytes type array_name[n][m]; n คือจำานวนแถวของตัวแปรอาร์เรย์ m คือจำานวนคอลัมน์ของตัวแปรอาร์เรย์ float real [3][2] ; 30-010-104 Computer Programming

  6. การประกาศและกำาหนดข้อมูลให้กับตัวแปรแบบอาร์เรย์การประกาศและกำาหนดข้อมูลให้กับตัวแปรแบบอาร์เรย์ อาร์เรย์ 1 มิติ type a_name [3] = { value_3, value_2, ..., value_n }; int number[ ] = {23, -186, 431}; char vowel [5] = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}; อาร์เรย์ 2 มิติ type a_name[m][n] = {{value_11, value_12, ..., value_1n}, {value_21, value_22, ..., value_2n}, ... {value_m1, value_m2, ..., value_mn}}; float matrix[4][4] = { {1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {5.0, -3.0, 2.0, 1.0} }; 30-010-104 Computer Programming

  7. การเข้าถึงข้อมูลในตัวแปรอาร์รย์การเข้าถึงข้อมูลในตัวแปรอาร์รย์ • ข้อมูลแต่ล่ละตัวในอาร์เรย์สามารถเข้าถึงได้โดยใช้ index หรือตัวเลขตามหลังชื่อตัวแปร index มีค่าจาก 0 ถึงขนาดของอาร์เรย์ลบ 1 • #include <stdio.h> • int year[7] = {2001, 2002, 2003, 2004 , 2005 , 2006 , 2007}; • float matrix[4][4] = {{1.0, 0.0, 1.0, 0.0}, • {0.0, 1.0, 0.0, 0.0}, • {0.0, 0.0, 1.0, 0.0}, • {5.0, -3.0, 2.0, 1.0}}; • void main() • { • year[5] += 543; • matrix[3][1] += 7.0; • printf(“This year is : %d\n” , year[5]): • printf(“%.2f”, matrix[3][1]+matrix[0][2]); • } This year is : 2549 5.00 30-010-104 Computer Programming

  8. ตัวแปรอาร์เรย์กับข้อความตัวแปรอาร์เรย์กับข้อความ • การเก็บข้อความจะใช้ต้ตัวแปรอาร์เรย์หนึ่งมิติชนิด char ขนาดเท่ากับจำานวนตัวอักษรบวกหนึ่ง • #include <stdio.h> • char sentence[21] = “Computer Programming”; • char word[9] = {‘T’,‘h’,‘a’,‘i’,‘l’,‘a’,‘n’,‘d’,‘\0’}; • char nword[5] = {‘R’,‘m’,‘u’,‘t’,’l’}; • void main() • { • printf(“%s\n”, sentence); • printf(“%s\n”, word); • printf(“%s\n”, nword); • } Computer Programming Thailand Rmutl 30-010-104 Computer Programming

  9. ตัวอย่างการใช้งานตัวแปรอาร์เรย์ตัวอย่างการใช้งานตัวแปรอาร์เรย์ • #include <stdio.h> • #include <conio.h> • float num[10], total = 0; • int i; • void main() • { • //Find the average value of 10 floating-point numbers • for(i=0; i<10; i++){ • printf(“Enter number : ”); • scanf(“%f”,&num[i]); • total += num[i]; • } • printf(“Total = %f\n”, total); • printf(“Average = %f\n”, total/10); • getch(); • } Enter number : 2 Enter number : 5 Enter number : 6 Enter number : 4 Enter number : 8 Enter number : 9 Enter number : 10 Enter number : 11 Enter number : 19 Enter number : 5 Total = 79.00 Average = 7.90 30-010-104 Computer Programming

  10. ตัวอย่างการใช้งานตัวแปรอาร์เรย์ (2) • #include <stdio.h> • #include <conio.h> • char message[21]; • int x = 0; • void main() • { • //Measure the length of message • printf(“Enter your message : ”); • gets(message); • while(message[x] != ‘\0’) • { • x++; • } • printf(“Message Length = %d\n”, x); • getch(); • } Enter your message : Thierry_HENRY Message Length = 13 30-010-104 Computer Programming

  11. ตัวอย่างการใช้งานตัวแปรอาร์เรย์ (3) • #include <conio.h> • int matrix[3][3],i,j; • void main() • { • for(i=0;i<3;i++) • for(j=0;j<3;j++) • { • printf(“Enter number : ”); • scanf(“%d”, &matrix[i][j]); • } • /***** DISPLAY SHOW ******/ • printf(“\n***Matrix***\n”); • for(i=0;i<3;i++) • { • for(j=0;j<3;j++) • printf(“%d ”,matrix[i][j]); • printf(“\n”); • } • getch(); • } Enter number: 6 Enter number: 8 Enter number: 2 Enter number: 1 Enter number: 4 Enter number: 9 Enter number: 3 Enter number: 7 Enter number: 0 ***Matrix*** 682 149 370 30-010-104 Computer Programming

  12. Pointer(ตัวชี้) 30-010-104 Computer Programming

  13. Pointer รู้จักกับตัวแปรพอยน์เตอร์ การประกาศตัวแปรพอยน์เตอร์ แสดงตำาแหน่งข้อมูลด้วย & (Address Operator) แสดงค่าข้อ้อมูลด้วย * (Indirect Operator) ตัวแปรพอยน์เตอร์กับอาร์เรย์ ตัวแปรพอยน์เตอร์กับข้อความ อาร์เรย์ของพอยน์เตอร์ Indirect Pointer 30-010-104 Computer Programming

  14. Pointer (cont.) เมื่อมีการสร้างตัวแปรชนิดใดขึ้นมา โปรแกรมจะทำการจองพื้นที่หน่วยความจำ (Memory Address) ตำแหน่งที่ว่างไว้เก็บข้อมูล ตัวแปรพอยน์เตอร์จะใช้เก็บตำาแหน่งหน่วยความจำของตัวแปรที่สร้างขึ้นถ้าสร้างตัวแปร int ชื่อ hundred และกำาหนดค่าเริ่มต้นเป็น 100 โปรแกรมจะจองพื้นที่หน่วยความจำให้ 2 bytes ดังรูป 00000000 01100100 03E1 03E0 หากสร้างตัวแปรพอยน์เตอร์เพื่อเก็บตำแหน่งหน่วยความจำา ตัวแปรพอยน์เตอร์น์นั้นจะเก็บค่า 03E0 ซึ่งเป็น็นตำาแหน่งแรกในหน่วยความจำ 30-010-104 Computer Programming

  15. การประกาศตัวแปรพอยน์เตอร์การประกาศตัวแปรพอยน์เตอร์ การสร้างตัวแปรพอยน์เตอร์จะต้องประกาศชนิดของตัวแปรด้วย โดยชนิดของตัวแปรพอยน์เตอร์จะเป็นชนิดเดียวกับตัวแปรที่เราจะเก็บตำแหน่งของหน่วยความจำ type คือชนิดของตัวแปรพอยน์เตอร์ * เครื่องหมายแสดงว่าตัวแปรที่สร้างขึ้นเป็นตัวแปรชนิดพอยน์เตอร์ variable คือชื่อของตัวแปรพอยน์เตอร์ type *variable; 30-010-104 Computer Programming

  16. ตัวอย่างการสร้างตัวแปรพอยน์เตอร์ตัวอย่างการสร้างตัวแปรพอยน์เตอร์ สร้างตัวแปร int ชื่อ num int num; int *pt_int; สร้างตัวแปร pointer ชนิด int เพื่อเก็บตำแหน่งหน่วยความจำของ ตัวแปร num สร้างตัวแปร char ชื่อ letter char letter; char *pt_char; สร้างตัวแปร pointer ชนิด char เพื่อเก็บตำแหน่งหน่วยความจำของ ตัวแปร letter 30-010-104 Computer Programming

  17. แสดงตำแหน่งข้อมูลด้วย & (Address Operator) ใช้นำค่าตำแหน่งหน่วยความจำของตัวแปรใดๆ มาเก็บไว้ในตัวแปรพอยน์เตอร์ โดยมีรูปแบบดังนี้ pointer ชื่อของตัวแปรพอยน์เตอร์เก็บตำาแหน่งของหน่วยความจำ &นำหน้าชื่อตัวแปร เพื่อหาตำแหน่งในหน่วยความจำของตัวแปรนั้น variable ตัวแปรที่ต้องการหาตำาแหน่งในหน่วยความจำ Format Code ที่ใช้แสดงค่าของตัวแปรพอยน์เตอร์คือ %p pointer = &variable; 30-010-104 Computer Programming

  18. ตัวอย่างการแสดงตำแหน่งข้อมูลด้วย & 30-010-104 Computer Programming

  19. ตัวอย่างการเขียนโปรแกรมเพื่อแสดงตำแหน่งข้อมูลด้วย & #include <stdio.h> #include <conio.h> int x = 17, *pt_int; float salary = 12000.00, *pt_float; char letter = ‘w’, *pt_char; void main() { pt_int = &x; pt_float = &salary; pt_char = &letter; printf(“Address of variable x = %p\n”, pt_int); printf(“Address of variable salary = %p\n”, pt_float); printf(“Address of variable letter = %p\n”, pt_char); getch(); } Address of variable x = 0040B158 Address of variable salary = 0040B15C Address of variable letter = 0040B160 30-010-104 Computer Programming

  20. แสดงค่าข้อมูลด้วย * (Indirect Operator) ใช้เพื่อหาข้อมูลจากตำแหน่งของหน่วยความจำที่เก็บไว้ตัวแปรพอยน์เตอร์ โดยเขียนเครื่องหมาย * นำหน้าชื่อตัวแปรพอยน์เตอร์ variable ตัวแปรที่เก็บข้อมูลจากตำแหน่งในหน่วยความจำที่ชี้ โดยตัวแปรพอยน์เตอร์ *เครื่องหมายเพื่อหาค่าข้อมูลจากตำแหน่งในหน่วยความจำ pointer ตัวแปรพอยน์เตอร์ที่ชี้ตำแหน่งในหน่วยความจำ variable = *pointer; 30-010-104 Computer Programming

  21. ตัวอย่างการแสดงค่าข้อมูลด้วย * 30-010-104 Computer Programming

  22. ตัวอย่างการเขียนโปรแกรมเพื่อแสดงค่าข้อมูลด้วย * #include <stdio.h> #include <conio.h> int num1 = 113, num2, *pt_num; char c1 = ‘a’, *pt_c; void main() { pt_num = &num1; pt_c = &c1; num2 = *pt_num; printf(“Variable num2 = %d\n”, num2); printf(“Varaible c1 = %c\n”, *pt_c); *pt_num = *pt_num+1; printf(“Variable num1 = %d\n”, num1); getch(); } Variable num2 = 113 Variable c1 = a Variable num1 = 114 30-010-104 Computer Programming

  23. ตัวแปรพอยน์เตอร์กับอาร์เรย์ที่ไม่ระบุ index ชื่อตัวแปรอาร์เรย์ที่ไม่ระบุ index จะหมายถึงค่าตำแหน่งในหน่วยความจำาของตัวแปรอาร์เรย์ตัวแรก ดังนั้นสำหรับตัวแปรอาร์เรย์เราสามารถกำหนดตำแหน่งหน่วยความจำให้กับพอยน์เตอร์โดยไม่ต้องใช้ & 30-010-104 Computer Programming

  24. ตัวอย่างตัวแปรอาร์เรย์ที่ไม่ระบุ index #include <stdio.h> #include <conio.h> float record[5] = {32.46, 12.67, 43.908, 76.09, 12.401}; float *pt_record, new_record; void main() { pt_record = record; //Same as pt_record = &record[0] printf(“Address of array of record = %p\n”, pt_record); new_record = *(pt_record+3); printf(“record[3] = %.02f\n”, new_record); *(pt_record+3) = *(pt_record+3) + 5; printf(“record[3] = %.02f\n”, *(pt_record+3)); getch(); } Address of array of record = 0040B158 record[3] = 76.09 record[3] = 81.09 30-010-104 Computer Programming

  25. ตัวแปรพอยน์เตอร์กับข้อความตัวแปรพอยน์เตอร์กับข้อความ การเขียนชื่อตัวแปรอาร์เรย์ที่เก็บข้อความโดยไม่ระบุ index จะหมายถึงค่าตำแหน่งหน่วยความจำเริ่มต้นของข้อความนั้น หรือจริงๆแล้ว ชื่ออาร์เรย์ที่เก็บข้อความก็เป็นพอยน์เตอร์นั้นเอง #include <stdio.h> #include <conio.h> char name[6] = "Bill"; char *pt_name; void main() { pt_name = name; printf("Address of name = %p\n", pt_name); printf("name = %s\n", pt_name); getch(); } Address of name = 0040B158 name = Bill; 30-010-104 Computer Programming

  26. อาร์เรย์ของพอยน์เตอร์อาร์เรย์ของพอยน์เตอร์ กรณีที่ต้องใช้พอยน์เตอร์ชนิดเดียวกันจำนวนมาก สามารถสร้างตัวแปรอาร์เรย์ของพอยน์เตอร์ได้เช่นเดียวกับตัวแปรอาร์เรย์ของข้อมูลชนิดอื่น type คือชนิดของตัวแปรพอยน์เตอร์ * เครื่องหมายแสดงว่าตัวแปรที่สร้างขึ้นเป็นตัวแปรชนิดพอยน์เตอร์ variable คือชื่อของตัวแปรพอยน์เตอร์ n ขนาดของอาร์เรย์พอยน์เตอร์ที่จะสร้างขึ้น type *variable[n]; 30-010-104 Computer Programming

  27. ตัวอย่างการใชง้ง้านอาร์เรย์ของพอยน์เตอร์ตัวอย่างการใชง้ง้านอาร์เรย์ของพอยน์เตอร์ #include <stdio.h> #include <conio.h> int i, room[5] = {409, 314, 412, 325, 509}; int *pt_r[5]; void main() { for(i=0; i<5; i++) { pt_r[i] = &room[i]; printf(“Address of room[%d] = %p\n”, i, pt_r[i]); } getch(); } Address of room[0] = 0040B158 Address of room[1] = 0040B15C Address of room[2] = 0040B160 Address of room[3] = 0040B164 Address of room[4] = 0040B168 30-010-104 Computer Programming

  28. Indirect Pointer ตัวแปรพอยน์เตอร์ถือเป็นตัวแปรชนิดหนึ่ง เมื่อถูกสร้างขึ้นมาโปรแกรมจะจองพื้นที่ในหน่วยความจำเช่นเดียวกัน หากต้องการทราบตำแหน่งหน่วยความจำของตัวแปรพอยน์เตอร์ จะต้องใช้ตัวแปร Indirect Pointer type คือชนิดของตัวแปร indirect pointer ซึ่งเป็นชนิดเดียวกับตัวแปรพอยน์เตอร์ที่จะหาตำแหน่งหน่วยความจำ ** เครื่องหมายของตัวแปรชนิด indirect pointer pointer คือชื่อของตัวแปร indirect pointer type **pointer; 30-010-104 Computer Programming

  29. ตัวอย่างการใช้ Indirect Pointer 30-010-104 Computer Programming

  30. ตัวอย่างการใช้ Indirect Pointer (ต่อ) 30-010-104 Computer Programming

  31. ตัวอย่างโปรแกรมที่ใช้ Indirect Pointer #include <stdio.h> #include <conio.h> int i; float time[3] = {9.28, 18.03, 23.59}; float *pt_time[3], **ipt_time[3]; void main() { for(i=0; i<3; i++) { pt_time[i] = &time[i]; ipt_time[i] = &pt_time[i]; printf(“Address of pointer pt_time[%d] = %p\n”, i,ipt_time[i]); } printf(“time[2] = %.02f\n”, **ipt_time[2]); printf(“time[1] = %.02f\n”, *pt_time[1]); getch(); } 30-010-104 Computer Programming

  32. แบบฝึกหัด 30-010-104 Computer Programming

More Related