1 / 23

ARRAY

ARRAY. Array คืออ ะไ ร ? จำเป็นแค่ไหน ?. 1. Array คือ ตัวแปรที่เก็บข้อมูลแบบเป็นตาราง 2. ความสำคัญของ Array ในบางกรณีเราต้องเกี่ยวข้องกับข้อมูลแบบ ตารางเช่น ตารางบัญชี ตารางคะแนนสอบ ซึ่ง ตัวแปรธรรมดาไม่สามารถจัดการได้. ลักษณะของตาราง. ตารางมิติเดียว (1 Dimension).

quant
Download Presentation

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

  2. Array คืออะไร? จำเป็นแค่ไหน? • 1. Array คือ ตัวแปรที่เก็บข้อมูลแบบเป็นตาราง • 2. ความสำคัญของ Array ในบางกรณีเราต้องเกี่ยวข้องกับข้อมูลแบบ • ตารางเช่น ตารางบัญชี ตารางคะแนนสอบซึ่งตัวแปรธรรมดาไม่สามารถจัดการได้ ลักษณะของตาราง ตารางมิติเดียว (1 Dimension) ตารางสองมิติ (2 Dimension) รายรับของร้านอาหารในคาเฟ่ต์ คนที่ คะแนนสอบ เดือนที่ 1 2 3 4 1 2 3 4 1 2 3 4 ร้านที่

  3. ตารางประกอบด้วย ตารางสองมิติ (2 Dimension) ตารางมิติเดียว (1 Dimension) ชื่อของตาราง คนที่ คะแนนสอบ รายรับของร้านอาหารในคาเฟ่ต์ หมายเลขสดมภ์ เดือนที่ 1 2 3 4 1 2 3 4 1 2 3 4 ร้านที่ ข้อมูลในตาราง หมายเลขช่อง หมายเลขแถว ข้อมูลในตาราง (มีหลายค่า)

  4. Array 1 มิติ เทียบได้กับตารางที่มีแถวเดียวหรือ column เดียว (ตารางมิติเดียว) ข้อมูล 1 ช่องเรียกว่า 1 element ชื่อของ Array Score Index (หมายเลขช่อง) 1 2 3 4 5 Score หมายเลข 3 มีค่า 64 Score หมายเลข 1 มีค่า 100 เวลาเราจะอ้างถึงข้อมูลใน Array 1 มิติ เราจะต้องบอก ชื่อของ Array และหมายเลขช่องเช่น score ช่องที่ 1มีค่า 100

  5. การประกาศตัวแปรArray 1 มิติ ชนิดข้อมูล ชื่อตัวแปร[ขนาดของarray]; เช่น int a[20]; //รับข้อมูลชนิดตัวเลขจำนวนเต็ม ได้ 20 ชุด char c[15];//รับข้อมูลชนิดอักขระได้ 14 ตัว float 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[0] = 13; //กำหนด element แรกมีค่า 13 score[4] = 42; // กำหนด element สุดท้ายมีค่า 42 42 13

  7. ประกาศ array 5 ช่อง แต่กำหนดค่าเพียง 2 ช่อง • #include<stdio.h> • void main() • { • inti; • intscore[5]; • score[0]=13; • score[4]=42; • for(i=0;i<5;i++) • { • printf("Score[%d] = %d\n",i,score[i]); • } • }

  8. { 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]); } inti; for(i=0;i<5;i++) scanf(“%d”,&score[i]); for(i=0;i<5;i++) printf(“%d”,score[i]); เขียนโปรแกรมดังตัวอย่างด้านล่างนี้ การรับและแสดงค่าตัวเลข โดยใช้ Array 1 มิติ { int score[5]; }

  9. โจทย์ Array 1 มิติ เขียนโปรแกรมรับตัวเลขจำนวนเต็ม N จำนวนโดยให้ผู้ใช้ป้อนจำนวนตัวเลขที่ต้องการแล้วแสดงผลรวมและค่าเฉลี่ยของตัวเลขชุดนั้น (นำตัวอย่าง array2.cpp มาแก้ไข แล้วบันทึกไฟล์ชื่อ array3.cpp )

  10. Array 2 มิติ เทียบได้กับตารางที่มีแถวมากกว่า 1 แถวและมีสดมภ์ (column) มากกว่า 1 สดมภ์ MyNumber ชื่อ Array 1 2 3 Row No. MyNumber แถว 2 สดมภ์ 5 มีค่า 1 MyNumber แถว 3 สดมภ์ 4 มีค่า 11 1 2 3 4 5 Column No. เวลาเราจะอ้างถึงข้อมูลใน Array 2 มิติเราจะต้องบอก ชื่อของ Array, หมายเลขแถว และหมายเลขสดมภ์

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

  12. #include<stdio.h>//array2D.cpp void main() { inti,j; int b[4][3]={{7,2,3},//4 แถว 3 คอลัมน์ {5,0,1}, {2,8,0}, {-3,0,0} }; for(i=0;i<4;i++)//ลูปนอก ซึ่งเป็นตำแหน่งแถว { for(j=0;j<3;j++)//ลูปใน ซึ่งเป็นตำแหน่ง คอลัมน์ { printf("%d ",b[i][j]); } printf("\n"); } }

  13. โปรแกรมรับข้อมูลสินค้า เพื่อคำนวณ VAT และราคาสุทธิ โดยจัดเก็บในตัวแปร ARRAY • #include<stdio.h> • #define RATE 0.07 • #define N 5 • void main() • { • char prod[N][15]; • float price[N],vat[N],net[N]; • inti; • for(i=0;i<N;i++) • { • printf("Enter Product Name : "); • scanf("%s",&prod[i]); • printf("\tprice : "); • scanf("%f",&price[i]); • vat[i]=price[i]*RATE; • net[i]=price[i]+vat[i]; • } • printf("No. product price vat7%% Net\n"); //ส่วนของการแสดงผล • for(i=0;i<N;i++) • { • printf("%2d %-10s %10.2f %8.2f %10.2f\n",i+1,prod[i],price[i],vat[i],net[i]); • } • }

  14. โจทย์ Array 2 มิติ • เขียนโปรแกรมโดยการใช้ Array 2 มิติ [3 x 5]เพื่อแสดงค่า ดังต่อไปนี้(array2D2.cpp)

  15. โจทย์ Array 2 มิติ (ต่อ) 2. เขียนโปรแกรมเพื่อคำนวณค่าคอมมิชชั่น0.25% จากยอดขาย ให้รับชื่อพนักงาน และยอดขาย ของพนักงานจำนวน 5 คน และแสดง ชื่อพนักงาน ยอดขาย ค่าคอมมิชชั่นและเงินรายได้รวม(array2D3.cpp)

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

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

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

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

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

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

  22. 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 (+)

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

More Related