1 / 17

第八章 陣列

第八章 陣列. 本章學習重點. 8-1 陣列的基本架構 8-2 一維陣列表示法 8-3 多維陣列表示法 8-4 以陣列為參數之函數呼叫 8-5 字元陣列表示法. 8-1 陣列的基本架構. 陣列 (array) : 由一組相同的資料型態所組成 記憶體位置具有連續性的特性 使用共同的名稱來存取 依據結構,可將陣列分為: 一維陣列 二維陣列 多維陣列. 8-2 一維陣列表示法. 語法 資料型態 陣列名稱 [ 陣列大小 ]; 說明 資料型態:陣列元素的資料型態 陣列名稱:根據識別字規定命名 [ ] :一個無正負符號的整數 範例

hop-wells
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. 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. 本章學習重點 8-1 陣列的基本架構 8-2 一維陣列表示法 8-3 多維陣列表示法 8-4 以陣列為參數之函數呼叫 8-5 字元陣列表示法

  3. 8-1 陣列的基本架構 • 陣列(array) :由一組相同的資料型態所組成 • 記憶體位置具有連續性的特性 • 使用共同的名稱來存取 • 依據結構,可將陣列分為: • 一維陣列 • 二維陣列 • 多維陣列

  4. 8-2 一維陣列表示法 • 語法 • 資料型態 陣列名稱[陣列大小]; • 說明 • 資料型態:陣列元素的資料型態 • 陣列名稱:根據識別字規定命名 • [ ]:一個無正負符號的整數 • 範例 • int a[5] ; • int b[5] = {2, 4, 3, 2, 7}; • int c[ ] = {1, 2, 3, 4, 5};

  5. 8-2-2 一維陣列元素的引用 • 語法 • 陣列名稱[索引值] • 說明 • 陣列名稱:陣列起始位置的位址 • 索引值:陣列起始位置的位移 • 範例:int a[5] ; /*假設a陣列的起始位址為100*/

  6. 範例 Ch8_2 (1/2) ch8_2 列出陣列的儲存方式 1 #include<stdio.h> 2 void main( ) 3 { 4 int a[5] = {2, 4, 3, 6, 8}, i; • for(i = 0 ; i < 5 ; i++) • printf(“索引位址:a[%d], 陣列的內容:%d, 實際記憶體位址: %d\n", i, a[i], &a[i]); /*&a[i]為求出實際位址的表示方式*/ 7 }

  7. Ch8_2 輸出結果 (2/2) • 程式執行結果 • 索引位址:a[0], 陣列的內容:2, 實際記憶體位址:8724 • 索引位址:a[1], 陣列的內容:4, 實際記憶體位址:8726 • 索引位址:a[2], 陣列的內容:3, 實際記憶體位址:8728 • 索引位址:a[3], 陣列的內容:6, 實際記憶體位址:8730 • 索引位址:a[4], 陣列的內容:8, 實際記憶體位址:8732

  8. 8-2-3 陣列界限檢查 • C語言不會自動檢查界限 • 若迴路的範圍超過其陣列的範圍,會造成不可預期的錯誤、當機等可能。 • 若範圍設定太小,則會發生資料遺失的問題。

  9. 8-3 多維陣列表示法 • 二維陣列 • 最常用的多維陣列 • 擁有行、列的資料 • 記憶體中的儲存方式以線性的方式來儲存 • 語法 • 資料型態 陣列名稱[陣列大小] [陣列大小]; 

  10. 第一列 Column(1) a[0][0] Column(2) Column(3) a[0][1] Row(1) a[0][0] a[0][1] a[0][2] a[0][2] Row(2) a[1][0] a[1][1] a[1][2] 第二列 a[1][0] Row(3) a[2][0] a[2][1] a[2][2] a[1][1] a[1][2] 第三列 a[2][0] a[2][1] a[2][2] 8-3 多維陣列 實際記憶體中的儲存方式 範列: int a[3][3];

  11. 範例 Ch8_5 (1/3) ch8_5 基本二維陣列的加法運算 1 #include<stdio.h> 2 void main( ) 3 { • int a[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; • int b[3][4], c[3][4], i, j; 6 printf("The origional matrix is :\n"); 7 for(i = 0; i < 3; i++) 8 { • for(j = 0; j < 4; j++) 10 printf("%3d",a[i][j]); 11 printf("\n"); 12 } 13 printf("Please Input the second 3*4 matrix.\n");

  12. 範例 Ch8_5 (2/3) 14 for ( i = 0; i < 3; i++ ) 15 for ( j = 0; j < 4; j++ ) 16 scanf("%d", &b[i][j]); 17 for ( i = 0; i < 3; i++ ) 18 for ( j = 0; j < 4; j++ ) 19 c[i][j] = a[i][j] + b[i][j]; 20 printf("After addition :\n"); 21 for(i = 0; i < 3;i++) 22 { 23 for(j = 0; j < 4; j++) 24 printf("%3d",c[i][j]); 25 printf("\n"); 26 } 27 }

  13. Ch8_5 輸出結果 (3/3) • The original matrix is : • 1 2 3 4 • 5 6 7 8 • 9 10 11 12 • Please Input the second 3*4 matrix. • 1 2 3 4 • 5 6 7 8 • 9 1 2 3 • After addition : • 2 4 6 8 • 10 12 14 16 • 18 11 13 15 • 程式執行結果

  14. 8-4 以陣列為參數之函數呼叫 • 語法 int function(int array[ ]); /*副程式宣告*/ void main(void) /*主程式*/ { int a[ ]; … function(a); … } int function(int array[ ]) /*副程式開始*/ { … }

  15. 範例 Ch8_8(1/2) ch8_8 以陣列做為參數,輸出陣列的內容及位址 1 #include<stdio.h> 2 void print(int array[ ]); 3 void main( ) • { • int a[8] = {1, 2, 3, 4, 5, 6, 7, 8}; 6 print(a); /*呼叫副程式print( )且將a陣列傳入*/ • } 8 void print(int array[ ]) 9 { 10 int i; 11 for(i = 0; i < 8; i++) 12 printf("array[%d] = %d, 陣列位址:%d\n", i, array[i], &array[i]); 13 }

  16. ch8_8輸出結果 (2/2) • 程式執行結果 • array[0] = 1, 陣列位址:8700 • array[1] = 2, 陣列位址:8702 • array[2] = 3, 陣列位址:8704 • array[3] = 4, 陣列位址:8706 • array[4] = 5, 陣列位址:8708 • array[5] = 6, 陣列位址:8710 • array[6] = 7, 陣列位址:8712 • array[7] = 8, 陣列位址:8714

  17. 8-5 字元陣列表示法 • 字元陣列 • 所有的資料型態都是以char(字元)組成 • 範例 • char a[ ] = {‘s’,’t’,’r’,’i’,’n’,’g’};

More Related