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語言中,第一個元素的索引值一定是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] 記憶體位址 記憶體 陣列名稱 陣列第一個元素之位址 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 一維陣列(1D Array) 範例一: 輸入並輸出4個數字. #include <stdio.h>
#include <stdlib.h> void main( ) { int x[4]; int i; for ( i = 0; i < 4; i++ ) printf(“x[%d] = ”,i); scanf(“%d”,&x[i]); } printf(“\nThe output is as following: \n”); printf(“x[%d] is %d\n”,i,x[i]); 檔名 : 1darray1.c 執行結果: x[0] = 15 x[1] = 36 x[2] = 9 x[3] = 28 The output is as following: x[0] is 15 x[1] is 36 x[2] is 9 x[3] is 28 C 編譯程式時, 並不會檢查存取陣列時的編號, 因此, 若使用負的, 或大於陣列大小的編號時, 編譯器並不會 檢查出來. 唯有執行結果可能有誤 存取語法 array[0] array[1] array[2] array[3] 15 10 2 5 陣列元素

6 一維陣列(1D Array) 範例二: 將使用者所輸入的字串,反轉輸出. void main( ) { char y[30];
int i,s,count=0; printf(“Type a sentence : ”); while (( s = getchar( )) != ‘\n’ ) y[count++] = s; printf(“\n Reverse the sentence is as following : \n”); for ( i = count-1; i >= 0; i-- ) putchar(y[i]); } 檔名 : 1darray2.c 執行結果: Type a sentence : We are family !! Reserve the sentence is as following : !! ylimaf era eW 參考來源 : 實用C程式語言-入門篇 (P.7-8)

7 一維陣列(1D Array) 範例三: 輸入一個正整數,將它轉換為二進位數. void main( ) {
int b[20], n,k,i; clrscr( ); /* 清除螢幕 */ printf(“Keyin an integer : ”); scanf(“%d”,&k); for ( n = 0; k > 0; n++ ) b[n] = k % 2; k = k / 2; } printf(“The binary code is ”); for ( i = n-1; i >= 0; i-- ) printf(“%d”,b[i]); printf(“\n”); 檔名 : 1darray3.c 執行結果: Keyin an integer : 13 The binary code is 1101 參考來源 : C語言入門 (P.7-21)

8 一維陣列(1D Array) 範例四: 輸入學生人數及成績, 並列印出全班平均. 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)

9 一維陣列(1D Array) --- 初值設定 範例一: 將數字輸出. void main( ) {
int x[]={15,36,9,28}; int i; printf(“\nThe output is as following: \n”); for ( i = 0; i < 4; i++ ) printf(“x[%d] is %d\n”,i,x[i]); } 檔名 : 1darray5.c 執行結果: The output is as following: x[0] is 15 x[1] is 36 x[2] is 9 x[3] is 28

10 字元陣列 宣告格式 : char 陣列的名稱 [陣列的大小] ; 或 char 陣列的名稱 [列陣列的大小][行陣列大小] ;
For example: char array1 [ 10 ] ; char array2 [ 5 ][ 25 ] ; 例一 : int string[6]={'A','B','C','D','E','\0'};  或 int string[5]="ABCDE";  例二 : int string1[2][6]={{'A','B','C','D','E','\0'}, {'F','G','H','I','J','\0'};  或 int string1[2][5]={ "ABCDE","FGHIJ" }; 如 果 字 元 陣 列 以 字 元 的 方 式 來 存 取 陣 列 , 則 須 加 結 束 字 元 \0 於 陣 列 的 最 末 端 , 表 示 此 字 元 陣 列 的 結 束 如 果 字 元 陣 列 以 字 串 的 方 式 來 存 取 陣 列 , 則 C 編 譯 器 會 自 動 在 字 串 最 末 端 加 上 結 束 字 元 \0 表 示 此 字 元 陣 列 的 結 束 , 所 以 我 們 不 需 自 行 加 入

11 字元陣列 – 範例一 /*列印出字元及字串之長度*/ #include <stdio.h> int main(void) {
char a[]="My friend"; char b='c'; char str[]="c"; printf("sizeof(a)=%d\n",sizeof(a)); printf("sizeof(b)=%d\n",sizeof(b)); printf("sizeof(str)=%d\n",sizeof(str)); return 0; } str_arr_1.c sizeof(a) =10 sizeof(b)=1 sizeof(str)=2 字串 a 的內容為 My friend, 包括空白共 9 個字元, 但結果為 10, 這是因為字串結尾之結束字元為 \0 字元變數 b 與字串變數 str內容皆為 c, 但長度不同, 因字串 str 以雙引號設定, 結束時自動加上 \0, 因此 str 長度為 2 個位元組, 字元變數以單引號設定, 並不會自動加上字串結束字元 \0

