SlideShare a Scribd company logo
อาเรย์ห ลายมิต ิ
(M
ulti-Dimensional
Arrays)
C# Programming
มิต ิข องอาเรย์


อาเรย์หนึ่งมิติ

0 1 2 3 4 5 6 7 8 9 10 11



ระบุจ ำา นวนเต็ม
หนึ่ง ค่า สำา หรับ
เป็น ดัช นี

อาเรย์สองมิติ
0
1
2
3

A[7]
ระบุจ ำา นวนเต็ม
สองค่า สำา หรับ
เป็น ดัช นี

A[1,4]
0 1 2 3 4 5 6 7 8
การประกาศอาเรย์ห ลาย
มิต ิ

สองมิติ

<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;
int[,] Array2D;
Array2D = new int[3,5];
Array2D = new int[3,5];



สามมิติ
<name> = new <type>[<dim1>,<dim2>,<dim3>];
<name> = new <type>[<dim1>,<dim2>,<dim3>];



ตัวอย่าง
int[,,]
int[,,]
Array3D
Array3D

Array3D;
Array3D;
= new int[3,5,2];
= new int[3,5,2];
ตัว อย่า ง
int[,] Array2D;
int[,] Array2D;
Array2D = new int[3,5];
Array2D = new int[3,5];

int[,] Array3D;
int[,] Array3D;
Array3D = new int[4,3,5];
Array3D = new int[4,3,5];
การกำา หนดค่า เริ่ม ต้น


แบบที่หนึ่ง เช่น
int[,] Array2D;
int[,] Array2D;
Array2D = new int[2,3] {{5,4,3},{2,1,0}};
Array2D = new int[2,3] {{5,4,3},{2,1,0}};



แบบที่สอง เช่น
int[,] Array2D;
int[,] Array2D;
Array2D = new int[,] {
Array2D = new int[,] {
{5,4,3},
{5,4,3},
{2,1,0}
{2,1,0}
};
};



แบบที่สามอย่างย่อ เช่น
int[,] Array2D;
int[,] Array2D;
Array2D = {
Array2D = {
{5,4,3},
{5,4,3},
{2,1,0}
{2,1,0}
};
};

5
2

4
1

3
0
การเข้า ถึง ข้อ มูล ในอาเรย์


จำานวนตัวเลขที่ใช้เป็นดัชนีขึ้นอยู่กับ
จำานวนมิตของอาเรย์
ิ
int[,] A = {
int[,] A = {
{51,24,3},
{51,24,3},
{72,11,90},
{72,11,90},
{42,41,67},
{42,41,67},
{37,1,0}
{37,1,0}
};
};

0
1
2
3

A[2,0]

51 24 3
72 11 90
42 41 67
37 1 0
0 1 2

A[1,1]

A[3,2]
ขนาดของอาเรย์


ทบทวน





Le ng th property
Ge tLe ng th method

GetLength(1)

5
2

4
1

3
0

GetLength(0)

ตัวอย่าง

int[,] Array2D = {
int[,] Array2D = {
{5,4,3},
{5,4,3},
{2,1,0}
{2,1,0}
};
};
Console.WriteLine(Array2D.GetLength(0));
Console.WriteLine(Array2D.GetLength(0));
Console.WriteLine(Array2D.GetLength(1));
Console.WriteLine(Array2D.GetLength(1));
Console.WriteLine(Array2D.Length);
Console.WriteLine(Array2D.Length);

Length
แบบฝึก หัด


พิจารณาอาเรย์
int[,] A = {
int[,] A = {
{5,34,3,1},
{5,34,3,1},
{27,11,90,9},
{27,11,90,9},
{24,41,67,6},
{24,41,67,6},
{73,1,0,4},
{73,1,0,4},
{1,2,3,4}
{1,2,3,4}
};
};



จงหาค่าของนิพจน์
ต่อไปนี
A[2,2]้
A[3,0]
A[1,3]
A.Length
A.GetLength(0)
A.GetLength(1)
แบบฝึก หัด


พิจารณาอาเรย์
int[,] B = {
int[,] B = {
{5,2,3,1,0},
{5,2,3,1,0},
{11,9,10,8,2},
{11,9,10,8,2},
{-1,20,4,33,12},
{-1,20,4,33,12},
{10,11,8,9,7}
{10,11,8,9,7}
};
};



จงหาค่าของนิพจน์
ต่อไปนี
B[2,2]้
B[3,0]
B[1,3]
B.Length
B.GetLength(0)
B.GetLength(1)
การประยุก ต์ใ ช้อ าเรย์ส อง
มิตาหนดให้ Aก ซ์Bเป็นเมตริกซ์ขนาด
 กำ ิ: เมตริ และ
3x3



1 3 2
A = 9 12 0


5 4 6



 4 1 3
B = 5 2 6


6 3 9 



using System;
using System;
class Matrix {{
class Matrix
public static void Main() {{
public static void Main()
int [,] AA ==
1
int [,]

คำานวน A B
+
int [,] BB ==
int [,]

2

int i,j;
int i,j;

}}

}}

