1 / 33

ตัวแปรชุด

ตัวแปรชุด. ประเภทข้อมูลแบบข้อมูลเดี่ยว. ประเภทข้อมูลที่กล่าวมาเป็นแบบข้อมูลแบบเดี่ยว เช่น int, char, float, double ตัวแปรหนึ่งตัวสามารถเก็บข้อมูลได้หนึ่งค่า ถ้าต้องการเก็บข้อมูลหลายค่าจะต้องประกาศตัวแปรหลายตัว. 2.

zuriel
Télécharger la présentation

ตัวแปรชุด

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. ตัวแปรชุด

  2. ประเภทข้อมูลแบบข้อมูลเดี่ยวประเภทข้อมูลแบบข้อมูลเดี่ยว • ประเภทข้อมูลที่กล่าวมาเป็นแบบข้อมูลแบบเดี่ยว • เช่น int, char, float, double • ตัวแปรหนึ่งตัวสามารถเก็บข้อมูลได้หนึ่งค่า • ถ้าต้องการเก็บข้อมูลหลายค่าจะต้องประกาศตัวแปรหลายตัว 2

  3. ตัวอย่าง เก็บข้อมูลอุณหภูมิของวันต่าง ๆ ใน 1 สัปดาห์แล้วหาวันที่มีอุณหภูมิที่สูงที่สุด //ประกาศ float day1, day2, day3, day4, day5, day6, day7; //รับค่า printf(“Enter the temp. of Monday”); scanf(“%f”,&day1); printf(“Enter the temp. of Tuesday”); scanf(“%f”,&day2); ….. printf(“Enter the temp. of Sunday”); scanf(“%f”,&day7); 3

  4. //ทดสอบหาวันที่มีอุณหภูมิที่สูงที่สุด//ทดสอบหาวันที่มีอุณหภูมิที่สูงที่สุด if ((day1 > day2) && (day1 > day3) && ………….. printf(“Monday is the Highest temp. day of this week”); else if ((day2 > day1) && (day3 > day2) && ………….. printf(“Tuesday is the Highest temp. day of this week”); ……… 4

  5. ปัญหา • ใช้ตัวแปรจำนวนมาก • การประกาศและการทดสอบค่อนข้างยาก 5

  6. 30.5 day1 float day[7]; float day1, day2, day3, day4, day5, day6, day7; day2 32.9 30.5 day day3 31.5 32.9 day4 29.7 31.5 29.7 day5 30.8 30.8 day6 28.5 28.5 30.1 day7 30.1 6

  7. 30.5 day[0] 32.9 day[1] float day[7]; 31.5 day[2] ทั้งหมดเรียก รวมว่า Array day 29.7 day[3] 30.8 day[4] 28.5 day[5] 30.1 day[6] 7

  8. Array หรือตัวแปรชุด • คือ ตัวแปรที่มีการเก็บข้อมูลในหน่วยความจำที่เก็บข้อมูลหลายค่า และต้องเป็นประเภทข้อมูลแบบเดียวกัน แต่ใช้เพียงชื่อเดียว • ข้อมูลแต่ละตัวในกลุ่มนั้น จะเรียกว่า element หรือ cell • ในการอ้างถึงข้อมูลจะใช้indexเป็นตัวบอกว่าเป็นตัวแปรที่เก็บค่าเท่าไหร่ในตัวแปรชุดนั้น • ค่าของ index เริ่มต้นที่ ___ 8

  9. 30.5 day[0] float day[7]; 32.9 day[1] ข้อมูลอยู่ใน array ชื่อ __day____ แต่ละ element เก็บข้อมูลอุณหภูมิที่มีประเภทข้อมูลแบบ __float_____ และมี __7___ element 31.5 day[2] 29.7 day[3] 30.8 day[4] 28.5 day[5] 30.1 day[6] 9

  10. 30.5 printf(“%d”,day[0]);  __30.5___ day[0] 32.9 printf(“%d”,day[5]);  __28.5___ day[1] printf(“%d”,day[7]);  __error__ 31.5 day[2] printf(“%d”,day[-1]);  _ error _ 29.7 day[3] printf(“%d”,day[0]+day[6]);  _60.6____ 30.8 day[4] printf(“%d”,day[0+6]);  __30.1___ 28.5 day[5] printf(“%d”,day[3]+1);  ___30.7__ 30.1 day[6]

  11. การประกาศ ARRAY รูปแบบ datatype varname[size]; ตัวอย่าง int score[10]; …… score[0] score[1] score[_] 11

  12. ตัวอย่าง char name[30]; …… name[0] name[1] name[_] ตัวอย่าง int pw[ ] = {1,5,7,199}; char name[ ] = “Nintendo”; 12

  13. การกำหนดค่าเริ่มต้นให้กับแถวลำดับการกำหนดค่าเริ่มต้นให้กับแถวลำดับ รูปแบบ ชนิดข้อมูล ชื่อตัวแปรแถวลำดับ = {ค่าเริ่มต้น}; int num[10] = {0,1,2,3,4,5,6,7,8,9}; char fname[10] = {‘s’,’w’,’e’,’e’,’t’,’\0’}; char last[10] = “Honey”; int guess[] = {1,2,5,7,9}; int emp[3][7] = {{10,20,30,40,50,60,70}, {11,21,31,41,51,61,71}, {12,22,32,42,52,62,72}}; 13

  14. การกำหนดค่าเริ่มต้นให้กับแถวลำดับการกำหนดค่าเริ่มต้นให้กับแถวลำดับ char paint[2][5] = {{‘r’,’e’,’d’,’\0’}, {‘p’,’i’,’n’,’k’,’\0’}}; char paint[2][5] = {“red”,“pink”}; int t[2[[3][4] = { { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} } { {13,14,15,16}, {17,18,19,20}, {21,22,23,24} } }; 14

  15. #include <stdio.h> void main() { int data[10]; //input for (int i =0;i<10 ;i++ ) { printf("enter data[%d] : ",i); scanf("%d",&data[i]); } เขียนโปรแกรมเพื่อรับจำนวนเต็ม 10 จำนวนใส่ในตัวแปร array แล้วแสดงผลทาง จอภาพ 15

  16. printf("\nSave them to array completely\n"); printf("\nRetrieval----->\n"); for (i =0;i<10 ;i++) printf("entered data[%d] : %d\n",I,data[i]); } int i =0; do{ printf("entered data[%d] : %d\n",i,data[i]); i++; } while (i<10); 16

  17. #include <stdio.h> #include <conio.h> void main() { int score[4],i,sum =0; float avg; for(i=0;i<4;i++) { printf(“Enter Number %d”, i); scanf(“%d”,&score[i]); sum += score[i]; } avg = (float)sum/4; printf(“The Average = %.2f\n”,avg); getch(); } จงเขียนโปรแกรมเพื่อรับค่าคะแนนสอบย่อย 4 ครั้งจากคีย์บอร์ด และจัดเก็บไว้ในตัวแปรแถวลำดับ จากนั้นคำนวณค่าเฉลี่ยและแสดงผลออกทางหน้าจอ 17

  18. จงเขียนโปรแกรมเพื่อรับค่าอุณหภูมิจากคีย์บอร์ด • จัดเก็บไว้ในตัวแปรแถวลำดับชื่อ temper กี่จำนวนก็ได้ แต่ไม่เกิน 50 จำนวน • หยุดป้อนข้อมูลเมื่อป้อนค่าอุณหภูมิน้อยกว่าหรือเท่ากับ 0 โดยแต่ละค่าเป็นเลขทศนิยม • จากนั้นอ่านค่าอุณหภูมิจากตัวแปรแถวลำดับชื่อ temper • คำนวณอุณหภูมิรวม อุณหภูมิเฉลี่ยและแสดงผลออกทางหน้าจอ 18

  19. void main() { int count,i,quit = 0,sum=0,min=(1<<15)-1,max=1<<15; // max = -32768, min = 32767 float avg; int temper[50]; printf(“Stopping input data by keying number <= 0 ); for(i=0; quit==0 && i<50;i++) { printf(“Pls Enter Temperature %d”,i+1); scanf(“%d”,&temper[i]); if (temper[i] <= 0) quit = 1; } count = i-1; // amount of temperature for(i=0; i<count;i++) { sum += temper[i]; if(max < temper[i]) max = temper[i]; if(min > temper[i]) min = temper[i]; } avg = (float)sum/(float)count; printf(“Average is %.2f” ,avg ); printf(“Max is %d” ,max ); printf(“Min is %d” ,min ); } 19

  20. int array1[5] int array2[5] array1[1] = array1[4]; array1[1] = array2[2]; 20

  21. int array1[5] int array2[5] ++array1[0]; array2[array1[0]]+2; 21

  22. int array1[5] int array2[5] --array1[array1[2]]; int i = 2; ++array1[i]; 22

  23. int array1[5] int array2[5] int i = array2[1]; --array1[i]; error 23

  24. #include <stdio.h> void main() { float day[7]; int i; for (i=0;i<=6;++i) { printf("Enter temperature of day no. %d : ",i); scanf("%f",&day[i]); } เขียนโปรแกรมเพื่อเก็บข้อมูลอุณหภูมิของวันต่าง ๆ ใน 1 สัปดาห์ หลังจากนั้นหา วันที่มีอุณหภูมิที่สูงที่สุด 24

  25. printf("\n++++++++++++++++\n"); float Htemp = day[0]; int Hday = 0; for (i=1;i<=6;++i) { if (Htemp < day[i]) { Htemp = day[i]; Hday = i; } } printf("\nDay no. %d is the highest temp. day of this week",Hday); } 25

  26. Array หลายมิติ • Array แบบ 2 มิติ • Array แบบหลายมิติ 26

  27. Array แบบ 2 มิติ • ใช้ index 2 ตัวเพื่อระบุจำนวนสมาชิกในแต่ละ row และ column รูปแบบ datatype varname[row_size][col_size]; float day[7][4]; ตัวอย่าง int a[3][5]; 27

  28. 30.5 day[0] float day[7]; 32.9 day[1] นี่คือ array ที่ใช้เก็บข้อมูลของอุณหภูมิของแต่ละวันใน 1 สัปดาห์ (7 วัน) ถ้าต้องการ array ที่เก็บข้อมูลของทุกวันใน 1 สัปดาห์ (7 วัน) เป็นเวลา 1 เดือน (4 สัปดาห์) = เก็บอุณหภูมิของทุกวันใน 1 เดือน 31.5 day[2] 29.7 day[3] 30.8 day[4] 28.5 day[5] 30.1 day[6] 28

  29. float day[4][7]; ตัวอย่าง int sqr[3][3] = { 1,2,3, 4,5,6, 7,8,9 }; int sqr[][3] = { 1,2,3, 4,5,6, 7,8,9 }; int b [2][2] = {{1,2},{3,4}};

  30. #include <stdio.h> void main() { float day[4][7]; for (int i=0;i<=3;++i) { printf("\n+++++++ Week no. %d +++++++\n",i); for (int j=0;j<=6;++j) { printf("Enter temp. of day no. %d : ",j); scanf("%f",&day[i][j]); } } printf("\n++++++++++++++++\n"); ตัวอย่าง เก็บข้อมูลอุณหภูมิของวันต่าง ๆ ในทุกสัปดาห์ของ 1 เดือน หลังจากนั้น หาวันที่มีอุณหภูมิที่สูงที่สุด 30

  31. float Htemp = day[0][0]; int Hday = 0; int Hweek = 0; for (int i=0;i<=3;++i) { for (int j=0;j<=6;++j) if (Htemp < day[i][j]) { Htemp = day[j][j]; Hday = j; Hweek = i; } } printf("\nDay no. % d of Week no. %d is the highest temp. day of this month",Hday,Hweek); } 31

  32. Array แบบ 3 มิติ • ใช้ index 3 ตัวเพื่อระบุจำนวนสมาชิก รูปแบบ DATATYPE varname[table_size][row_size][col_size]; ตัวอย่าง float day[12][4][7]; int a[2][3][4]; 32

  33. array ที่ใช้เก็บข้อมูลของอุณหภูมิของแต่ละวันใน 1 สัปดาห์ (7 วัน) array ที่เก็บข้อมูลของทุกวันใน 1 สัปดาห์ (7 วัน) เป็นเวลา 1 เดือน (4 สัปดาห์) = เก็บอุณหภูมิของทุกวันใน 1 เดือน array ที่เก็บข้อมูลของทุกวันใน 1 สัปดาห์ (7 วัน) เป็นเวลา 1 เดือน (4 สัปดาห์) ตลอด 1 ปี (12 เดือน) = เก็บอุณหภูมิของทุกวันใน 1 ปี int day[7]; int day[4][7]; int day[12][4][7]; 33

More Related