12 字元陣列 – 範例二 /* 輸入及印出字串, 使用 printf(), scanf() */
#include <stdio.h> int main(void) { char name[15]; int i; for(i=0;i<2;i++) printf("What's your name?"); scanf("%s",name); printf("Hi! %s,How are you?\n\n",name); } return 0; str_arr_2.c What’s your name? David Hi! David, How are you? What’s your name? Tom Lee Hi! Tom, How are you? 第二次輸入姓名 Tom Lee, 缺只輸出 Tom?? 因利用 %s 輸入字串, 當scanf()獨到 Enter 鍵或第一個空白時, 就認為字串已輸入完畢, 即結束讀取動作 因此, 以 scanf() 輸入字串時, 不可包含空白

13 字元陣列 – 範例三 /* prog 9-21,輸入及印出字串 */ #include <stdio.h>
int main(void) { char name[15]; puts("What's your name?"); gets(name); puts("Hi!"); puts(name); puts("How are you?"); return 0; } str_arr_3.c What’s your name? David Young Hi! How are you? 使用gets()或 puts(), 都會於讀取或輸出字串後換行

14 二維陣列(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)

15 二維陣列(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)

16 二維陣列(2D Array) 範例二: 輸入兩個3*3的陣列, 將加算結果存入第三個陣列內. void main( ) {
int num1[3][3],num2[3][3],num3[3][3]; int i,j; printf(“Please input first 2 dimension array.\n”); for ( i = 0; i < 3; i++ ) for ( j = 0; j < 3; j++ ) scanf(“%d”,&num1[i][j]); printf(“Please input second 2 dimension array.\n”); scanf(“%d”,&num2[i][j]); num3[i][j]= num1[i][j] + num2[i][j]; printf(“The result 2 dimension array is as following : \n”); printf(“%3d %3d %3d\n”,num3[i][0],num3[i][1],num3[i][2]); } 檔名 : 2darray2.c 執行結果: Please input first 2 dimension array. 3 4 5 2 3 4 1 2 3 Please input second 2 dimension array. 2 2 2 3 3 3 4 4 4 The result 2 dimension array is as following : 參考來源 : Borland C++入門與應用徹底剖析(P.208)

17 二維陣列(2D Array) 範例三: 輸入兩個3*3的陣列, 將乘法結果存入第三個陣列內.
範例三: 輸入兩個3*3的陣列, 將乘法結果存入第三個陣列內. #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 . . .

18 二維陣列(2D Array) --- 初值設定 範例四: 設計一個中文字“洪”. void main( ) {
int x[9][16]={ { 1,1,0,0,0,0,0,1,1,0,0,0,1,1,0,0 }, { 0,1,1,0,0,0,0,1,1,0,0,0,1,1,0,0 }, { 0,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1 }, { 0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0 }, { 1,1,1,1,0,0,0,1,1,0,0,0,1,1,0,0 }, { 0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1 }, { 0,0,1,1,0,0,0,0,1,1,0,0,1,1,0,0 }, { 0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0 }, { 1,1,0,0,0,0,1,1,0,0,0,0,0,0,1,1 } }; int i,j; for ( i = 0; i < 9; i++ ) for ( j = 0; j < 16; j++ ) if (x[i][j] == 1) printf(“%c”,219); else printf(“ ”); printf(“\n”); } 檔名 : 2darray3.c 執行結果: 註一: 十進位的219的繪圖字元為 參考來源 : Borland C++入門與應用徹底剖析(P.211及P.983)

19 二維陣列(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

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

21 多維陣列(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]

22 多維陣列(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}}}; 對初學者而言,應用到多維陣列的處理是微乎其微.

23 作業一 某家公司有五位業務人員,於2001年一至六月個人月績如下表:請寫一程式計算每個月這家公司的總營業額各為多少? 並計算每位業務人員一至六月的個人總營業額及這家公司六個月的全部營業額為多少? 2001 年 一至六月 (單位 : 百萬元 ) Sales A Sales B Sales C Sales D Sales E Month 1 120 93 135 99 87 Month 2 102 115 180 154 118 Month 3 114 125 91 127 70 Month 4 108 150 153 66 123 Month 5 79 103 105 98 Month 6 97 110 112 101 檔案名稱 : hw1.c #include<stdio.h> void main() { int i,j; int sales[5][5]={{120,93,135,99},{102,115,180,154}, {114,125,91,127},{108,150,153,66}}; clrscr(); printf("\n"); printf(" month \n"); printf(" Sales A Sales B Sales C Sales D Total \n"); printf(" \n"); for(i=0;i<4;i++) for(j=0;j<4;j++) sales[i][4]=sales[i][4]+sales[i][j]; sales[4][j]=sales[4][j]+sales[i][j]; } sales[4][4]=sales[4][4]+sales[i][4]; for(i=0;i<5;i++) if(i>=4) printf("Total=>"); else printf("Month %d",i+1); for(j=0;j<5;j++) printf("%10d",sales[i][j]); }


Download ppt "Introduction to the C Programming Language"

Similar presentations


Ads by Google