1 / 63

Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

Computer Programming การเขียนโปรแกรมคอมพิวเตอร์. สัปดาห์ที่ 9 ตัวแปรแถวลำดับ (Array). Outline. 1. Objective. 2. Why an Array is Essential?. p. 3. One-Dimensional Array. Two-Dimensional Array. 4. 5. Assignment #5. objectives. เพื่อให้นิสิตรู้จักและเข้าใจ ตัวแปรแถวลำดับ ในภาษาซี

Download Presentation

Computer Programming การเขียนโปรแกรมคอมพิวเตอร์

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. Computer Programmingการเขียนโปรแกรมคอมพิวเตอร์ สัปดาห์ที่ 9 ตัวแปรแถวลำดับ (Array)

  2. Outline 1 Objective 2 Why an Array is Essential? p 3 One-Dimensional Array Two-Dimensional Array 4 5 Assignment #5

  3. objectives • เพื่อให้นิสิตรู้จักและเข้าใจตัวแปรแถวลำดับในภาษาซี • สามารถเขียนโปรแกรมภาษาซีโดยใช้ตัวแปรแถวลำดับได้ • สามารถนำความรู้เรื่องตัวแปรแถวลำดับไปประยุกต์เพื่อแก้ไขโจทย์ปัญหาในชีวิตประจำวันได้ได้อย่างถูกต้องเหมาะสม

  4. Outline 1 Objective 2 Why an Array is Essential? p 3 One-Dimensional Array Two-Dimensional Array 4 5 Assignment #5

  5. Why an array is essential? • ถ้าต้องการเก็บค่าคะแนนสอบ (score) ของนักเรียน จำนวน 20,000 คน เราต้องใช้ตัวแปร 20,000 ตัว เพื่อใช้เก็บค่าคะแนนของนักเรียนทั้งหมด float score1, score2, score3, ..., score20000; • จากข้อจำกัดของชนิดข้อมูลพื้นฐานที่มีอยู่ (char, int, float, double) เราต้องใช้ตัวแปรจำนวนมาก เพื่อเก็บข้อมูลหลายค่า • ภาษาซีจึงได้กำหนดชนิดข้อมูลแบบโครงสร้างที่รวมข้อมูลพื้นฐานดังกล่าวไว้เป็นลำดับหรือเป็นกลุ่ม

  6. The Advantage of Array • อาเรย์ (array) เป็นโครงสร้างข้อมูลประกอบด้วยกลุ่มของข้อมูลชนิดเดียวกันที่เรียงกันเป็นลำดับโดยใช้ตัวแปรเพียงตัวเดียวอ้างถึงเช่น ตัวแปร score เป็นชนิดข้อมูลแบบอาเรย์ ใช้เก็บคะแนนจำนวน 20,000 ค่าพร้อมกันได้ เช่น • ดัชนีที่ใช้จะเป็นเลขจำนวนเต็ม เริ่มจาก 0 ตามลำดับ (0, 1, 2, ...)

  7. Example 1 Regular Format Example 1 : จงเขียนโปรแกรมเพื่อรับรหัสนักศึกษา และคะแนนสอบกลางภาควิชา Computers and Programming ของนักศึกษาห้อง 1 – 10 #include <stdio.h> #include<conio.h> int main () { char id0001[9],id0002[9],id0003[9],...,id1158[9],id1159[9]; float point0001,point0002,point0003,...,pint158,point1159; scanf ("%s",id0001); scanf ("%f",&point0001); ... scanf ("%s",id1159); scanf ("%f",&point1159); return 0; }

  8. Example 1 in Array Format #include<stdio.h> #include<conio.h> int main() { charid [1159] [9]; float point [1159]; inti; for (i=0;i<1159;i++) { scanf ("%s", id [ i ]); scanf ("%f", &point [ i ]); } return 0; }

  9. num [0] num [1] num [2] num [3] num [4] num [5] num [6] num [7] num [8] -23 -22 1 9 -1 44 2 6 -9 num[9] Type of Array One-dimensional Array Column Column [0] [1] [2] [0] [1] [2] [3] Row [0] Row [1] [0] Row [2] Row [1] [3] [2] Row [3] [1] Depth [2] [0] Multi-dimensional Array

  10. Outline 1 Objective 2 Why an Array is Essential? p 3 One-Dimensional Array Two-Dimensional Array 4 5 Assignment #5

  11. One-dimensional Array Variable Declaration typearray_name[size]; • ตัวแปรแถวลำดับ อะเรย์ หรือ อาเรย์ : “An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.” • type • ชนิดของตัวแปร • array_name • ชื่อของตัวแปรแถวลำดับที่ประกาศหรือตั้งโดยผู้เขียนโปรแกรม • size • ขนาดของตัวแปรแถวลำดับที่จะใช้ (จะใช้เท่าไรให้จองเผื่อไว้ 1 ช่องสำหรับ \0(null string))

  12. Declaration & Assignment Examples (Memory) charstudent_name [5]; student_name [0] = ‘A’; student_name [2] = ‘B’; intbill [5]; intbilly [5] = { 16, 2, 77, 40, 12071 }; float Point [20]; Point [19] = 3.57; doublex_data [100]; x_data [99] = 9.86975; 8 bits, 2 complement 32 bits, 2 complement

  13. Declaration & Assignment Examples (Value) charstudent_name [5]; student_name [0] = ‘A’; student_name [2] = ‘B’; intbill [5]; intbilly [5] = { 16, 2, 77, 40, 12071 }; float Point [20]; Point [19] = 3.57; doublex_data [100]; x_data [99] = 9.86975; Column [4] [0] [2] [1] [3] Row [0] student_name Column [4] [0] [2] [1] [3] Row [0] billy Column [20] [16] [18] [17] [19] Row [0] Point Column [100] [96] [98] [97] [99] Row [0] x_data

  14. Declaration & Assignment to Array type array-name[n]={value-1,value-2,…,value-n}; • type • ชนิดของตัวแปรเช่น int, float, char • array_name • ชื่อของตัวแปรแถวลำดับที่ประกาศหรือตั้งโดยผู้เขียนโปรแกรม • value-1,value-2,…,value-n • เป็นข้อมูลที่จะทำการกำหนดให้กับตัวแปรแถวลำดับ โดยจะต้องเป็นข้อมูลชนิดเดียวกับ type ที่กำหนด

  15. Declaration & Assignment Examples (cont.) #include<stdio.h> #include<conio.h> int main() { intnumber[3] = {23, -186, 43}; float value_2[5]={0.98,43.213,-3.47,52.08,-0.987}; char vowel[5] = {'a','e','i','o','u'}; char name[9] = {'E','n','g','i','n','e','e','r','\0'}; return 0; }

  16. References Data in Array #include<stdio.h> int main() { intyear[5] = {2552,2553,2554,2555,2556}; printf ("%d\n",year[0]); printf ("%d\n",year[1]); printf ("%d\n",year[2]); printf ("%d\n",year[3]); printf ("%d\n",year[4]); return 0; }

  17. Example 1 • Output Analysis • ไม่มี • Input Analysis • อายุของผู้ใช้งานทั้ง 20 คนที่ป้อนเข้ามา • Process Analysis • สร้างตัวแปรแถวลำดับขนาด 20 เพื่อเก็บค่าอายุ โปรแกรมวนรอบเพื่อรอรับค่าจากผู้ใช้งาน • Variable Define • age[20] เป็นตัวแปรแถวลำดับชนิดจำนวนเต็มขนาด 20 เพื่อใช้เก็บค่าอายุ • count เป็นตัวแปรชนิดจำนวนเต็มเพื่อใช้นับจำนวนรอบของ for-loop • Example 1 :จงเขียนผังงานและโปรแกรมเพื่อเก็บอายุของผู้ใช้งานจำนวน 20 คนโดยเก็บข้อมูลอายุในตัวแปรชนิดอาร์เรย์

  18. Process & Pseudo-code Process เริ่มต้นทำงาน ทำการวนรอบเพื่อรับอายุของผู้ใช้งานทั้ง 20 คนที่ป้อนเข้ามาแล้วเก็บไว้ในตัวแปรแถวลำดับ จบการทำงาน Pseudo-code Begin For ( i = 1 && i < = 20 ) { Keep the age of users } End

  19. START age[20],count count = 0 False count<20 True age[count] count++ END

  20. Code of Example 1 #include<stdio.h> #include<conio.h> int main() { intage[20],count; for (count=0; count<20; count++) { printf("Enter age[%d] : ",count); scanf("%d",&age[count]); } printf ("Finish\n"); return 0; }

  21. Example 2 • Example 2 : จงเขียนผังงานและโปรแกรม เพื่อรับจำนวนนักศึกษาในห้อง หลังจากนั้น ให้โปรแกรมรอรับส่วนสูงของคน n คน แล้ววิเคราะห์ว่ามีนักศึกษาในห้องมีส่วนสูงช่วงต่างๆ จำนวนกี่คน แล้วคำนวณส่วนสูงเฉลี่ย แล้วแสดงผลส่วนสูงของนักศึกษาทั้งหมด

  22. Problem Analysis • Output Analysis • จำนวนนักศึกษาที่สูงแต่ละช่วง • ส่วนสูงของนักศึกษาเฉลี่ยในห้อง • ส่วนสูงของนักศึกษาทั้งหมด • Input Analysis • จำนวนนักศึกษาทั้งหมด และส่วนสูงของแต่ละคน • Process Analysis • โปรแกรมรับจำนวนนักศึกษา • วนรอบเพื่อรับส่วนสูงเท่ากับจำนวนนักศึกษา • วนรอบเพื่อตรวจสอบช่วงส่วนสูงของนักศึกษาและหาผลรวมส่วนสูงของนักศึกษาทุกคน • คำนวณหาค่าเฉลี่ย

  23. Problem Analysis (cont.) • Variable Define • num เป็นจำนวนเต็มเพื่อเก็บจำนวนนักศึกษา • a เป็นจำนวนเต็มเพื่อตรวจตำแหน่งตัวแปร และนับรอบ • range1=0, range2=0, range3=0, range4=0 เป็นจำนวนเต็มสำหรับเก็บค่าจำนวนนักศึกษาแต่ละช่วง • high[300] เป็นตัวแปรแถวลำดับชนิดทศนิยมเพื่อเก็บส่วนสูง • avg = 0 เป็นจำนวนทศนิยมเพื่อเก็บค่าผลรวมและค่าเฉลี่ย

  24. Process • เริ่มต้นทำงาน • ทำการรับจำนวนนักศึกษา • วนรอบเท่ากับจำนวนนักศึกษาที่ป้อนเข้ามาเพื่อรับส่วนสูงของนักศึกษาแต่ละคน • วนรอบเพื่อตรวจสอบจำนวนของนักศึกษาที่มีความสูงตรงกับแต่ละช่วงความสูงและหาค่าผลรวมความสูงของนักศึกษาทั้งหมด • หาค่าเฉลี่ยความสูง • จบการทำงาน

  25. Pseudo Code • Begin • Read amount of students • Repeat • { • Read each height of student • Check and increase amount of each range • Calculate the summation of height • } • Calculate the average height • End

  26. START num,a range1=0,range2=0 range3=0,range4=0 high[300],avg=0 num a=0 a<num True False high[a] (2) a++

  27. (2) a=0 True False a<num T <=160 range1++ F T <=170 range2++ avg=avg/num F T <=180 range3++ (3) F range4++ avg=avg+high[a] a++

  28. (3) range1 range2 range3 range4 avg a=0 False True a<num high[a] END a++

  29. #include<stdio.h> #include<conio.h> int main() { int num,a,range1=0,range2=0,range3=0,range4=0; floathigh[300],avg=0; printf ("Please enter number of student : "); scanf ("%d",&num); for (a=0; a<num; a++) { printf ("Student %2d : ",a+1); scanf ("%f",&high[a]); }

  30. for (a=0; a<num; a++) { if (high[a]<=160) range1++; else if (high[a]<=170) range2++; else if (high[a]<=180) range3++; else range4++; avg = avg + high[a]; } avg = avg/num;

  31. printf ("\n 0 - 160 : %3d",range1); printf ("\n161 - 170 : %3d",range2); printf ("\n171 - 180 : %3d",range3); printf ("\n181 - 200 : %3d",range4); printf ("\n\nAverage : %f ",avg); for (a=0; a<num; a++) { printf ("%.2f ",high[a]); } return 0; }

  32. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] C l a n g u a g e \0 subject String & Array variable char subject[11] = {"C language"}; or char subject[11] = {'C', ' ', 'l', 'a', 'n', 'g', 'u', 'a', 'g', 'e', '\0'}; char name[9] = {"Engineer"}; [0] [1] [2] [3] [4] [5] [6] [7] [8] E n g i n e e r \0 name

  33. Example 3 #include<stdio.h> #include<conio.h> int main () { char sentence[22]="Welcome to my country"; char word[9]={'T','h','a','i','l','a','n','d','\0'}; char not_word[4]={'l','o','v','e'}; printf("Message1 = %s\n",sentence); printf("Message2 = %s\n",word); printf("Message3 = %s\n",not_word); return 0; }

  34. Outline 1 Objective 2 Why an Array is Essential? p 3 One-Dimensional Array Two-Dimensional Array 4 5 Assignment #5

  35. Multi-dimensional Array Variable Declaration typearray_name[size_1][size_2]; typearray_name[m][n]; • type • ชนิดของตัวแปรเช่น int, float, char • array_name • ชื่อของตัวแปรแถวลำดับที่ประกาศหรือตั้งโดยผู้เขียนโปรแกรม • size_1 or m • ขนาดของตัวแปรแถวลำดับที่จะใช้มิติที่ 1 (ปกติจะจองไว้สำหรับดัชนีของแถว (row)) • size_2 or n • ขนาดของตัวแปรแถวลำดับที่จะใช้มิติที่ 2 (ปกติจะจองไว้สำหรับดัชนีของสดมภ์ (column))

  36. Assignment Value to Multi-dimensional Array typearray-name[m][n]={value-1-1,value-1-2,…,value-1-m, value-2-1,value-2-2,…,value-2-m, …, value-n-1,value-n-2,…,value-n-m}; • type • ชนิดของตัวแปรเช่น int, float, char • array_name • ชื่อของตัวแปรแถวลำดับที่ประกาศหรือตั้งโดยผู้เขียนโปรแกรม • m • ขนาดของตัวแปรแถวลำดับที่จะใช้มิติที่ 1 (ปกติจะจองไว้สำหรับดัชนีของแถว (row)) • n • ขนาดของตัวแปรแถวลำดับที่จะใช้มิติที่ 2 (ปกติจะจองไว้สำหรับดัชนีของสดมภ์ (column)) • value-1-1, value-1-2, …, value-1-n, …, …, value-n-m • เป็นข้อมูลที่จะทำการกำหนดให้กับตัวแปรแถวลำดับ โดยจะต้องเป็นข้อมูลชนิดเดียวกับ type ที่กำหนด

  37. Declaration & Assignment Examples intmatrix [2][2]; matrix [0][0] = ‘2’; matrix [0][1] = ‘7’; matrix [1][0] = ‘9’; matrix [1][1] = ‘5’ double data_array[2][2]; data_array[2][2] = {1.0,2.0,3.0,4.0}; ordata_array[2][2] = {{1.0,2.0},{3.0,4.0}}; ordata_array[ ][2] = {{1.0,2.0},{3.0,4.0}}; Column [1] [0] Row [0] Row [1] Column [0] [1] [0] [1]

  38. Declaration & Assignment Examples (cont.) doubledata_array [2][2]; data_array[0][0] = 1.0; data_array[0][1] = 2.0; data_array[1][0] = 3.0; data_array[1][1] = 4.0; Column [1] [0] Row [0] Row [1]

  39. 11 12 13 14 21 22 23 24 31 33 34 32 num[0][0] num[0][1] num[0][2] num[0][3] 11 12 13 14 num[1][0] num[1][1] num[1][2] num[1][3] 22 21 23 24 32 31 33 34 num[2][0] num[2][1] num[2][2] num[2][3] Declaration & Assignment Examples (cont.) int num[3][4] = { 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34 }; int num[3][4] = { 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34 };

  40. Declaration & Assignment Examples (cont.) float matrix[2][4] = { 0.19, -0.01, -0.23, 4.44, -4.44, 0.26, -0.09, -0.22 }; float matrix[2][4] = { 0.19, -0.01, -0.23, 4.44, -4.44, 0.26, -0.09, -0.22 }; matrix[0][0] matrix[0][1] matrix[0][2] matrix[0][3] 0.19 -0.01 -0.23 4.44 matrix[1][0] matrix[1][1] matrix[1][2] matrix[1][3] -4.44 0.26 -0.09 -0.22

  41. Declaration & Assignment Examples (cont.) char letter[2][4] = { 'G', 'o', 'o', 'D', 'T', 'i', 'm', 'E'}; char letter[2][4] = {'G', 'o', 'o', 'D', 'T', 'i', 'm', 'E'}; letter[0][0] letter[0][1] letter[0][2] letter[0][3] G o o D T i m E letter[1][0] letter[1][1] letter[1][2] letter[1][3]

  42. Declaration & Assignment Examples (cont.) charstr[2][10] = {"Computer", "Engineer" }; charstr[2][10] = {"Computer", "Engineer" }; [0][8] r p t e o m u \0 C str[0] r i e e n g n \0 E str[1] [1][8]

  43. Multi-dimensional Example #include <stdio.h> #define WIDTH 5 #define HEIGHT 3 int square [HEIGHT][WIDTH]; intn,m; int main () { for (n=0;n<HEIGHT;n++) for (m=0;m<WIDTH;m++) { square[n][m]=(n+1)*(m+1); printf("square [%d][%d] = %d\n", n, m,square[n][m] ); } return 0; }

  44. Example 4 • Example 4 : จงเขียนโปรแกรมเพื่อรับค่าจำนวนเต็มในรูปแบบเมตริกซ์โดยเก็บค่าไว้ในตัวแปร แถวลำดับ ขนาด 3  3 แล้วแสดงผลเมตริกซ์ Enter numbers [0][0] : 1 Enter numbers [0][1] : 2 Enter numbers [0][2] : 3 Enter numbers [1][0] : 4 Enter numbers [1][1] : 5 Enter numbers [1][2] : 6 Enter numbers [2][0] : 7 Enter numbers [2][1] : 8 Enter numbers [2][2] : 9 ***Matrix*** 1 2 3 4 5 6 7 8 9

  45. #include<stdio.h> #include<conio.h> int main() { int matrix[3][3],r,c; for (r=0; r<3; r++) { for(c=0; c<3; c++) { printf ("Enter numbers [%d][%d] : ",r,c); scanf ("%d",&matrix[r][c]); } } printf ("\n*** Matrix ***\n"); for (r=0; r<3; r++) { for(c=0; c<3; c++) { printf ("%5d ",matrix[r][c]); } printf ("\n"); } return 0; }

  46. Example 5 • Example 5 : จากตัวแปรแถวลำดับที่กำหนดให้ จงเขียนโปรแกรมหาผลรวมของจำนวนในแต่ละหลัก และผลรวมของจำนวนในแต่ละแถว โดยเก็บค่าผลรวมไว้ในตัวแปร row [ ], column [ ] intnum[3][4] = { 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6 };

  47. *** Show Matrix *** 1 2 3 4 2 3 4 5 3 4 5 6 Sum of row[0] = 10 Sum of row[1] = 14 Sum of row[2] = 18 Sum of column[0] = 6 Sum of column[1] = 9 Sum of column[2] = 12 Sum of column[3] = 15

  48. #include<stdio.h> #include<conio.h> int main() { int num[3][4] = { 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6 }; intr,c,row[3]={0,0,0},column[4]={0,0,0,0}; /* Display Matrix */ printf ("\n*** Show Matrix ***\n\n"); for (r=0; r<3; r++) { for(c=0; c<4; c++) printf ("%5d ",num[r][c]); printf ("\n\n"); }

  49. /* Summation Matric */ for (r=0; r<3; r++) for(c=0; c<4; c++) { row[r] = row[r] + num[r][c]; column[c] = column[c] + num[r][c]; } /* Display Summation */ printf ("\n\n"); for (r=0; r<3; r++) printf ("sum of row [%d] = %d\n",r,row[r]); for (c=0; c<4; c++) printf ("sum of column [%d] = %d\n",c,column[c]); return 0; }

  50. Example 6 Example 6 : จงเขียนผังงานและโปรแกรม เพื่อรับข้อความเข้ามาแล้วตรวจสอบว่ามีทั้งหมดกี่ประโยค • วิเคราะห์โจทย์ • เขียนขั้นตอนการทำงานอย่างละเอียด • เขียนรหัสเทียม • เขียนแผนภาพการไหลของข้อมูล • เขียนโค้ด

More Related