Upload
a-num-sara
View
228
Download
0
Embed Size (px)
Citation preview
อาเรย ์หลายมติ ิ(Multi-Dimensional Arrays)
C# Programming
มิต ิของอาเรย ์ อาเรย์หนึ่งมิติอาเรย์หนึ่งมิติ
อาเรย์สองมิติอาเรย์สองมิติ
00 11 22 33 44 55 66 77 88 99 1010 1111
00 11 22 33 44 55 66 77 88
33
22
11
00
A[7]A[7]
A[1,4]A[1,4]
ระบุจ ำานวนเต ็มระบ ุจ ำานวนเต ็มหนึ่งค ่าส ำาหร ับหนึ่งค ่าส ำาหร ับ
เป ็นด ัชนีเป ็นด ัชนี
ระบ ุจ ำานวนเตม็ระบ ุจ ำานวนเตม็สองค่าส ำาหร ับสองค่าส ำาหร ับ
เป ็นด ัชนีเป ็นด ัชนี
การประกาศอาเรย ์หลายมิต ิ สองมิติสองมิติ
ตัวอยา่งตัวอยา่ง
สามมิติสามมิติ
ตัวอยา่งตัวอยา่ง
<type>[,] <name>;<type>[,] <name>;
int[,] Array2D;int[,] Array2D;
<type>[,,] <name>;<type>[,,] <name>;
int[,,] Array3D;int[,,] Array3D;
การสรา้งอาเรย ์หลายมิต ิ สองมิติสองมิติ
ตัวอยา่งตัวอยา่ง
สามมิติสามมิติ
ตัวอยา่งตัวอยา่ง
<name> = new <type>[<dim1>,<dim2>];<name> = new <type>[<dim1>,<dim2>];
int[,] Array2D;Array2D = new int[3,5];
int[,] Array2D;Array2D = new int[3,5];
<name> = new <type>[<dim1>,<dim2>,<dim3>];<name> = new <type>[<dim1>,<dim2>,<dim3>];
int[,,] Array3D;Array3D = new int[3,5,2];
int[,,] Array3D;Array3D = new int[3,5,2];
ตัวอย ่าง
int[,] Array2D;Array2D = new int[3,5];
int[,] Array2D;Array2D = new int[3,5];
int[,] Array3D;Array3D = new int[4,3,5];
int[,] Array3D;Array3D = new int[4,3,5];
การกำาหนดค่าเร ิ่มต ้น แบบที่หนึ่ง เช่นแบบที่หนึ่ง เช่น
แบบที่สอง เช่นแบบที่สอง เช่น
แบบที่สามอย่างย่อ เช่นแบบที่สามอย่างย่อ เช่น
int[,] Array2D;Array2D = new int[2,3] {{5,4,3},{2,1,0}};
int[,] Array2D;Array2D = new int[2,3] {{5,4,3},{2,1,0}};
int[,] Array2D;Array2D = new int[,] { {5,4,3}, {2,1,0}};
int[,] Array2D;Array2D = new int[,] { {5,4,3}, {2,1,0}};
int[,] Array2D;Array2D = { {5,4,3}, {2,1,0}};
int[,] Array2D;Array2D = { {5,4,3}, {2,1,0}};
55 44 3322 11 00
การเข ้าถ ึงข ้อม ูลในอาเรย ์ จำานวนตวัเลขที่ใชเ้ป็นดัชนขีึ้นอยู่กับจำานวนตวัเลขที่ใชเ้ป็นดัชนขีึ้นอยู่กับ
จำานวนมิตขิองอาเรย์จำานวนมิตขิองอาเรย์5151 2424 337272 1111 9090
0011
4242 4141 67673737 11 00
2233
00 11 22
int[,] A = { {51,24,3}, {72,11,90}, {42,41,67}, {37,1,0}};
int[,] A = { {51,24,3}, {72,11,90}, {42,41,67}, {37,1,0}};
11114242
00
A[3,2]A[3,2]
A[1,1]A[1,1]
A[2,0]A[2,0]
ขนาดของอาเรย ์ ทบทวนทบทวน
Leng thLeng th property property Ge tLeng thGe tLeng th method method
ตัวอย่างตัวอย่างint[,] Array2D = { {5,4,3}, {2,1,0}};Console.WriteLine(Array2D.GetLength(0));Console.WriteLine(Array2D.GetLength(1));Console.WriteLine(Array2D.Length);
int[,] Array2D = { {5,4,3}, {2,1,0}};Console.WriteLine(Array2D.GetLength(0));Console.WriteLine(Array2D.GetLength(1));Console.WriteLine(Array2D.Length);
55 44 33
22 11 00GetLength(0)
GetLength(1)
Length
แบบฝึกห ัด พิจารณาอาเรย์พิจารณาอาเรย์ จงหาค่าของนิพจน์จงหาค่าของนิพจน์
ต่อไปนี้ต่อไปนี้int[,] A = { {5,34,3,1}, {27,11,90,9}, {24,41,67,6}, {73,1,0,4}, {1,2,3,4}};
int[,] A = { {5,34,3,1}, {27,11,90,9}, {24,41,67,6}, {73,1,0,4}, {1,2,3,4}};
A[2,2]A[2,2]A[3,0]A[3,0]A[1,3]A[1,3]A.LengthA.LengthA.GetLength(0)A.GetLength(0)A.GetLength(1)A.GetLength(1)
แบบฝึกห ัด พิจารณาอาเรย์พิจารณาอาเรย์ จงหาค่าของนิพจน์จงหาค่าของนิพจน์
ต่อไปนี้ต่อไปนี้int[,] B = { {5,2,3,1,0}, {11,9,10,8,2}, {-1,20,4,33,12}, {10,11,8,9,7}};
int[,] B = { {5,2,3,1,0}, {11,9,10,8,2}, {-1,20,4,33,12}, {10,11,8,9,7}};
B[2,2]B[2,2]B[3,0]B[3,0]B[1,3]B[1,3]B.LengthB.LengthB.GetLength(0)B.GetLength(0)B.GetLength(1)B.GetLength(1)
การประย ุกต ์ใช ้อาเรย ์สองม ิต ิ: เมตรกิซ ์ กำาหนดให้กำาหนดให้ AA และ และ BB เป็นเมตริกซ์ขนาด เป็นเมตริกซ์ขนาด
3x33x3
คำานวน คำานวน AA++BB
=
645
0129
231
A
=
936
625
314
B
using System;class Matrix { public static void Main() { int [,] A =
int [,] B =
int i,j;
for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { Console.Write("{0,4}", A[i,j]+B[i,j]); } Console.WriteLine(); } }}
using System;class Matrix { public static void Main() { int [,] A =
int [,] B =
int i,j;
for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { Console.Write("{0,4}", A[i,j]+B[i,j]); } Console.WriteLine(); } }}
11
22
เมท ็อดที่ส ำาค ัญสำาหรบัเมตร ิกซ์ ReadMatrixReadMatrix()() ShowMatrixShowMatrix()()
เมท ็อด ReadMatrix() รับคา่พารามิเตอร์สองคา่คือรับคา่พารามิเตอร์สองคา่คือ
จำานวนแถวจำานวนแถว, , nrnr จำานวนหลักจำานวนหลัก, , ncnc
ทำาหน้าที่อ่านคา่จำานวนเต็ม ทำาหน้าที่อ่านคา่จำานวนเต็ม nrnr××ncnc คา่จากผูใ้ช้คา่จากผูใ้ช้ คืนค่าเปน็อาเรย์สองมิติซึ่งแทนเมตริกซ์คืนค่าเปน็อาเรย์สองมิติซึ่งแทนเมตริกซ์
static int[,] ReadMatrix(int nr, int nc) { int[,] m = new int[nr,nc]; for (int i = 0; i < nr; i++) { for (int j = 0; j < nc; j++) { Console.Write("Element [{0},{1}]: ", i+1, j+1); m[i,j] = int.Parse(Console.ReadLine()); } } return m;}
static int[,] ReadMatrix(int nr, int nc) { int[,] m = new int[nr,nc]; for (int i = 0; i < nr; i++) { for (int j = 0; j < nc; j++) { Console.Write("Element [{0},{1}]: ", i+1, j+1); m[i,j] = int.Parse(Console.ReadLine()); } } return m;}
เมท ็อด ShowMatrix() รับคา่พารามิเตอร์เปน็อาเรย์สองมิติซึ่งแทนเมรับคา่พารามิเตอร์เปน็อาเรย์สองมิติซึ่งแทนเม
ตริกซ์ตริกซ์ แสดงข้อมูลในเมตริกซ์ออกหน้าจอแสดงข้อมูลในเมตริกซ์ออกหน้าจอ ไม่มีการคนืค่าใดใดไม่มีการคนืค่าใดใด
static void ShowMatrix(int[,] m) { for (int i = 0; i < m.GetLength(0); i++) { for (int j = 0; j < m.GetLength(1); j++) { Console.Write("{0,3}", m[i,j]); } Console.WriteLine(); }}
static void ShowMatrix(int[,] m) { for (int i = 0; i < m.GetLength(0); i++) { for (int j = 0; j < m.GetLength(1); j++) { Console.Write("{0,3}", m[i,j]); } Console.WriteLine(); }}
โปรแกรมตัวอย ่าง ตัวอย่างข้างลา่งแสดงการเรียกใชเ้มท็ตัวอย่างข้างลา่งแสดงการเรียกใชเ้มท็
อด อด ReadMatrixReadMatrix และ และ ShowMatrixShowMatrix แสดงเฉพาะสว่น แสดงเฉพาะสว่น MainMain เท่านั้นเท่านั้น
static void Main() { int[,] A; Console.Write("Enter #rows: "); int nrows = int.Parse(Console.ReadLine()); Console.Write("Enter #columns: "); int ncols = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Matrix A"); A = ReadMatrix(nrows, ncols); Console.WriteLine("Matrix A is"); ShowMatrix(A);}
static void Main() { int[,] A; Console.Write("Enter #rows: "); int nrows = int.Parse(Console.ReadLine()); Console.Write("Enter #columns: "); int ncols = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Matrix A"); A = ReadMatrix(nrows, ncols); Console.WriteLine("Matrix A is"); ShowMatrix(A);}
การคำานวนเมตร ิกซ์ เราสามารถเขียนโปรแกรมเพือ่คำานวนเราสามารถเขียนโปรแกรมเพือ่คำานวน
เมตริกซ์ได้เชน่เมตริกซ์ได้เชน่ การบวกสองเมตริกซ์การบวกสองเมตริกซ์ การคณูสองเมตริกซ์การคณูสองเมตริกซ์ การคำานวนหาดีเทอร์มิแนนท์การคำานวนหาดีเทอร์มิแนนท์ การหาทรานสโพสของเมตริกซ์การหาทรานสโพสของเมตริกซ์
การบวกเมตร ิกซ์ เมท็อด เมท็อด AddMatrixAddMatrix ทำาการบวกสองเมตริทำาการบวกสองเมตริ
กซ์ และส่งผลลัพธเ์ปน็เมตริกซ์ใหม่กซ์ และส่งผลลัพธเ์ปน็เมตริกซ์ใหม่ โดยสมมติว่าเมตริกซ์ทั้งสองมีขนาดเท่ากันโดยสมมติว่าเมตริกซ์ทั้งสองมีขนาดเท่ากัน
static int[,] AddMatrix(int[,] a, int[,] b) { int nr = a.GetLength(0); int nc = a.GetLength(1); int[,] result = new int[nr,nc]; for (int i = 0; i < nr; i++) { for (int j = 0; j < nc; j++) { result[i,j] = a[i,j] + b[i,j]; } } return result;}
static int[,] AddMatrix(int[,] a, int[,] b) { int nr = a.GetLength(0); int nc = a.GetLength(1); int[,] result = new int[nr,nc]; for (int i = 0; i < nr; i++) { for (int j = 0; j < nc; j++) { result[i,j] = a[i,j] + b[i,j]; } } return result;}
การคูณเมตร ิกซ์ AA เปน็เมตริกซ์ขนาด เปน็เมตริกซ์ขนาด mm××nn และ และ BB เปน็เมตริกซ์เปน็เมตริกซ์
ขนาด ขนาด nn××pp
เมท็อด เมท็อด MulMatrixMulMatrix ทำาการคณูเมตริกซ์ และทำาการคณูเมตริกซ์ และส่งผลลัพธใ์นเมตริกซ์ใหม่ส่งผลลัพธใ์นเมตริกซ์ใหม่ สมมติว่า สมมติว่า #cols #cols ของเมตริกซแ์รกมคี่าเทา่กับ ของเมตริกซแ์รกมคี่าเทา่กับ #rows #rows
ของเมตริกซ์ทีส่องของเมตริกซ์ทีส่องstatic int[,] MulMatrix(int[,] a, int[,] b) { int[,] result = new int[a.GetLength(0), b.GetLength(1)]; for (int i = 0; i < a.GetLength(0); i++) { for (int j = 0; j < b.GetLength(1); j++) { int sum = 0; for (int k = 0; k < a.GetLength(1); k++) sum += a[i,k]*b[k,j]; result[i,j] = sum; } } return result;}
static int[,] MulMatrix(int[,] a, int[,] b) { int[,] result = new int[a.GetLength(0), b.GetLength(1)]; for (int i = 0; i < a.GetLength(0); i++) { for (int j = 0; j < b.GetLength(1); j++) { int sum = 0; for (int k = 0; k < a.GetLength(1); k++) sum += a[i,k]*b[k,j]; result[i,j] = sum; } } return result;}
((AA××BB)[)[ii,,jj] = ] = AA[[ii,1],1]××BB[1,[1,jj] + ] + AA[[ii,2],2]××BB[2,[2,jj] + ... + ] + ... + AA[[ii,,nn]]××BB[[nn,,jj]]