1 / 19

Arrays

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

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

  4. วิธีการประกาศตัวแปรชุดวิธีการประกาศตัวแปรชุด int score[5]; ตัวแปรชุด score • จากตัวอย่าง เป็นการประกาศตัวแปรชุดชนิด integer ชื่อ score ซึ่งมีขนาดเท่ากับ 5 • หมายถึง ตัวแปรชุด score สามารถเก็บข้อมูลจำนวนเต็มได้ 5 ค่า นั่นเอง score score[0] score[1] score[2] score[3] score[4]

  5. การระบุตำแหน่งหรือค่าใน array จะใช้ตัวเลข index • score[0] คือ คะแนนสอบของนักเรียนคนที่ 1 จะเห็นว่า ตัวแปร array ง่ายต่อการอ้างอิงเพื่อใช้งาน ตัวเลขindex ซึ่งจะเริ่มต้นที่ 0 เสมอ ซึ่งถ้าไม่ใช้ array จะต้องประกาศตัวแปรถึง 5 ตัว หรือถ้าต้องการเก็บคะแนนของนักเรียน 24 คน ก็เพียงประกาศ int score[24]; ไม่จำเป็นต้องประกาศตัวแปรถึง 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 ช่องแรก score[0] = 13; score[4] = 42; กำหนดค่าให้กับ score ช่องสุดท้าย 42 13

  7. ตัวอย่างการอ้างถึงสมาชิกในตัวแปรชุดตัวอย่างการอ้างถึงสมาชิกในตัวแปรชุด #include<stdio.h> int main() { int a[5]={5}; inti = 2; printf(“a[0] = %d\n”, a[0]); printf(“a[1] = %d\n”, a[1]); a[2] = a[0]+3; a[1] = a[i]+10; a[i+1] = 20; a[4] = a[i-1]; • printf(“a[1] = %d\n”, a[1]); • printf(“a[2] = %d\n”, a[2]); • printf(“a[3] = %d\n”, a[3]); • printf(“a[4] = %d\n”, a[4]); return 0; } เพราะเหตุใด a[1] ในตอนแรกจึงเท่ากับ 0 ?? เราสามารถอ้างอิงถึงสมาชิกใน array โดยใช้ตัวแปรได้ เช่น a[i] เราสามารถอ้างอิงถึงสมาชิกใน array โดยอยู่ในรูปของ expression ได้ เช่น a[i-1], a[i+1], a[i*2]

  8. #include<stdio.h> int main() { 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]); return 0; } ทดลองเขียนโปรแกรมรับและแสดงผลเลขจำนวนเต็ม 5 จำนวน รับข้อมูลเลขจำนวนเต็ม 5 จำนวน เก็บใส่ในตำแหน่งของตัวแปรชุด score แสดงข้อมูลเลขจำนวนเต็ม ที่เก็บในตัวแปรชุด score

  9. #include<stdio.h> int main() { 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]); return 0; } สังเกตโค้ดที่เขียนซ้ำๆกัน จะเห็นว่าเป็นการสั่งให้รับเลขจำนวนเต็มซ้ำๆกัน 5 ครั้ง ดังนั้น ใช้ for ช่วยวนลูปได้ จะเห็นว่าเป็นการสั่งให้แสดงผลเลขจำนวนเต็มซ้ำๆกัน 5 ครั้ง ดังนั้น ใช้ for ช่วยวนลูปได้

  10. #include<stdio.h> int main() { 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]); return 0; } เปลี่ยนไปใช้คำสั่ง for เพื่อวนลูป รับและแสดงค่า #include<stdio.h> int main() { int score[5]; return 0; } inti; for(i=0;i<5;i++) scanf(“%d”,&score[i]); for(i=0;i<5;i++) printf(“%d”,score[i]);

  11. โปรแกรมรับและแสดงผลเลขจำนวนเต็ม 5 จำนวน ที่ใช้คำสั่ง for ในการวนซ้ำเพื่อรับ-แสดงค่า #include<stdio.h> int main() { int score[5]; return 0; } inti; for(i=0;i<5;i++) scanf(“%d”,&score[i]); รับข้อมูลเลขจำนวนเต็ม 5 จำนวน เก็บใส่ในตำแหน่งของตัวแปรชุด score for(i=0;i<5;i++) printf(“%d”,score[i]); แสดงข้อมูลเลขจำนวนเต็ม ที่เก็บในตัวแปรชุด score

  12. การประกาศตัวแปรและกำหนดค่าเริ่มต้นในคราวเดียวการประกาศตัวแปรและกำหนดค่าเริ่มต้นในคราวเดียว 1 2 3 4 5 a a[0] a[1] a[2] a[3] a[4] int a[5] = { 1, 2, 3, 4, 5};

  13. การประกาศตัวแปรและกำหนดค่าเริ่มต้นในคราวเดียวการประกาศตัวแปรและกำหนดค่าเริ่มต้นในคราวเดียว เหมือนกัน int a[5] = { 1, 2, 3, 4, 5}; int a[] = {1, 2, 3, 4, 5}; int *a = {1, 2, 3, 4, 5}

  14. การประกาศตัวแปรและกำหนดค่าเริ่มต้นในคราวเดียวการประกาศตัวแปรและกำหนดค่าเริ่มต้นในคราวเดียว 1 2 3 4 5 0 0 a a[0] a[1] a[2] a[3] a[4] a[5] a[6] สังเกตว่า a[5] และ a[6] จะเป็น 0 เพราะกำหนดค่าเริ่มต้นเพียง 5 ค่าเท่านั้น int a[7] = { 1, 2, 3, 4, 5};

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

  16. โจทย์ 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

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

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

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

More Related