for (i == 0; ii << 3; i++) {{
for (i
0;
3; i++)
for (j == 0; jj << 3; j++) {{
for (j
0;
3; j++)
Console.Write("{0,4}", A[i,j]+B[i,j]);
Console.Write("{0,4}", A[i,j]+B[i,j]);
}}
Console.WriteLine();
Console.WriteLine();
}}
เมท็อ ดที่ส ำา คัญ สำา หรับ เม
ตริก ซ์



ReadMatrix()
ShowMatrix()
เมท็อ ด ReadMatrix()


รับค่าพารามิเตอร์สองค่าคือ






จำานวนแถว, nr
จำานวนหลัก, nc

ทำาหน้าที่อ่านค่าจำานวนเต็ม nr×nc ค่าจากผูใช้
้
คืนค่าเป็นอาเรย์สองมิติซึ่งแทนเมตริกซ์
static int[,] ReadMatrix(int nr, int nc) {
static int[,] ReadMatrix(int nr, int nc) {
int[,] m = new int[nr,nc];
int[,] m = new int[nr,nc];
for (int i = 0; i < nr; i++) {
for (int i = 0; i < nr; i++) {
for (int j = 0; j < nc; j++) {
for (int j = 0; j < nc; j++) {
Console.Write("Element [{0},{1}]: ", i+1, j+1);
Console.Write("Element [{0},{1}]: ", i+1, j+1);
m[i,j] = int.Parse(Console.ReadLine());
m[i,j] = int.Parse(Console.ReadLine());
}
}
}
}
return m;
return m;
}
}
เมท็อ ด ShowMatrix()




รับค่าพารามิเตอร์เป็นอาเรย์สองมิติซึ่งแทนเม
ตริกซ์
แสดงข้อมูลในเมตริกซ์ออกหน้าจอ
ไม่มีกvoid นค่าใดใด
ารคื ShowMatrix(int[,] m) {
static
static void ShowMatrix(int[,] m) {
for (int i = 0; i < m.GetLength(0); i++) {
for (int i = 0; i < m.GetLength(0); i++) {
for (int j = 0; j < m.GetLength(1); j++) {
for (int j = 0; j < m.GetLength(1); j++) {
Console.Write("{0,3}", m[i,j]);
Console.Write("{0,3}", m[i,j]);
}
}
Console.WriteLine();
Console.WriteLine();
}
}
}
}
โปรแกรมตัว อย่า ง


ตัวอย่างข้างล่างแสดงการเรียกใช้เมท็
อด ReadMatrix และ ShowMatrix


แสดงเฉพาะส่วน Main เท่านั้น

static void Main() {
static void Main() {
int[,] A;
int[,] A;
Console.Write("Enter #rows: ");
Console.Write("Enter #rows: ");
int nrows = int.Parse(Console.ReadLine());
int nrows = int.Parse(Console.ReadLine());
Console.Write("Enter #columns: ");
Console.Write("Enter #columns: ");
int ncols = int.Parse(Console.ReadLine());
int ncols = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Matrix A");
Console.WriteLine("Enter Matrix A");
A = ReadMatrix(nrows, ncols);
A = ReadMatrix(nrows, ncols);
Console.WriteLine("Matrix A is");
Console.WriteLine("Matrix A is");
ShowMatrix(A);
ShowMatrix(A);
}
}
การคำา นวนเมตริก ซ์


เราสามารถเขียนโปรแกรมเพือคำานวน
่
เมตริกซ์ได้เช่น





การบวกสองเมตริกซ์
การคูณสองเมตริกซ์
การคำานวนหาดีเทอร์มิแนนท์
การหาทรานสโพสของเมตริกซ์
การบวกเมตริก ซ์



เมท็อด AddMatrix ทำาการบวกสองเมตริ
กซ์ และส่งผลลัพธ์เป็นเมตริกซ์ใหม่
โดยสมมติว่าเมตริกซ์ทั้งสองมีขนาดเท่ากัน
static int[,] AddMatrix(int[,] a, int[,] b) {
static int[,] AddMatrix(int[,] a, int[,] b) {
int nr = a.GetLength(0);
int nr = a.GetLength(0);
int nc = a.GetLength(1);
int nc = a.GetLength(1);
int[,] result = new int[nr,nc];
int[,] result = new int[nr,nc];
for (int i = 0; i < nr; i++) {
for (int i = 0; i < nr; i++) {
for (int j = 0; j < nc; j++) {
for (int j = 0; j < nc; j++) {
result[i,j] = a[i,j] + b[i,j];
result[i,j] = a[i,j] + b[i,j];
}
}
}
}
return result;
return result;
}
}
การคูณ เมตริก ซ์


A เป็นเมตริกซ์ขนาด m×n และ B เป็นเมตริกซ์
(A× )[i, ]
ขนาด Bn×jp= A[i,1]×B[1,j] + A[i,2]×B[2,j] + ... + A[i,n]×B[n,j]



