Download Presentation

Loading in 2 Seconds...

1 / 67

# 鄭士康 國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所 - PowerPoint PPT Presentation

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

## 鄭士康 國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### 陣列

• 一維陣列
• 亂數產生器
• 猜數字遊戲
• 矩形二維陣列
• 井字遊戲Tic-tac-toe
• *不規則二維陣列
• *高維陣列
• foreach 敘述

• 一維陣列
• 亂數產生器
• 猜數字遊戲
• 矩形二維陣列
• 井字遊戲Tic-tac-toe
• *不規則二維陣列
• *高維陣列
• foreach 敘述
p1 = 2;

p2 = 3;

p3 = 5;

p4 = 7;

p5 = 11;

p[0] = 2;

p[1] = 3;

p[2] = 5;

p[3] = 7;

p[4] = 11;

. . .

Console.WriteLine(p[i-1]);

p[0]

p[1]

p[2]

p[3]

p[4]

• 一維陣列與陣列索引

int[] p = new int [5];

const int N = 5;

int[] p = new int[N];

p[0] = 2;

p[1] = 3;

p[2] = 5;

p[3] = 7;

p[4] = 11;

int i;

for (i = 0; i < N; ++i)

{

Console.WriteLine(p[i]);

}

• 「索引在陣列的界限之外」 錯誤
• 語意錯誤
• 需要仔細閱讀程式碼，或以偵錯器追蹤索引的變化過程

int[] p = new int[5] { 2, 3, 5, 7, 11 };

int[] p = { 2, 3, 5, 7, 11 };

int[] p = { 2, 3, 5, 7, 11 };

int i;

for (i = 0; i < p.Length; ++i)

{

Console.WriteLine(p[i]);

}

Console.Write("輸入年數: ");

int n = Int32.Parse(Console.ReadLine());

double[] m = new double[n];

int i;

for (i = 0; i < n; ++i)

{

m[i] = c*Math.Pow(1.0 + p, i+1);

}

for (i = 0; i < n; ++i)

{

Console.WriteLine("第{0}年後的本利和為{1}", i+1, m[i]);

}

• 寫一程式，用迴圈輸入3位同學成績到一維陣列，再用迴圈把所有成績顯示出來.
• 修改上述程式, 使同學人數可以在程式執行時決定.

• 一維陣列
• 亂數產生器
• 猜數字遊戲
• 矩形二維陣列
• 井字遊戲Tic-tac-toe
• *不規則二維陣列
• *高維陣列
• foreach 敘述

• 依一定機率分佈隨機產生的數
• 均勻分佈之亂數通常以整數除法的餘數產生
• 亂數產生器
• 亂數種子與亂數數列
UsingRandom.Program片段

int seed = 123;

Random rand = new Random(seed);

int[] count = new int[10];

for (k = 0; k < 10; ++k)

{

count[k] = 0;

}

const int N = 1000;

for (i = 0; i < N; ++i)

{

k = rand.Next() % 10;

++count[k];

}

Random rand = new Random();

• 產生一萬個0到9之間的亂數，統計各點數出現次數，以星號繪出對應長條圖, 每個星號代表出現50次
• 例如：

0 ********************

• *********************
• *******************
• ********************
• *****************
• **********************
• ******************

. . .

9 *********************

• 一維陣列
• 亂數產生器
• 猜數字遊戲
• 矩形二維陣列
• 井字遊戲Tic-tac-toe
• *不規則二維陣列
• *高維陣列
• foreach 敘述

• 電腦先隨機產生一個四個位數都不同的四位數作為謎底
• 最高位數字(千位數)可以是0
• 接著使用者猜測一個四位數
• 電腦比較所猜的數字與謎底
• 假設使用者所猜的四個位數中有m位數，不論位置，在謎底中出現；而其中n位數，其數值與位置都與謎底相同，則電腦告知使用者nAmB的訊息
• 使用者產生下一次所猜的數字
nAmB一例
• 謎底為0567
• 所猜數字為5566
• 5、5、6、6均出現在謎底中，所以m等於4
• 其中一個5一個6與謎底的第二與第三位數之位置與數值均相同，所以n等於2
• 電腦顯示2A4B

