1 / 26

資料坐火車 …… 談陣列 ( Array )

資料坐火車 …… 談陣列 ( Array ). 綠園 2008/12/15. student. student[3]. student[4]. student[2]. student[1]. student[0]. Array 的宣告. 整數 陣列的宣告 int student[ 5 ]; 意義:宣告了 5 個 int 大小的 連續空間 ,名稱 為 student ,沒有預設值,則為 系統殘值 。. 0. 0. 0. 0. 0. student. student[3]. student[4]. student[2].

duman
Download Presentation

資料坐火車 …… 談陣列 ( Array )

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. 資料坐火車……談陣列(Array) 綠園2008/12/15

  2. student student[3] student[4] student[2] student[1] student[0] Array 的宣告 • 整數陣列的宣告 • int student[5];意義:宣告了5個int大小的連續空間,名稱 為student,沒有預設值,則為系統殘值。

  3. 0 0 0 0 0 student student[3] student[4] student[2] student[1] student[0] Array 的宣告 • 整數陣列的宣告 • int student[5]={0};意義:宣告了5個int大小的連續空間,名稱 為student,且裏面的值皆預設為 0。

  4. 1 3 4 6 7 student student[3] student[4] student[2] student[1] student[0] Array 的宣告 • 整數陣列的宣告 • int student[5]={1, 3, 4, 6, 7};意義:宣告了5個int大小的連續空間,名稱 為student, 其預設值如下:

  5. 1.2 3.2 4.5 0 num num[0] num[1] num[2] num[3] Array 的宣告 • 浮點數陣列的宣告 • float num[4]={1.2, 3.2, 4.5};意義:宣告了4個float大小的連續空間,名稱 為num, 其預設值如下:

  6. J J o o h h n n \0 name name name[1] name[1] name[2] name[2] name[8] name[8] name[9] name[9] name[0] name[0] Array 的宣告 • 字元陣列的宣告—常用來儲存字串 • char name[10]= “John”;(雙引號) 意義:宣告了10個char大小的連續空間,名稱 為name, 其預設值如下: • char name[10]={‘J’,‘o’,‘h’,‘n’}; (單引號) 意義:宣告了10個char大小的連續空間,名稱 為name, 其預設值如下

  7. J o h n 1 name[0] \0 J o h n 2 name[1] \0 J o h n 3 name[2] \0 Array的宣告 • 字串陣列的宣告—常用來儲存字串 • char name[3][10] ={“John1”, “John2”, “John3”} • 意義:宣告了 3組10個char大小的連續空間,名稱 為name, 其預設值如下:

  8. Array的運用--Fibonacci #include <iostream>#include <cstdlib>using namespace std; int main(){ int f[10]; int i; f[0]=1; f[1]=1; for(i=2; i<10; i++) {f[i] = f[i-1] + f[i-2]; } system(“PAUSE”); return 0;}

  9. Array的運用--找最大值與最小值 • 請編寫一程式,找出33, 75, 69, 41, 32, 19中的最大值(max)與最小值(min)。Hint:(1)先開一陣列,將這些數值存下。(2)最大值:max,最小值:min。 (3)用重覆結構(for迴圈 或 while迴圈)一一比對。(4)印出 max 和 min。

  10. 練習:試設計一程式,將字串陣列中的所有小寫字母轉換成大寫字母。 #define MAX 50 int main(void) { char data[MAX]; cout << "Input a string:" ; /* 輸入字串 */ cin.getline(data, MAX); /* 小寫轉換成大寫 */ …… …… …… cout <<"\n** After translation **\n"; cout << data << endl; /* 印出陣列的內容 */ system("PAUSE"); return 0; }

  11. Array的運用-泡泡排序法(Bubble Sort) int main() { int list[5]={3, 5, 2, 4, 1}; int i, j, tmp; for(i=4; i>0; i--) for(j=0; j<i; j++) if(list[j] > list[j+1]) // 比較 { tmp=list[j];// 交換 list[j] = list[j+1]; list[j+1] = tmp; } for(i=0; i<5; i++) // 印出結果 cout << list[i]; system("PAUSE"); return 0; }

  12. 函數間傳遞一維陣列 • 函數的宣告 void show(int array[]); • 函數的呼叫 int arr[5]={1,2,3,4,5}; show(arr); • 函數的定義 void show(int array[]) { int i; for(i=0;i<5;i++) cout << array[i] << endl; } • 當傳遞的引數是陣列時,傳遞到函數中的是該陣列實際的位址,而不是另外複製一份陣列。

  13. 隨堂練習---泡泡排序法(Bubble Sort) int main() { int data[5]; ……… //亂數產生5個整數,並儲存在data內 cout << “排序前……\n”; show(data); bobble(data); cout << “排序後……\n”; show(data); return 0; } void show(int a[]) //試完成之。 void bobble(int a[]) //試完成之。

  14. 二維陣列 • 宣告:資料型態 陣列名稱[列的個數][行的個數] int sale[2][4]={{30,35,26,32}, {33,34,30,29}}; 【行】 sale sale[0] 【列】 sale[1]

  15. 二維陣列—印出陣列中的資料 int main(void) { int i,j,sum=0; int sale[2][4]={{30,35,26,32},{33,34,30,29}}; for(i=0;i<2;i++) { cout << “業務員” << (i+1) << “的業績分別為:”; for(j=0;j<4;j++) { cout << sale[i][j] << “ ”; sum+=sale[i][j]; } cout << endl; } cout <<endl <<“本年度總銷售量為” <<sum <<“輛車” <<endl; system(“pause”); return 0; }

  16. 二維陣列—印出陣列中的資料 int main(void) { int i,j,sum=0; int sale[2][4]={{30,35,26,32},{33,34,30,29}}; for(i=0;i<2;i++) { cout << “業務員”<< (i+1) << “的業績分別為:”; for(j=0;j<4;j++) { cout << sale[i][j] << “ ”; sum+=sale[i][j]; } cout << endl; } cout <<endl <<“本年度總銷售量為”<<sum <<“輛車”<<endl; system(“pause”); return 0; }

  17. 二維陣列 • C++允許二維以上的多維陣列不必定義陣列的長度,但是只有最左邊(第一個)的註標可以省略不定義外,其它的註標都必須定義其長度。如: int temp[][4]={{30,35,26,32}, {33,34,30,29}, {25,33,29,25}};

  18. 多維陣列 第二維 int a[2][4][3]; 第一維 第三維 第二維 第一維 第三維

  19. 函數間傳遞多維陣列 • 函數的宣告 void show(int array[2][4]); • 函數的呼叫 int arr[2][4]={{1,2,3,4},{5,6,7,8}}; show(arr); • 函數的定義 void show(int array[2][4]) { int i,j; for(i=0;i<2;i++) { for(j=0;j<4;j++) cout << array[i][j] << “ ”; } cout << endl; } • 當傳遞的引數是多維陣列時,只有陣列名稱後面的第一個註標可以不填入元素個數,其餘均須填入數值。

  20. 隨堂練習 • 自我評量第7題、第8題。

  21. 字串的輸入與輸出 • 使用 cin char str[20]; cin >> str; 字串中不可包含空白字元。 • 使用 cin.getline(字串名稱,最大長度,字串結束字元); char str[15]; cin.getline(str,15); 字串結束字元預設為 ‘\n’,若不需更改則不必指出該結束字元。 字串中可包含空白字元。 • 使用 cin.get(字元變數名稱); char ch; cin.get(ch); 在輸入單一字元的情況下使用。

  22. 字串陣列元素的引用及存取 int main(void) { int i; char name[3][15]; for(i=0;i<3;i++) //輸入字串 { cout << “Input student” << i << “\’s name:”; cin.getline(name[i],15); } for(i=0;i<3;i++) //輸出字串 cout << “name[” << i << “]=” << name[i] << endl; cout << endl; for(i=0;i<3;i++) //輸出字串位址 { cout << “addr of name[” << i << “]=” << &name[i] << endl; cout << “addr of name[” << i << “][0]=”; cout << (name+i) << endl << endl; } system(“pause”); return 0; }

  23. 字串陣列的複製 int main(void) { int i,j; char name[3][15]={“David”, “Jane Wang”, “Tom Lee”}; char copystr[3][15]; for(i=0;i<3;i++) { for(j=0;j<15;j++) if(name[i][j] == ‘\0’ break; else copystr[i][j] = name[i][j]; copystr[i][j]=‘\0’; } for(i=0;i<3;i++) cout << “copystr[” << i << “]=” << copystr[i] << endl; system(“pause”); return 0; }

  24. 字串陣列的複製 int main(void) { int i,j; char name[3][15]={“David”, “Jane Wang”, “Tom Lee”}; char copystr[3][15]; for(i=0;i<3;i++) //複製字串陣列 { for(j=0;j<15;j++) if(name[i][j] == ‘\0’) //判斷是否為字串結束字元 break; else copystr[i][j] = name[i][j]; copystr[i][j]=‘\0’; } for(i=0;i<3;i++) //輸出字串陣列 cout << “copystr[” << i << “]=” << copystr[i] << endl; system(“pause”); return 0; }

  25. 常用字串處理函數 • strlen字串長度 strlen(string); //計算string字串的長度 • strcat字串連結 strcat(dest,source); //將dest字串加上source字串後存回dest。 • strcpy字串拷貝 strcpy(dest, source); //將source字串拷貝至dest • strlwr將字串中的大寫字母轉換小寫 • strupr將字串中的小寫字母轉換大寫 • strcmp字元比較 strcmp(str1, str2); //根據ASCII值的大小比較str1, str2,傳回值分為 //小於0:str1 < str2 //等於0:str1 = str2 //大於0:str1 > str2

  26. 常用字元處理函數 • isalpha是否為英文字母 isalpha(ch); //結果為0表示為數字 //結果為1表示為大寫英文字母 //結果為2表示為小寫英文字母 • isupper是否為大寫英文字母 isupper(ch); • islower是否為小寫英文字母 islower(ch); • toupper轉換為大寫英文字母 toupper(ch); • tolower轉換為小寫英文字母 tolower(ch);

More Related