Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to the C Programming Language

Similar presentations


Presentation on theme: "Introduction to the C Programming Language"— Presentation transcript:

1 Introduction to the C Programming Language
陣列 (Array) 複習

2 陣 列 (Array) 有限個相同資料型態之元素所組成之集合,以一個名稱來代表. 佔有一大塊連續之記憶體空間
存取陣列資料值時,以陣列的索引值(index)指示所存取的資料. []為陣列修飾符號,每一組方括號表示一維(dimension). 在C語言中,第一個元素(element)的索引值一定是0. C語言不做界限的檢查,所以在儲存陣列元素時,即使超過陣列宣告長度,編譯時不會有錯,執行時則會有錯. 通常陣列資料的輸入需藉由for迴圈來處理.

3 一維陣列(1D Array) 宣告格式 : 資料型態 陣列名稱[陣列大小]; 例如: int score[5]; score[1]=23;
資料型態 陣列名稱[陣列大小]; 例如: int score[5]; score[1]=23; m score[0] m score[1] m score[2] m score[3] m score[4] 記憶體位址 記憶體 陣列名稱 Data Type size char 1Byte short 2Bytes int 4Bytes long float double 8Bytes 陣列第一個元素之位址 23 註一: 長度為無號整數(1~65535) 註二: m 代表第一個元素的位址. 註三: 因為是整數,所以每個元素佔2個位元組; 如果是浮點數,則佔4個位元組. 註四: 陣列的名稱就代表該陣列在記憶體內開頭的位址. 例如: int aa[10]; printf(“The start address of this array is %d”,aa)

4 一維陣列(1D Array) --- 初值設定 設定陣列初值的格式 : 資料型態 陣列名稱[陣列大小]={xx,xx,…,xx};
例如: int num[5]={3,6,7,5,9}; (或int num[]={3,6,7,5,9}; ) num[0] num[1] num[2] num[3] num[4] 記憶體 3 6 7 5 9 註一: 長度部分, 可有可無(因為C編譯程式在設定陣列的初值時, 會自行計算有多少個資料元素,然後配予足夠空間給它). 註二: 必須以一對大括號將所要設定之陣列元素值圍住, 陣列元素值間 以逗號相隔. 參考來源 : Borland C++入門與應用徹底剖析(P.203) C入門與Turbo系統 (P.8-7)

5 Example 輸入學生人數及成績, 並列印出全班平均. void main( ) {
int score[10], i, sum=0, num; float ave; printf(“Please input number of student : “); scanf(“%d”,&num); for ( i = 0; i < num; i++ ) printf(“Input score here : “); scanf(“%d”,&score[i]); sum += score[i]; } ave = (float) sum / (float) num; printf(“The average is %6.2f \n”,ave); 檔名 : 1darray4.c 執行結果: Please input number of student : 5 Input score here : 87 Input score here : 55 Input score here : 64 Input score here : 72 Input score here : 90 The average is 73.60 註一 : C語言不做界限的檢查,所以在儲存陣列元素時,即使超過陣列宣告長度, 在程式編譯時不會有錯誤訊息產生,但在執行時則會有錯. 參考來源 : Borland C++入門與應用徹底剖析(P.200)

6 陣列之N維陣列

7 二維陣列(2D Array) 宣告格式 : 資料型態 陣列名稱[列數][行數];
資料型態 陣列名稱[列數][行數]; 例如: int num[2][2]; /* 宣告一個 2x2 的陣列*/ col col 2 row m num[0][0] row m num[0][1] m num[1][0] m num[1][1] 記憶體 num[0][0] num[0][1] num[1][0] num[1][1] 註一: m 代表第一個元素的位址. 註二: 因為是整數,所以每個元素佔2個位元組; 如果是浮點數,則佔4個位元組. 註三: 元素個數 = 2*2 = 4 個 註四: 二維陣列的輸入與輸出均需藉由兩層的for迴圈來處理. 參考來源 : Borland C++入門與應用徹底剖析(P.206) C入門與Turbo系統 (P.8-14)

8 二維陣列(2D Array) 範例一: 輸入一個3*4的陣列, 並將其印出來. void main( ) { int x[3][4];
int i,j; printf(“Please input 2 dimension array(3*4).\n”); for ( i = 0; i < 3; i++ ) for ( j = 0; j < 4; j++ ) scanf(“%d”,&x[i][j]); printf(“The result 2 dimension array is as following : \n”); printf(“%3d”,x[i][j]); printf(“\n”); } 檔名 : 2darray1.c 執行結果: Please input 2 dimension array(3*4). The result 2 dimension array is as following : 參考來源 : C入門與Turbo系統 (P.8-15)

