Introduction to the C Programming Language

Slides:



Advertisements
Similar presentations
第 8 章 数组 计算机科学学院 李淮 Tel QQ
Advertisements

資料坐火車 …… 談陣列 (Array) 綠園 2008/12/15. Array 的宣告 整數陣列的宣告  int student[5]; 意義:宣告了 5 個 int 大小的連續空間,名稱 為 student ,沒有預設值,則為系統殘值。 student student[0] student[1]
綠園 2012/11/06. Array 的宣告 整數陣列的宣告 int student[5]; 意義:宣告了 5 個 int 大小的連續空間,名稱 為 student ,沒有預設值,則為系統殘值。 student student[0] student[1] student[4] student[2]
綠園 2012/11/06. Array 的宣告 整數陣列的宣告 int student[5]; 意義:宣告了 5 個 int 大小的連續空間,名稱 為 student ,沒有預設值,則為系統殘值。 student student[0] student[1] student[4] student[2]
CSIM, PU C Language Introduction to the C Programming Language 重覆敘述 (for,while,break,continue) 適合重複性的計算或判斷.
第一單元 建立java 程式.
Loops.
C/C++基礎程式設計班 陣列 (Array)
輸出與輸入(I/O).
File Access 井民全製作.
第十一章 結構.
循环结构又称为重复结构:用来处理需要重复处理的问题,它是程序中一种很重要的结构。
Visual C++ introduction
Introduction to the C Programming Language
String C語言-字串.
Introduction to the C Programming Language
Introduction to the C Programming Language
STRUCTURE 授課:ANT 日期:2010/5/12.
计算概论 第十八讲 C语言高级编程 结构与习题课 北京大学信息学院.
C語言簡介 日期 : 2018/12/2.
Introduction to the C Programming Language
Introduction to the C Programming Language
第四章 C 语言中的输入和输出.
Introduction to the C Programming Language
11/25計程實習課 Speaker: Wen-Ching Lo.
Java 程式設計 講師:FrankLin.
第13章 结构体的应用 13.1 了解由用户构造的数据类型 13.2 结构体类型说明及结构体变量 13.3 结构体数组
計數式重複敘述 for 迴圈 P
Introduction to the C Programming Language
第七章 函数及变量存贮类型 7.1 函数基础与C程序结构 7.2 函数的定义和声明 7.3 函数的调用 7.4 函数的嵌套与递归
第一單元 建立java 程式.
数组 梁春燕 华电信息管理教研室.
陣列(Array).
陣列
C语言概述 第一章.
輸入&輸出 函數 P20~P21.
第九章 字串.
C语言大学实用教程 第6章 数组 西南财经大学经济信息工程学院 刘家芬
Introduction to the C Programming Language
程式設計 博碩文化出版發行.
Introduction to the C Programming Language
挑戰C++程式語言 ──第8章 進一步談字元與字串
Pointer 指標 授課老師:蕭志明.
認識常數與變數 學習C++所提供的各種基本資料型態 瞭解溢位的發生 學習認識資料型態之間的轉換
7.1 C程序的结构 7.2 作用域和作用域规则 7.3 存储属性和生存期 7.4 变量的初始化
輸出與輸入(I/O).
C qsort.
Introduction to the C Programming Language
挑戰C++程式語言 ──第7章 輸入與輸出.
第三章 基本的輸出與輸入函數 (Basic Output & Input Function)
陣列與結構.
第四章 C 语言中的输入和输出.
選擇性結構 if-else… switch-case 重複性結構 while… do-while… for…
Introduction to the C Programming Language
第七章  数 组.
第四章 陣列、指標與參考 4-1 物件陣列 4-2 使用物件指標 4-3 this指標 4-4 new 與 delete
Introduction to the C Programming Language
Introduction to the C Programming Language
Programming & Language Telling the computer what to do
Introduction to the C Programming Language
ABAP Basic Concept (2) 運算子 控制式與迴圈 Subroutines Event Block
第一次上機考參考答案 僅供參考,同學可自行再想更好的方法..
String類別 在C語言中提供兩種支援字串的方式 可以使用傳統以null結尾的字元陣列 使用string類別
Array(陣列) Anny
C語言程式設計 老師:謝孟諺 助教:楊斯竣.
Introduction to the C Programming Language
Introduction to the C Programming Language
C语言基础学习 从外行到入门.
ABAP Basic Concept (2) 運算子 控制式與迴圈 Subroutines Event Block
InputStreamReader Console Scanner
Presentation transcript:

Introduction to the C Programming Language 陣列 (Array)

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

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

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

一維陣列(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 陣列元素

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

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

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

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

字元陣列 宣告格式 : 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 表 示 此 字 元 陣 列 的 結 束 , 所 以 我 們 不 需 自 行 加 入

字元陣列 – 範例一 /*列印出字元及字串之長度*/ #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

字元陣列 – 範例二 /* 輸入及印出字串, 使用 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() 輸入字串時, 不可包含空白

字元陣列 – 範例三 /* 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(), 都會於讀取或輸出字串後換行

二維陣列(2D Array) 宣告格式 : 資料型態 陣列名稱[列數][行數]; 資料型態 陣列名稱[列數][行數]; 例如: int num[2][2]; /* 宣告一個 2x2 的陣列*/ col 1 col 2 row 1 m num[0][0] row 2 m+2 num[0][1] m+4 num[1][0] m+6 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)

二維陣列(2D Array) 範例一: 輸入一個3*4的陣列, 並將其印出來. int 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"); } system("pause"); return 0; 檔名 : 2darray1.c 執行結果: Please input 2 dimension array(3*4). 3 4 5 6 2 3 4 5 1 2 3 4 The result 2 dimension array is as following : 3 4 5 6 2 3 4 5 1 2 3 4 參考來源 : C入門與Turbo系統 (P.8-15)

二維陣列(2D Array) 範例二: 輸入兩個3*3的陣列, 將加算結果存入第三個陣列內. int 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]); system("pause"); return 0; } 檔名 : 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 : 5 6 7 參考來源 : Borland C++入門與應用徹底剖析(P.208)

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

二維陣列(2D Array) --- 初值設定 範例四: 設計一個中文字“洪”. int 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"); } system("pause"); return 0; 檔名 : 2darray3.c 執行結果: 洪 註一: 十進位的219的繪圖字元為 參考來源 : Borland C++入門與應用徹底剖析(P.211及P.983)

二維陣列(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]); } system(“pause”); 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

字串陣列說明 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]的位址 用法類似一維陣列

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

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

練習一 某家公司有五位業務人員,於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(" 2001 month 1-4 \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]); }

傳遞二維陣列與多維陣列 傳回值型態 函數名稱(資料型態 陣列名稱[列的個數,可不填][行的個數]); /*宣告副程式*/ 傳回值型態 函數名稱(資料型態 陣列名稱[列的個數,可不填][行的個數]); /*宣告副程式*/ int main(void) { 資料型態 陣列名稱[列的個數][行的個數]; /*宣告陣列*/ … 函數名稱(陣列名稱); /*主程式呼叫副程式*/ } 傳回值型態 函數名稱(資料型態 陣列名稱[列的個數,可不填][行的個數]) /*副程式定義*/ … }