เมท็อด MulMatrix ทำาการคูณเมตริกซ์ และ
ส่งผลลัพธ์ในเมตริกซ์ใหม่

สมมติว่า #cols ของเมตริกซ์แรกมีค่าเท่ากับ #rows
static int[,] MulMatrix(int[,] a, int[,] b) {
static int[,] MulMatrix(int[,] a, int[,] b) {
ของเมตริ = new อง
ี่
int[,] resultกซ์ทสint[a.GetLength(0), b.GetLength(1)];
int[,] result = new int[a.GetLength(0), b.GetLength(1)];


}
}

for (int i = 0; i < a.GetLength(0); i++) {
for (int i = 0; i < a.GetLength(0); i++) {
for (int j = 0; j < b.GetLength(1); j++) {
for (int j = 0; j < b.GetLength(1); j++) {
int sum = 0;
int sum = 0;
for (int k = 0; k < a.GetLength(1); k++)
for (int k = 0; k < a.GetLength(1); k++)
sum += a[i,k]*b[k,j];
sum += a[i,k]*b[k,j];
result[i,j] = sum;
result[i,j] = sum;
}
}
}
}
return result;
return result;

More Related Content

PDF
Java-Chapter 10 Two Dimensional Arrays
PDF
Java-Chapter 07 One Dimensional Arrays
PDF
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง 6.1
PPT
อาเรย์ (Array)
PDF
งานทำ Blog บทที่ 8
PDF
งานทำ Blog บทที่ 8
PPT
ตัวแปรชุดและตัวแปรอักขระ
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 07 One Dimensional Arrays
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง 6.1
อาเรย์ (Array)
งานทำ Blog บทที่ 8
งานทำ Blog บทที่ 8
ตัวแปรชุดและตัวแปรอักขระ

What's hot (19)

PPTX
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง
PPTX
โครงสร้างแบบอาร์เรย์
PDF
Java-Chapter 02 Data Operations and Processing
PPT
ตัวแปรชุดและตัวแปรอักขระ PPT
PDF
Java-Chapter 05 String Operations
PDF
บทที่5 ข้อมูลชนิดอาร์เรย์และสตริง
PPT
งาน
PDF
Java-Answer Chapter 07 (For Print)
PDF
Java-Answer Chapter 10-11 (For Print)
PDF
Java-Answer Chapter 08-09 (For Print)
PDF
เมทริกซ์...
PDF
Java-Chapter 08 Methods
PDF
Java-Answer Chapter 08-09
PDF
Java-Answer Chapter 10-11
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง
โครงสร้างแบบอาร์เรย์
Java-Chapter 02 Data Operations and Processing
ตัวแปรชุดและตัวแปรอักขระ PPT
Java-Chapter 05 String Operations
บทที่5 ข้อมูลชนิดอาร์เรย์และสตริง
งาน
Java-Answer Chapter 07 (For Print)
Java-Answer Chapter 10-11 (For Print)
Java-Answer Chapter 08-09 (For Print)
เมทริกซ์...
Java-Chapter 08 Methods
Java-Answer Chapter 08-09
Java-Answer Chapter 10-11
Ad

Similar to 09 multi arrays (20)

PPT
Java Programming [8/12] : Arrays and Collection
DOC
ตัวแปรชุดและตัวแปรอักขระ
PPTX
..Arrays..
PPT
08 arrays
PDF
Java Programming: อะเรย์และคอลเล็กชั่น
PDF
งาน
PPTX
Matrix53
PDF
งานทำ Blog บทที่ 8
PPT
ตัวแปรชุดและตัวแปรอักขระ
PPT
PDF
งานทำ Blog บทที่ 8
PPTX
งานนำเสนอ1
PDF
เมทริกซ์ (Matrix)
PDF
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]
DOC
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขร
PDF
Week1 f end
PDF
การใช้งาน Matlab
PDF
(Big One) C Language - 01 ฟังก์ชันส่งผ่านอาร์เรย์
PPTX
ข้อมูลชนิดอาร์เรย์และสตริง
Java Programming [8/12] : Arrays and Collection
ตัวแปรชุดและตัวแปรอักขระ
..Arrays..
08 arrays
Java Programming: อะเรย์และคอลเล็กชั่น
งาน
Matrix53
งานทำ Blog บทที่ 8
ตัวแปรชุดและตัวแปรอักขระ
งานทำ Blog บทที่ 8
งานนำเสนอ1
เมทริกซ์ (Matrix)
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขร
Week1 f end
การใช้งาน Matlab
(Big One) C Language - 01 ฟังก์ชันส่งผ่านอาร์เรย์
ข้อมูลชนิดอาร์เรย์และสตริง
Ad

More from a-num Sara (10)

PPT
Webprogramming
POT
PPT
10 win apps
PPT
07 methods
PPT
06 for loops
PPT
05 loops
PPT
04 conditional
PPT
02 basic
PPT
01 intro
PPT
C sharp ide
Webprogramming
10 win apps
07 methods
06 for loops
05 loops
04 conditional
02 basic
01 intro
C sharp ide

09 multi arrays