9 二維陣列(2D Array) 範例三: 輸入兩個陣列, 將乘法結果存入第三個陣列內.
範例三: 輸入兩個陣列, 將乘法結果存入第三個陣列內. #include <stdio.h>/* 引入標頭檔 */ #include <stdlib.h> int main(void) { int a[3][2]={{5,4},{7,8},{7,8}}, /* 二個二維整數陣列的宣告並設定初值 */ b[2][4]={{2,3,5,1},{1,2,3,3}}, c[3][4], /* 儲存相乘之結果陣列 */ i,j,k; /* 計數用之變數 */ for(i=0;i<3;i++) /* 設定 c 陣列之初值為 0 */ for(j=0;j<4;j++) c[i][j]=0; for(i=0;i<3;i++) for(k=0;k<2;k++) c[i][j] += ( a[i][k]*b[k][j] ); /* a 與 b 的陣列元素相乘並加總 */ } printf("c[%d][%d]=%-2d ",i,j,c[i][j]); /* 陣列元素的顯示 */ printf("\n"); system("PAUSE");/* 暫停程式執行 */ return 0;/* <-程式正常結束,傳回0 */ 檔名 : 2darr_mul.c 執行結果: c[0][0]=14 c[0][1]=23 c[0][2]=37 c[0][3]=17 c[1][0]=22 c[1][1]=37 c[1][2]=59 c[1][3]=31 c[2][0]=22 c[2][1]=37 c[2][2]=59 c[2][3]=31 Press any key to continue . . .

10 多維陣列(Multi-dimensional Array)
宣告格式 : 資料型態 陣列名稱[平面數][列數][行數]; 例如: int x[2][3][4]; x[1][0][0] x[1][0][1] x[1][0][2] x[1][0][3] x[1][1][0] x[1][1][1] x[1][1][2] x[1][1] [3] x[1][2][0] x[1][2][1] x[1][2][2] x[1][2] [3] 註一: 整數,所以共佔2*3*4*2=48個位元組. 註二: 元素個數 = 2*3*4 = 48 個 註三: 三維陣列的輸入與輸出均需藉由三層的for迴圈來處理. 參考來源 : C入門與Turbo系統 (P.8-17) x[0][0][0] x[0][0][1] x[0][0][2] x[0][0][3] x[0][1][0] x[0][1][1] x[0][1][2] x[0][1] [3] x[0][2][0] x[0][2][1] x[0][2][2] x[0][2] [3]

11 多維陣列(Multi-dimensional Array) --- 初值設定
例如: int x[2][3][4] = {{{0,0,0,0},{0,0,0,0},{0,0,0,0}}, {{0,0,0,0},{0,0,0,0},{0,0,0,0}}}; 對初學者而言,應用到多維陣列的處理是微乎其微.

12 二維字串陣列

13 二維陣列(2D Array)-字串陣列 /* prog 9-22,字串陣列 */ #include <stdio.h>
int main(void) { char name[3][10]={"David","Jane Wang","Tom Lee"}; int i; for(i=0;i<3;i++) /* 印出字串陣列內容 */ printf("name[%d]=%s\n",i,name[i]); printf("\n"); for(i=0;i<3;i++) /* 印出字串陣列元素的位址 */ printf("address of name[%d]=%p\n",i,&name[i]); printf("address of name[%d][0]=%p\n\n",i,&name[i][0]); } return 0; 2darray_4.c name[0]=David name[1]=Jane Wang name[2]=Tom Lee Address of name[0]=0253FDB8 Address of name[0][0]=0253FDB8 Address of name[1]=0253FDC2 Address of name[1][0]=0253FDC2 Address of name[2]=0253FDCC Address of name[2][0]=0253FDCC

14 字串陣列說明 Name[0] 0253FDB8 Name[1] 0253FDC2 Name[2] 0253FDCC D a v i d \0
J n e W g T o m L name[I]的位址其實就是name[i][0]的位址 用法類似一維陣列


Download ppt "Introduction to the C Programming Language"

Similar presentations


Ads by Google