Download presentation
Presentation is loading. Please wait.
1
授課教師 Wanjiun Liao email wjliao@ntu.edu.tw
Array I 授課教師 Wanjiun Liao 【本著作除另有註明外,採取創用CC「姓名標示-非商業性-相同方式分享」台灣3.0版授權釋出】 本課程指定教材為 c++ how to program 8th.edition, Paul Deitel,Harvey Deitel,2012,Prentice Hall 本作品轉載自Microsoft Office 2007多媒體藝廊,依據Microsoft服務合約及著作權法第46、52、65條合理使用。
2
What, Why, and How? int a[6]; a[0], a[1], a[2], a[3], a[4], and a[5]
for (int i=0; i<6;i++) a[i]=i*2; //Fig. 7.3 for (int i=0; i<6;i++) cout <<a[i]<<endl; int a[3], b[5], c[13]; int a[5] = {1,2,3,4,5}; Q1: int a[5] = {1,2,3}; Q2: int a[5] = {1,2,3,4,5,6}; Q3: int a[] = {1,2,3,4,5};
3
Illustration of a 12-Element Array
1543 72 6 -45 -89 62 -3 1 6543 78 C[0] C[1] C[2] C[3] C[4] C[5] C[6] C[7] C[8] C[9] C[10] C[11] Name of the array is c value Name of an individual array element Position number of the element within the array c
4
Data Array vs. Character Array
String: char x[]=“happy”; char x[6] ={‘h’, ‘a’, ‘p’, ‘p’, ‘y’, ‘\0’}; cin >> x; cout << x; Note: const int N =9; //Fig. 7.5, Fig. 7.6 int a[N]={0}; char b[N]=“hello”; Static array vs. automatic array //Fig. 7.13
5
Some More Examples 本頁內容請參考C++ HOW TO PROGRAM/Paul Deitel Harvey Deitel/2011/P.275~276
6
Some More Examples 本頁內容請參考C++ HOW TO PROGRAM/Paul Deitel Harvey Deitel/2011/P.278
7
Some More Examples 本頁內容請參考C++ HOW TO PROGRAM/Paul Deitel Harvey Deitel/2011/P.278
8
Some More Examples
9
Some More Examples Rating Frequency 1 2 2 2 3 2 4 2 5 5 6 11 7 5 8 7
8 7
10
Some More Examples
11
Some More Examples Enter the string "hello there": hello there
string1 is: hello string2 is: string literal string1 with spaces between characters is: h e l l o string1 is: there
12
Multi-Dimensional Array
n-dim array: int a[3][4]; for (int i=0; i<3;i++) for (int j=0; j<4;j++) a[i][j]=0; int a [2][3]={{1,2,3},{4,5,6}}; int a [2][3]={1,2,3,4,5,6}; Q1: int a[2][3] = {1,2,3,4,5}; Q2: int a[2][3] = {{1},{2,3}};
13
Two Dim Array a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 2 ]
Column 3 Column 1 Column 2 Column 0 Row 0 Row 1 Row 2 Array name Column subscript Row subscript
14
Passing Arrays to Functions
本頁內容請參考C++ HOW TO PROGRAM/Paul Deitel Harvey Deitel/2011/P.284~285
15
Passing Arrays to Functions
本頁內容請參考C++ HOW TO PROGRAM/Paul Deitel Harvey Deitel/2011/P.284~285
16
Passing Arrays to Functions
本頁內容請參考C++ HOW TO PROGRAM/Paul Deitel Harvey Deitel/2011/P.284~285
17
Passing Arrays to Functions
本頁內容請參考C++ HOW TO PROGRAM/Paul Deitel Harvey Deitel/2011/P.286
18
Linear Search 本頁內容請參考C++ HOW TO PROGRAM/Paul Deitel Harvey Deitel/2011/P.293~294
19
Linear Search (cont.) 本頁內容請參考C++ HOW TO PROGRAM/Paul Deitel Harvey Deitel/2011/P.293~294
20
Sorting 本頁內容請參考C++ HOW TO PROGRAM/Paul Deitel Harvey Deitel/2011/P.295~296
21
Sorting (cont.) 本頁內容請參考C++ HOW TO PROGRAM/Paul Deitel Harvey Deitel/2011/P.295~296
22
版權聲明 頁碼 作品 版權圖示 來源/作者 1-23 本作品轉載自Microsoft Office 2007多媒體藝廊,依據Microsoft服務合約及著作權法第46、52、65條合理使用。 3 C++ HOW TO PROGRAM 8/e 2011 Paul Deitel, Harvey Deitel p.269 依據著作權法第46、52、65條合理使用 8-9 台灣大學 電機系 廖婉君 教授
23
版權聲明 頁碼 作品 版權圖示 來源/作者 10-11 台灣大學 電機系 廖婉君 教授 13
C++ HOW TO PROGRAM 8/e 2011 Paul Deitel, Harvey Deitel p.297 依據著作權法第46、52、65條合理使用
Similar presentations