---
###### tags: `課程`
---
陣列-array
===
----

---
## 一維陣列
----
## 格式
```cpp=
資料型態 陣列名稱[陣列大小];
int arr[10];
int ay[3] = {0, 1, 2};
array<int,10> arr;
```
**陣列的區間是arr[0] ~ arr[n-1]**
<font color="red">**不包括arr[n]**</font>
----
## 輸入輸出
```cpp=
for(int i = 0; i < 10 ; i++)
cin >> arr[i];
for(int i = 0; i < 10 ; i++)
cout << arr[i] << ' ';
cout << endl;
rep(i,n) cin >> arr[i];
rep(i,n) cout << arr[i] << endl;
```
----
## 練習時間!
給定$n$ 接下來會有$n$個數字
請倒過來輸出
進階:並且輸出有多少大於零的數
----
## code
```cpp=
#include<iostream>
using namespace std;
int main(){
int n,cnt = 0;
cin>>n;
int arr[n];
for(int i = 0 ; i< n ; i++){
cin >> arr[i];
if(arr[i] > 0) cnt++;
}
for(int i = n-1 ; i >= 0 ; i--)
cout << arr[i] << ' ';
cout << endl << cnt << endl;
return 0;
}
```
---
## 二維陣列
----
表格是很常見的東西
他就是廣義的陣列
| arr[i][j] | 0 | 1 | 2 |
| -- | -- | -- | --|
| 0 | arr[0][0] | arr[0][1]|arr[0][2]|
| 1 | arr[1][0] | arr[1][1]|arr[1][2]|
| 2 | arr[2][0] | arr[2][1]|arr[2][2]|
| 3 | arr[3][0] | arr[3][1]|arr[3][2]|
----
```cpp=
int arr[5][10];
for(int i = 0; i < 5; i++)
for(int j = 0; j < 10; j++)
cin>>arr[i][j];
for(int i = 0; i < 5; i++){
for(int j = 0; j < 10; j++)
cout << arr[i][j] << ' ';
cout<<endl;
}
```
----
## 練習時間
輸出九九乘法表(要用陣列先存起來)
----
```cpp=
int arr[9][9];
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9;j++)
arr[i][j] = i*j;
for(int i = 0; i < 5; i++){
for(int j = 0; j < 10; j++)
cout << arr[i][j] << ' ';
cout<<endl;
}
```
---
## 進階
----
## [ ]運算子
會把 arr[10] 括號內跟括號前的位置合併
-> arr(記憶體位置) + 10
10[arr]也是合法的!
```cpp=
試試看: cout << arr <<endl;
```
----
## 行尾換行
```cpp=
for(int i = 0; i < n;i ++)
cout<<arr[i]<<" \n"[i == n-1];
```
---
## string
字串是c++STL的一種工具
用於處理一串文字 用法則類似一維陣列
----
## 輸入輸出
```cpp=
string str;
cin >> str;
cout << str << endl;
cout << str[1] << endl;
```
---
## 題目們
---
## 排序數列
給出n 接下來會有一個數列
請由小到大排序他們之後輸出
input: 5 3 9 1 2 5
output: 1 2 3 5 9
----
## code
```cpp=
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
int tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
}
}
//輸出我就省略了
return 0;
}
```
----
## 進階
```cpp=
int n; cin >> n;
rep(i, n) cin >> arr[i];
sort(arr, arr + n);
rep(i, n) cout << arr[i] << " \n"[i == n - 1];
```
---
## 大數
給定兩個大約10^40的數字
請輸出他們相加
----
## code
```cpp=
```
---
##
---
int 啊就宣告啊
array(或著是任何字元) 宣告後陣列的名字
[n] 存放的資料個數
{ }每個資料的初值
----

----
宣告一個空間為5的**陣列**(自行設計初值)
並且確保輸入的數字(n<5)與輸出的數字相同
(使用陣列)

---
進階
宣告一個**陣列**
並且確保輸入的數字(n<10000)與輸出的數字相同
(一樣要用陣列)
----
建立一個包含n個元素的陣列(0<n<101),輸入n個數值,顯示所有數值與最大數

----
小明是老師,他想偷懶但他不會寫程式,不管怎樣請幫他寫一個程式:
"先輸入學生的成績,透過陣列儲存成績,計算總成績與平均"
(學生數最大為50)

----

----
```cpp=
#include <iostream>
using namespace std;
int main() {
int a[100] = {}, n, max = 0;
cout << "輸入n: ";
cin >> n;
for (int i = 0; i < n; ++i) {
cout << "輸入第" << i + 1 << "個數: ";
cin >> a[i];
if (a[i] > max) max = a[i];
}
cout << "輸入的數: ";
for (int j = 0; j < n; ++j) {
cout << a[j] << " ";
}
cout << "\n最大數: " << max << "\n";
}
```
----

---
### 二維陣列

###### (我亂編的)
----

如上圖,第一個框框指的是"列"(直的),
第二個框框指的是"行"(橫的)
----
#### 二維陣列初值設定
---
假設要設定3X2的陣列初值 a[3][2]
若少一個數值則自動設定為0
int a[3][2]={{1,2},{3},{5,6}}
對應關係:
| 陣列與值 | 陣列與值 |
|:----:|:--------:|
| a[0][0]=1 | a[0][1]=2 |
| a[1][0]=3 | a[1][1]=0 |
| a[2][0]=5 | a[2][1]=6 |
----
請設定好與題目一樣的初值,並用二維陣列印出與下圖一樣的結果

----
小明又在偷懶,他最近在玩掛機遊戲,下表是他的角色數值和光環效果

小明想花一些貨幣買三個光環(可重複),請寫一個程式來推算購買後的結果
###### (挖鑽效率+n%的算法:300/1+n%)
###### (%的效果為疊加而不是相乘)
###### (輸入1就是指鋒利之光,2就是靈性之光...以此類推)
----


##### (警告(?):若對程式沒有一定程度的話請先做其他題)
----
###### a遊戲公司3首人氣歌曲某季的粗計遊玩次數如下:
| 歌曲 | 6月 | 7月 | 8月 |
| -------- | -------- | -------- | -------- |
| Fracture Ray | 2130000 | 4010000 | 3660000 |
| Grievous Lady | 2710000 | 4630000 | 5000000 |
| Tempestissimo | 4330000 | 4860000 | 2230000 |
###### 建立一個二維陣列儲存資料,並計算每首歌和每個月的總遊玩次數
###### 以及這一季的總遊玩次數

----
```cpp=
#include <iostream>
using namespace std;
int main(){
int a[3][5]={{1,2,3,4,5},{6,7,8},{9}};
for(int i=0;i<5;++i){
for(int j=0;j<3;++j){
cout<<" | "<<a[j][i];
}
cout<<" |\n";
}
}
```
----
```cpp=
#include <iostream>
using namespace std;
int main(){
int a[3][3]={{213,401,366},{271,463,500},{433,486,223}};
int sum_song[3]={},sum_month[3]={},sum;
for(int i=0;i<3;++i){
for(int j=0;j<3;++j){
sum_song[i]+=a[i][j];
sum_month[i]+=a[j][i];
sum+=a[i][j];
}
}
cout<<"FR遊玩次數: "<<sum_song[0]*10000;
cout<<"\nGL遊玩次數: "<<sum_song[1]*10000;
cout<<"\nTT遊玩次數: "<<sum_song[2]*10000;
for(int i=0;i<3;++i){
cout<<endl<<i+6<<"月遊玩次數: "<<sum_month[i]*10000;
}
cout<<"\n總計遊玩次數: "<<sum*10000<<endl;
}
```
----

----

---

{"metaMigratedAt":"2023-06-15T15:33:48.732Z","metaMigratedFrom":"Content","title":"陣列-array","breaks":true,"contributors":"[{\"id\":\"d00f6533-e69a-43c0-9ace-aa4bdb9a7ead\",\"add\":75,\"del\":1},{\"id\":\"7d4f22ac-9934-417b-aa5e-c76934d4fc98\",\"add\":6356,\"del\":3708},{\"id\":\"e11ac3bf-451f-4c7d-8b92-eb93bd018787\",\"add\":4136,\"del\":1581}]"}