1 / 15

陣列 (Array)

Introduction to the C Programming Language. 陣列 (Array). 陣列共有 3 個元素 , 起始索引值為 0. 陣列. 可存放 相同資料型態的資料 ,陣列與變數一樣,也需要經過宣告才能使用。 Compiler 給陣列的記憶體是一個 連續的區塊 。 一維陣列宣告格式 : 資料型態 陣列名稱 [ 元素個數 ]; int score[3]; /* 宣告整數陣列 score, 可存放 3 個元素* / 宣告完陣列後,如果要使用陣列的元素,就可以利用陣列的”索引值”來操作。

gram
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. Introduction to the C Programming Language 陣列 (Array)

  2. 陣列共有3個元素,起始索引值為0. 陣列 • 可存放相同資料型態的資料,陣列與變數一樣,也需要經過宣告才能使用。 • Compiler給陣列的記憶體是一個連續的區塊。 • 一維陣列宣告格式: • 資料型態 陣列名稱 [元素個數]; • int score[3]; /*宣告整數陣列score,可存放3個元素*/ • 宣告完陣列後,如果要使用陣列的元素,就可以利用陣列的”索引值”來操作。 • 陣列就好比宿舍的房間,索引值就是房間的編號,只要根據房間編號(索引值) ,就能找到房客(陣列的元素)。 • []為陣列修飾符號,每一組方括號表示一維(dimension). • 在C/C++語言中,第一個元素的索引值一定是0.

  3. 一維陣列(1D Array) • 宣告格式 : 資料型態 陣列名稱[陣列大小]; • 例如: int score[5]; score[1]=23; m score[0] m+4 score[1] m+8 score[2] m+12 score[3] m+16 score[4] 記憶體位址 記憶體 陣列名稱 陣列第一個元素之位址

  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] 記憶體

  5. 一維陣列(1D Array) • 範例一: 比較3個數字的大小. #include <stdio.h> #include <stdlib.h> int main( ) { int count, max=0; int number[3]={5, 9, 3}; for (count = 0; count <=2 ; count++ ) { if(number[count]>max) max=number[count]; } cout<<“三數中最大的值:”; cout<<max<<endl; return 0; }

  6. 範例二:陣列界限正確版 • #include<iostream.h> • int main(void) • { • int x[4]; //用來儲存user輸入的值 • int i; • for(i=0;i<=3;i++) • { • cout<<“x[”<<i<<“]”<<“=”; //控制可以輸入多少個數值 • cin>>x[i]; • } • cout<<"The output is:"<<endl; • for(i=0;i<=3;i++) //分別輸出x陣列的值 • cout<<x[i]<<endl; • return 0; • }

  7. 陣列界限的檢查 • C++不會作陣列界限的檢查!! • 若是使用者放置資料超過陣列的界限,則C++會讓多餘的資料放在陣列之外的記憶體中,可能會蓋掉其他的程式碼,產生錯誤。 • 這種錯誤在run-time才會發生!! • #include<iostream.h> • int main(void) • { • int x[4]; • int i; • for(i=0;i<=5;i++) • { • cout<<"x["<<i<<"]"<<"="; • cin>>x[i]; • } • cout<<"The output is:"<<endl; • for(i=0;i<=5;i++) • cout<<x[i]<<endl; • return 0; • }

  8. 字元陣列 • 宣告格式 : char 陣列的名稱 [陣列的大小] ; 或 char 陣列的名稱 [列陣列的大小][行陣列大小] ; For example: char array1 [ 10 ] ; char array2 [ 5 ][ 25 ] ;

  9. /*列印出字元及字串之長度*/ #include<iostream.h> int main(void) { char arr[]="friend"; char word[]={'b'}; char cad[]="b"; cout<<"size of a[]="<<sizeof(arr)<<endl; cout<<"size of word="<<sizeof(word)<<endl; cout<<"size of c[]="<<sizeof(cad)<<endl; return 0; } 執行結果: size of arr[]=7 size of word[]=1 size of cad[]=2 字元陣列 – 範例一 為什麼用陣列儲存字串,陣列的size會多一個字元單位?

  10. 矩陣圖示 • char a[ ]=“friend"; [0] [1] [2] [3] [4] [5] [6] • char arr[4]=“C++”; [0] [1] [2] [3] • char arr[4]={‘C’,’+’,’+’}; [0] [1] [2] [3]

  11. 二維陣列(2D Array) • 宣告格式 : 資料型態 陣列名稱[列數][行數]; • 例如: int num[3][2]; /* 宣告一個 3x2 的陣列*/ row 1m num[0][0] m+4 num[0][1] m+8 num[1][0] m+12 num[1][1] m+16 num[2][0] m+20 num[2][1] 記憶體 row2 row3

  12. 二維陣列初值的設定 • 資料型態 陣列名稱[列個數][行個數]={{第一列初值}, {第二列初值}, …, {第N列初值}}; • int sale[2][4]={{30,35,26,32}, {33,34,30,29}}; 1維陣列,有4個元素 1維陣列,有4個元素 第4行 第3行 第2行 第1行 第一列 第二列

  13. 二維陣列(2D Array) • 範例: 輸入兩個3*3的陣列, 將加總結果存入第三個陣列內. • #include<iostream.h> • int main(void) • { • int num1[3][3],num2[3][3],num3[3][3]; • int i,j; • cout<<“please input first 2D-dimension array:\n”; //請輸入第一個2維陣列的值 • for(i=0;i<3;i++) • { • for(j=0;j<3;j++) • cin>>num1[i][j]; • } • cout<<"please input second 2D-dimension array:\n"; • for(i=0;i<3;i++) //請輸入第二個2維陣列的值 • { • for(j=0;j<3;j++) • cin>>num2[i][j]; • } • for(i=0;i<3;i++) • { • for(j=0;j<3;j++) //算出前兩個陣列加總的結果並存入第三個2維陣列 • { • num3[i][j]=num1[i][j]+num2[i][j]; • cout<<"num3["<<i<<"]"; • cout<<"["<<j<<"]"<<"="<<num3[i][j]<<endl; } • } return 0; • }

  14. 找出二維陣列的最大值 • #include<iostream.h> • int main(void) • { • int arr[2][4]={{3,13,26,32},{33,10,25,29}}; • int i,j,max=arr[0][0]; //將max的值預設為arr[0][0]的值 • for(i=0;i<2;i++) • { • for(j=0;j<4;j++) • { • if(max<arr[i][j]) //在二維陣列中,若找到更大的值就儲存在max • max=arr[i][j]; • } • } • cout<<"\n 最大的數是"<<max; • return 0; • }

  15. 多維陣列(Multi-dimensional Array) • 宣告格式 : 資料型態 陣列名稱[平面數][列數][行數]; • 例如: int x[2][3][4];

More Related