1 利用亂數產生一個四位數字都不同的四位數x

2 x的四個位數為xd[0]~xd[3]

3 do

{

3.1 使用者輸入所猜四位數y

3.2 決定並輸出nAnB

3.3 if( n == 4 )

{

3.3.1 輸出猜對訊息

3.3.2 break

}

} while ( 猜測次數未達上限 )

4 輸出謎底與猜測次數

// 謎底xd[0]~xd[3]

// 使用者輸入yd[0]~yd[3]

for(i=0; i<4; ++i)

{

for(j=0; j<4; ++j)

{

if( yd[i] != xd[j] ) continue

++m

if( i == j ) ++n

break

}

}

xd[0] = '0';

xd[1] = '5';

xd[2] = '6';

xd[3] = '7';

char[] yd = new char[4];

do

{

Console.Write("猜一個四位數: ");

yd = Console.ReadLine().ToCharArray();

++nGuess;

n = 0;

m = 0;

for(i=0; i<4; ++i)

{

for(j=0; j<4; ++j)

{

if( yd[i] != xd[j] ) continue;

++m;

if( i == j ) ++n;

break;

}

}

Console.WriteLine("{0}A{1}B", n, m);

if( n == 4 )

{

Console.WriteLine("恭喜!您猜對了");

break;

}

} while( nGuess < maxNGuess );

1 設定陣列d[0]~d[9]為’0’~’9’

2 for(i=0; i<4; ++i)

{

do

{

} while( d[k]已出現過 )

xd[i] = d[k]

}

GuessingNumber.Program片段 (1/2)

char[] d = { '0', '1', '2', '3', '4',

'5', '6', '7', '8', '9' };

bool again;

Random rand = new Random();

for (i = 0; i < 4; ++i){

do{

k = rand.Next() % 10;

again = false;

GuessingNumber.Program片段 (2/2)

for(j=0; j<i; ++j){

if (d[k] != xd[j]) continue;

again = true;

break;

}

} while( again );

xd[i] = d[k];

}

• 一維陣列
• 亂數產生器
• 猜數字遊戲
• 矩形二維陣列
• 井字遊戲Tic-tac-toe
• *不規則二維陣列
• *高維陣列
• foreach 敘述
Array2D.Program片段 (1/3)

const int N_STUDENTS = 3;

const int N_SUBJECTS = 2;

string[] registerNumber = new string[N_STUDENTS];

registerNumber[0] = "B645330";

registerNumber[1] = "B645331";

registerNumber[2] = "B645332";

string[] subject = new string[N_SUBJECTS];

subject[0] = "計算機概論";

subject[1] = "計算機程式設計";

int[,] score = new int[N_STUDENTS,N_SUBJECTS];

Array2D.Program片段 (2/3)

score[0, 0] = 90;

score[0, 1] = 84;

score[1, 0] = 88;

score[1, 1] = 86;

score[2, 0] = 86;

score[2, 1] = 92;

double[] individualAverage = new double[N_STUDENTS];

double[] subjectAverage = new double[N_SUBJECTS];

Array2D.Program片段 (3/3)

for (int i = 0; i < N_STUDENTS; i++){

int sum = 0;

for (int j = 0; j < N_SUBJECTS; j++){

sum += score[i,j];

}

individualAverage[i] = (double)sum /

N_SUBJECTS;

}

score[0,0]

score[0,0]

score[0,1]

score[0,1]

score[1,0]

score[1,1]

score[1,1]

score[1,0]

score[2,0]

score[2,0]

score[2,1]

score[2,1]

int[ , ] score = new int [3, 2];

score[0,0]

i = 0

score[0,1]

i = 1

score[1,1]

score[1,0]

i = 2

score[2,0]

score[2,1]

j = 0

j = 1

for(i=0; i<N_STUDENTS; ++i) {

for(j=0; j<N_SUBJECTS; ++j) {

. . .

}

}

score[0,0]

i = 0

score[0,1]

i = 1

score[1,1]

score[1,0]

i = 2

score[2,0]

score[2,1]

j = 0

j = 1

for(j=0; j<N_SUBJECTS; ++j) {

for(i=0; i<N_STUDENTS; ++i) {

. . .

}

}

j=0

j

i=0

i

data[i,j]

for(i=0; i<data.GetUpperBound(0); ++i) {

for(j=0; j<data.GetUpperBound(1); ++j) {

. . .

}

}

j

j=0

i=0

i

data[i,j]

for(j=0; j<data.GetUpperBound(1); ++j) {

for(i=0; i<data.GetUpperBound(0); ++i) {

. . .

}

}

Array2D2.Program片段 (1/2)

string[] registerNumber = {

"B645330", "B645331", "B645332" };

int[,] score = { {90, 84},

{88, 86},

{86, 92} };

int nStudents = score.GetUpperBound(0)+1;

int nSubjects = score.GetUpperBound(1)+1;

double[] individualAverage = new double[nStudents];

double[] subjectAverage = new double[nSubjects];

Array2D2.Program片段 (2/2)

Console.WriteLine(

"學號\\科目\t計算機概論\t計算機程式設計\t兩科平均成績");

for (int i = 0; i < nStudents; ++i){

Console.Write(registerNumber[i]+" \t");

for(int j=0; j<nSubjects; ++j)

{

Console.Write(score[i,j]+" \t\t");

}

Console.WriteLine("{0:F2}",

individualAverage[i]);

}

• 寫一程式，設值及印出3階魔方陣(河圖洛書)

• 一維陣列
• 亂數產生器
• 猜數字遊戲
• 矩形二維陣列
• 井字遊戲Tic-tac-toe
• *不規則二維陣列
• *高維陣列
• foreach 敘述

1

2

0

0

1

2

1

2

0

0

1

2

0

4

5

3

1

6

7

8

2

1 board[0,0]~board[2,2]設為' '(引號內是一個空白)

2 do

{

2.1 顯示棋盤

2.2 使用者輸入'x'位置

2.3 if( 使用者獲勝 || board已無空格) break

2.4 以亂數決定一個'o'位置

2.5 if( 電腦獲勝 ) break

} while( board仍有空格 )

3 輸出棋盤及訊息

1 設使用者或電腦最新位置在i0, j0

2 檢查是否成列

( board[i0, 0] == board[i0, 1] == board[i0, 2] )

3 檢查是否成行

( board[0, j0] == board[1, j0] == board[2, j0] )

4 檢查主對角線是否均為‘x’或 ‘o’

( board[0, 0] == board[1, 1] == board[2, 2] == ‘x’或 ‘o’ )

5 檢查次對角線是否均為‘x’或 ‘o’

( board[0, 2] == board[1, 1] == board[2, 0] == ‘x’或 ‘o’ )

1 do

{

1.1 產生一個0到8的亂數k

1.2 io = k % 3

1.3 jo = k / 3

} while( board[io, jo] 不是空白 )

1

2

0

0

1

2

0

4

5

3

1

6

7

8

2

char[,] board = { {' ', ' ', ' '},

{' ', 'o', ' '},

{' ', ' ', ' '} };

Console.WriteLine(" 0 1 2 ");

Console.WriteLine(" ");

Console.WriteLine("0 {0} | {1} | {2} ",

board[0, 0], board[0, 1], board[0, 2]);

Console.WriteLine(" ---+---+---");

Console.WriteLine("1 {0} | {1} | {2} ",

board[1, 0], board[1, 1], board[1, 2]);

Console.WriteLine(" ---+---+---");

Console.WriteLine("2 {0} | {1} | {2} ",

board[2, 0], board[2, 1], board[2, 2]);

Console.WriteLine(" ");

Console.Write(

"輸入x位置的座標, 以逗點分隔: ");

string[] input = new string[2];

input = Console.ReadLine().Split(',');

int ix = Convert.ToInt16(input[0]);

int jx = Convert.ToInt16(input[1]);

board[ix, jx] = 'x';

bool hasVacancies = true;

do

{

//此註解代表先前的兩段程式敘述

} while( hasVacancies );

userWin =

( (board[ix, 0] == board[ix, 1]) &&

(board[ix, 1] == board[ix, 2]) ) ||

( (board[0, jx] == board[1, jx]) &&

(board[1, jx] == board[2, jx]) ) ||

( (board[0, 0] == 'x') &&

(board[1, 1] == 'x') &&

(board[2, 2] == 'x') ) ||

( (board[0, 2] == 'x') &&

(board[1, 1] == 'x') &&

(board[2, 0] == 'x') );

• board[i0, 0] == board[i0, 1] == board[i0, 2]不可以直接翻譯成

board[ix, 0] == board[ix, 1] == board[ix, 2]

• 會被解讀為

( board[ix, 0] == board[ix, 1] ) ==

board[ix, 2]

hasVacancies = false;

for (int i = 0; i < 3; ++i)

{

for (int j = 0; j < 3; ++j)

{

if (board[i, j] == ' ')

{

hasVacancies = true;

break;

}

}

}

• 一維陣列
• 亂數產生器
• 猜數字遊戲
• 矩形二維陣列
• 井字遊戲Tic-tac-toe
• *不規則二維陣列
• *高維陣列
• foreach 敘述

/*

* 以下三角矩陣

*

* / x \

* | x x |

* | x x x |

* | x x x x |

* \ x x x x x /

*

* 示範不規則二維陣列之使用

*

* 9/29/2008

*/

int[][] a = new int [3][];

a[0][0]

a[1][1]

a[0]= new int[1];

a[1]= new int[2];

a[2]= new int[3];

a[2][0]

a[2][2]

Array2DA.Program片段 (1/2)

const int N_ROW = 5;

int[][] lowerTriangularMatrix = new int[N_ROW][];

for (int i = 0; i < N_ROW; ++i){

lowerTriangularMatrix[i] = new int[i + 1];

}

lowerTriangularMatrix[0][0] = 1;

lowerTriangularMatrix[1][0] = 2;

lowerTriangularMatrix[1][1] = 21;

Array2DA.Program片段 (2/2)

for (int i = 0; i < N_ROW; i++){

for (int j = 0; j <

lowerTriangularMatrix[i].Length; j++){

Console.Write(

lowerTriangularMatrix[i][j] +

"\t");

}

Console.WriteLine();

}

lowerTriangularMatrix[0]

[0][0]

[1][0]

lowerTriangularMatrix[1]

[1][1]

[2][0]

lowerTriangularMatrix[2]

[2][1]

[2][2]

[3][0]

lowerTriangularMatrix[3]

[3][1]

[3][2]

[3][3]

[4][0]

lowerTriangularMatrix[4]

[4][1]

[4][2]

[4][3]

[4][4]

• 宣告如下稀疏矩陣(sparse matrix),分別設定各列長度為3、2、4、3、1，再設各元素的值後輸出

• 一維陣列
• 亂數產生器
• 猜數字遊戲
• 矩形二維陣列
• 井字遊戲Tic-tac-toe
• *不規則二維陣列
• *高維陣列
• foreach 敘述

k

j

1

2

0

1

0

0

1

2

3

i

Array3D.Program片段

int[,,] a = { { {1, 2}, {-1, 1}, {0, 5} },

{ {2, 4}, {1, 3}, {3, 7} },

{ {4, 0}, {8, -3}, {9, 6} },

{ {-2, 3}, {5, -2}, {1, 1} } };

for (int i=0;i<a.GetUpperBound(0)+1;++i){

for (int j=0;j<a.GetUpperBound(1)+1;++j){

for (int k=0;k<a.GetUpperBound(2)+1;++k){

Console.Write("a[{0}, {1}, {2}] = {3} \t",

i, j, k, a[i, j, k]);

}

Console.WriteLine();

}

}

• 一維陣列
• 矩形二維陣列
• 不規則二維陣列
• *高維陣列
• *foreach 敘述
UsingForEach.Program 片段

string[,] strArray = {

{"Demonstrating", "the", "use", "of",

"foreach"},

{"\nGood", "for", "high", "dimensional", "array"}

};

foreach (string str in strArray)

{

Console.Write(str+" ");

}

Console.WriteLine();

for (int i = 0; i<strArray.GetUpperBound(0)+1; ++i)

{

for (int j = 0; j<strArray.GetUpperBound(1)+1; ++j) {

Console.Write(strArray[i, j] + " ");

}

}