「陣列(Array)」,按序排列的同類資料元素的集合。
1.索引值是從0 開始計數
2.索引值不可超出陣列的範圍
3.取陣列最後一個值=陣列長度-1

 - #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main(){
- string birds[]={"小白","小黃","小綠","小紅","小紫","小橙","小靖","小橘"};
- // cout<<birds[0]<<endl;
- // cout<<birds[1]<<endl;
- // cout<<birds[5]<<endl;
- for(int i=0;i<=7;i++)
- {
- cout<<"第"<<i+1<<"隻"<<birds[i]<<endl;
- }
- system("pause");
- return 0;
- }
複製代碼 假設五年級共有8個班: 1班有30人, 2班有31人, 3班有33人, 4班有32人, 5班有31人, 6班有35人, 7班有28人, 8班有29人. 試利用陣列與for迴圈, 列出五年級各班的人數.
 - #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int n[8]={30,31,33,32,31,35,28,29};
- for(int i=0; i<8; i++)
- cout<<"5年"<<i+1<<"班有"<<n[i]<<"人"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |