1 / 30

Arrays

Arrays. Arrays. array หรือแถวลำดับ คือโครงสร้างข้อมูลที่สามารถเก็บข้อมูล ชนิดเดียวกัน เป็นกลุ่มหรือชุดที่ เรียงต่อกัน เป็นแถว array จะมีโครงสร้างเป็นแบบเชิงเส้น (Linear) ดังนั้น เราสามารถระบุค่าถัดไปหรือก่อนหน้าของแต่ละค่าใน array ได้. 6570. score.

keaira
Download Presentation

Arrays

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. Arrays

  2. Arrays • array หรือแถวลำดับ คือโครงสร้างข้อมูลที่สามารถเก็บข้อมูลชนิดเดียวกัน เป็นกลุ่มหรือชุดที่เรียงต่อกันเป็นแถว • array จะมีโครงสร้างเป็นแบบเชิงเส้น(Linear) ดังนั้น เราสามารถระบุค่าถัดไปหรือก่อนหน้าของแต่ละค่าใน array ได้

  3. 6570 score score[0] score[1] score[2] score[3] score[4]

  4. การระบุตำแหน่งหรือค่าใน array จะใช้ตัวเลข index • score[0] คือ คะแนนสอบของนักเรียนคนที่ 1 • score[23] คือ คะแนนสอบของนักเรียนคนที่ 24 จะเห็นว่า ตัวแปร array ง่ายต่อการอ้างอิงเพื่อใช้งาน ซึ่งถ้าไม่ใช้ array จะต้องประกาศตัวแปรถึง 24 ตัว เช่น score1, score2, …, score24

  5. Array 1 มิติ • การประกาศตัวแปร ชนิดข้อมูล ชื่อตัวแปร[ขนาดของarray]; เช่น int a[20]; char c[15]; float score[24];

  6. random values เพราะยังไม่มีการกำหนดค่าให้ score ใน memory -1068 22541 18253 -5673 6570 score[0] score[1] score[2] score[3] score[4] int score[5]; score[0] = 13; //set first element score[4] = 42; //set last element 42 13

  7. { int score[5]; scanf(“%d”,&score[0]); scanf(“%d”,&score[1]); scanf(“%d”,&score[2]); scanf(“%d”,&score[3]); scanf(“%d”,&score[4]); printf(“%d ”,score[0]); printf(“%d ”,score[1]); printf(“%d ”,score[2]); printf(“%d ”,score[3]); printf(“%d ”,score[4]); } int i; for(i=0;i<5;i++) scanf(“%d”,&score[i]); for(i=0;i<5;i++) printf(“%d”,score[i]); { int score[5]; }

  8. array - initialize int a[5] = { 1, 2, 3, 4, 5}; int a[] = {1, 2, 3, 4, 5}; int a[7] = {1, 2, 3, 4, 5}; int *a = {1, 2, 3, 4, 5} printf(“%d”, a[3]);

  9. array - initialize int a[5] = { 1, 2, 3}; printf(“%d”, a[3]); จะได้ผลลัพธ์อย่างไร

  10. โจทย์ 1 • รับเลขจำนวนเต็ม เก็บใส่ในตัวแปรชุด A และ B ซึ่งมีขนาดเท่ากับ 5 แล้วหาผลบวกของข้อมูลในตำแหน่งที่ตรงกันของตัวแปร A และ B แล้วแสดงผล A : 1 3 4 6 4 B : 1 2 3 4 5 A+B : 2 5 7 10 9

  11. โจทย์ 2 • รับเลขจำนวนเต็ม 5 ตัวแล้วหาผลรวมของตัวเลขที่มีค่ามากกว่าค่าเฉลี่ยของตัวเลขทั้ง 5 ตัวนี้ เช่น input : 10 3 2 6 4 average : 5.00 output : 16

  12. โจทย์ 3 • รับเลขจำนวนเต็ม 5 ตัว เก็บไว้ในเซต A และอีก 5 ตัวเก็บไว้ในเซต B แล้วหา • A union B • A intersect B • A – B • B - A

  13. C Strings • ในภาษาซีใช้ array of character ในการเก็บสายอักขระ(string) • เพิ่ม null character ‘\0’ ต่อท้ายอักขระตัวท้าย เป็นการบอกจุดสิ้นสุดสตริง

  14. เช่นchar code[6];

  15. เช่นchar name[6]=“FOKKY”;

  16. เช่นchar str[6]=“JAN”;

  17. คำสั่งรับค่า - แสดงผลสตริง

  18. C Strings • ในภาษาซีมี standard library function เกี่ยวกับสตริงให้ใช้งาน • ที่ใช้งานบ่อยๆ ได้แก่ strlen(ชื่อตัวแปรสตริง) strcpy(ชื่อตัวแปรสตริงปลายทาง, ชื่อตัวแปรสตริงต้นทาง)

  19. ตัวอย่าง char s[10]; strcpy(s, “MWIT”); s memory 0 1 2 3 4 5 6 7 8 9

  20. Note!! • assignment operator หรือ เครื่องหมายเท่ากับ (=) ไม่สามารถใช้กำหนดค่าให้กับตัวแปรสตริงได้ ต้องใช้ฟังก์ชัน strcpy() เท่านั้น strcpy(s, “MWIT”);  S = “MWIT” ;

  21. Question • จะเกิดอะไรขึ้น ถ้าเราเก็บข้อความ “Mahidol Wittayanusorn” ลงในตัวแปร char s[10]; • ข้อความจะถูกเก็บไว้ในตัวแปร s และจะบันทึกตัวอักษรที่เกินไปด้วย ซึ่งอาจไปบันทึกทับข้อมูลที่ถูกเก็บไว้ถัดจากตัวแปร s

  22. ตัวอย่าง ผลลัพธ์ char s[10]; int len; strcpy(s, “MWIT”); len = strlen(s); printf(“%d”, len); 4

  23. string.h • strcpy(dest_string, source_string); • int a = strlen(string); • strcat(dest_string, source_string); • int a = strcmp(string1, string2); • string1 == string2 if a == 0 • string1 < string2 if a is negative (-) • string1 > string2 if a is positive (+)

  24. โจทย์ 4 • ตรวจสอบ string ที่ผู้ใช้ป้อนเข้ามาว่าเป็น palindrome หรือไม่ เช่น level  success deed maimai

  25. Multidimensional Arrays • ตัวอย่างการประกาศ array 2 มิติ ขนาด 10 x 10 โดยเก็บเลขจำนวนเต็ม กำหนดค่าแรกและค่าสุดท้ายเป็น 13 int board[10][10]; board[0][0] = 13; board[9][9] = 13;

  26. Memory Row 0 Row 1 Row 2 Row 3 ถ้ากำหนด int b[4][3]; 0 1 2 0 1 2 3 Rows Columns

  27. Memory Row 0 Row 1 Row 2 Row 3 ถ้ากำหนด int b[2][4][3]; Columns 0 1 2 Page 0 0 1 2 3 Rows Page 1 Page 0

  28. array - initialize int a[2][3] = { 1, 2, 3, 4, 5, 6}; int a[2][3] = { { 1, 2, 3 }, {4, 5, 6 } }; int a[2][3] = { { 1, 2 }, {3, 4, 5 } }; int a[5][5] = { { 1, 2, 3 }, {4, 5, 6 } };

  29. array - initialize int a[][] = { { 1, 2, 3 }, {4, 5, 6 } }; int a[][3] = { { 1, 2, 3 }, {4, 5, 6 } }; int a[2][] = { { 1, 2, 3 }, {4, 5, 6 } };

  30. โจทย์ 5 • เขียนโปรแกรมคำนวณหาผลบวก ลบ และผลคูณเมตริกซ์ขนาด 3 x 3 • A + B • A – B • A x B

More Related