陣列是什麼
為什麼需要陣列
陣列的語法
常見的陣列使用實例
陣列的小秘密
變數
是由相同類型的元素(element)的集合所組成的資料結構,分配一塊連續的記憶體來儲存。
by 維基百科
不要問那麼多,給我學就對了(x
[你媽]
OO!好無聊喔!我們來收衣櫃吧!
接下來我會從上到下告訴你衣櫃中的每一層有多少衣服。
請幫我從下到上輸出每一層的衣服數量。
[這你]
身為資訊之芽語法班的學員,讓我來寫個程式解決這個問題!
[這你]
case1:你在一個挺正常的家裡,衣櫃有五層。
#include <iostream>
int main() {
int grid_1, grid_2, grid_3, grid_4, grid_5 ;
std::cin >> grid_1 >> grid_2 >> grid_3
>> grid_4 >> grid_5 ;
std::cout << grid_5 << " " << grid_4 << " " << grid_3
<< " " << grid_2 << " " << grid_1 << "\n" ;
return 0 ;
}
[這你]
case2:你在一個挺不正常的家裡,衣櫃有二十層。
#include <iostream>
int main() {
int grid_1, grid_2, grid_3, grid_4, grid_5,
grid_6, grid_7, grid_8, grid_9, grid_10,
grid_11, grid_12, grid_13, grid_14, grid_15,
grid_16, grid_17, grid_18, grid_19, grid_20 ;
std::cin >> grid_1 >> grid_2 >> grid_3
>> grid_4 >> grid_5 >> grid_6
>> grid_7 >> grid_8 >> grid_9
>> grid_10 >> grid_11 >> grid_12
>> grid_13 >> grid_14 >> grid_15
>> grid_16 >> grid_17 >> grid_18
>> grid_19 >> grid_20 ;
std::cout << grid_20 << " " << grid_19 << " " << grid_18
<< " " << grid_17 << " " << grid_16 << " " << grid_15
<< " " << grid_14 << " " << grid_13 << " " << grid_12
<< " " << grid_11 << " " << grid_10 << " " << grid_9
<< " " << grid_8 << " " << grid_7 << " " << grid_6
<< " " << grid_5 << " " << grid_4 << " " << grid_3
<< " " << grid_3 << " " << grid_2 << " " << grid_1
<< "\n" ;
return 0 ;
}
[這你]
case3:你住在室內挑高裝潢的101大樓,衣櫃有1000層。
#include <iostream>
int main() {
int arr[6] ;
char str[20] ;
bool flags[20] ;
double numbers[100] ;
}
元素型別
陣列名稱[陣列大小] ;
#include <iostream>
int main() {
int array[6] ;
array[1] = 5 ;
array[2] = 3 ;
array[3] = -1 ;
array[4] = 9 ;
array[5] = 2 ;
array[6] = 7 ;
std::cout << array[1] << " " << array[2] << " " << array[3] << " "
<< array[4] << " " << array[5] << " " << array[6] << " "
<< "\n" ;
return 0 ;
}
陣列名稱[索引值] = 數值 ;
索引值 | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|
變數值 | 5 | 3 | -1 | 9 | 2 | 7 |
int array[6] ;
#include <iostream>
int main() {
int array[6] ;
array[0] = 5 ;
array[1] = 3 ;
array[2] = -1 ;
array[3] = 9 ;
array[4] = 2 ;
array[5] = 7 ;
std::cout << array[0] << " " << array[1] << " " << array[2] << " "
<< array[3] << " " << array[4] << " " << array[5] << " "
<< "\n" ;
return 0 ;
}
陣列名稱[索引值] = 數值 ;
#include <iostream>
int main() {
//陣列大小為6
int arr_1[6] = {5, 3, -1, 9, 2, 7} ;
//陣列大小為4
int arr_2[] = {7, 9, 3, 1} ;
}
元素型別
陣列名稱[陣列大小] = {元素, 元素, ... , 元素} ;
#include <iostream>
int main() {
//初始化為零
int arr_1[6] = {0, 0, 0, 0, 0, 0} ;
//這樣也是初始化為零
int arr_2[6] = {0} ;
//這樣也是初始化為零
int arr_3[6] = {} ;
//那這樣是初始化為1嗎?
int arr_4[6] = {1} ;
std::cout << arr_4[0] << " " << arr_4[1] << " " << arr_4[2] << " "
<< arr_4[3] << " " << arr_4[4] << " " << arr_4[5] << " "
<< "\n" ;
return 0 ;
}
陣列被部分初始化的時候,編譯器會將未被初始化的元素設為0。
#include <iostream>
int main() {
int arr[3] ;
std::cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n" ;
return 0 ;
}
未被初始化的陣列裡面存的是之前那格記憶體殘留下來的東西
aka怪怪的數字
題目:給你n個數字......
#include <iostream>
int main() {
int n ;
std::cin >> n ;
int arr[n] ;
return 0 ;
}
#include <iostream>
int main() {
int n ;
std::cin >> n ;
int arr[n] ;
return 0 ;
}
這樣有可能會compile error
陣列的長度必須是「常數」!
#include <iostream>
int main() {
//以題目給的最大數量宣吿陣列
int arr[10000] ;
int n ;
std::cin >> n ;
for (int i = 0; i < n; i++) {
std::cin>> arr[i] ;
}
return 0 ;
}
題目:n筆資料, 1<=n<=10000
或是:
-動態記憶體配置dynamic memory allocation
-用std::vector
[你媽]
OO!好無聊喔!我們來收衣櫃吧!
接下來我會從上到下告訴你衣櫃中的每一層有多少衣服。
請幫我從下到上輸出每一層的衣服數量。
#include <iostream>
int main() {
int arr[5] ;
std::cin >> arr[0] >> arr[1] >> arr[2] >> arr[3] >> arr[4] ;
std::cout << arr[4] << " " << arr[3] << " " << arr[2] << " "
<< arr[1] << " " << arr[0] << "\n" ;
return 0 ;
}
有沒有覺得哪裡怪怪的?
#include <iostream>
int main() {
int arr[5] ;
std::cin >> arr[0] >> arr[1] >> arr[2] >> arr[3] >> arr[4] ;
std::cout << arr[4] << " " << arr[3] << " " << arr[2] << " "
<< arr[1] << " " << arr[0] << "\n" ;
return 0 ;
}
#include <iostream>
int main() {
int grid_1, grid_2, grid_3, grid_4, grid_5 ;
std::cin >> grid_1 >> grid_2 >> grid_3
>> grid_4 >> grid_5 ;
std::cout << grid_5 << " " << grid_4 << " " << grid_3
<< " " << grid_2 << " " << grid_1 << "\n" ;
return 0 ;
}
學會陣列後寫的
還沒學會陣列時寫的
想想上一堂課教了什麼!
沒錯!就是迴圈!
for (初始值; 執行條件; 操作) {敘述}
#include <iostream>
int main() {
int arr[5] ;
for (int i = 0; i < 5; i++) {
std::cin >> arr[i] ;
}
for (int i = 4; i >= 0; i--) {
std::cout << arr[i] << " " ;
}
std::cout << "\n" ;
return 0 ;
}
我愛零分(算分)
σ.σ - 2015(不算分)
#include <iostream>
int main() {
int score[10005], n ;
std::cin >> n ;
for (int i = 0; i < n; i++) {
std::cin >> score[i] ;
if (score[i] >= 0) score[i] = 0 ;
}
for (int i = n - 1; i >= 0; i--) {
std::cout << score[i] << "\n" ;
}
return 0 ;
}
我愛零分
#include <iostream>
int main() {
int arr[1005], n ;
std::cin >> n ;
for (int i = 0; i < n; i++) {
std::cin >> arr[i] ;
}
for (int i = 0; i < n; i++) {
std::cout << arr[arr[i]] << "\n" ;
}
return 0 ;
}
σ.σ - 2015
陣列的小秘密
有沒有想過,如果直接輸出arr這個變數名稱會怎麼樣?
#include <iostream>
int main() {
int arr[6] = {5, 6, 7, 1, 2, 3} ;
std::cout << arr << "\n" ;
return 0 ;
}
陣列的小秘密
會輸出一個記憶體位址!
0x代表這是一個16進位的數字
0x16b14b370
陣列的小秘密
陣列的小秘密
那arr + 1、arr + 2 呢?
#include <iostream>
int main() {
int arr[6] = {5, 6, 7, 1, 2, 3} ;
for (int i = 0; i < 6; i++) {
std::cout << (arr + i) << "\n" ;
}
return 0 ;
}
陣列的小秘密
arr + 0 | arr[0]的記憶體位址 | 0x16d96f300 |
---|---|---|
arr + 1 | arr[1]的記憶體位址 | 0x16d96f304 |
arr + 2 | arr[2]的記憶體位址 | 0x16d96f308 |
arr + 3 | arr[3]的記憶體位址 | 0x16d96f30c |
arr + 4 | arr[4]的記憶體位址 | 0x16d96f310 |
arr + 5 | arr[5]的記憶體位址 | 0x16d96f314 |
為什麼會相差4?
陣列的小秘密
8個bit = 1個byte
integer 佔掉32個bit的記憶體 = 4個byte的記憶體
陣列的小秘密
一個 int
型態的儲存格地址 + 1
會是指向「下一格」 int
儲存格的地址
(參考2022年北區語法班上課講義)
arr | arr[0]的記憶體位址 | 0x16b14b370 |
---|---|---|
arr + 1 | arr[1]的記憶體位址 | 0x16b14b374 |
arr + 2 | arr[2]的記憶體位址 | 0x16b14b378 |
arr + 3 | arr[3]的記憶體位址 | 0x16b14b37c |
arr + 4 | arr[4]的記憶體位址 | 0x16b14b380 |
arr + 5 | arr[5]的記憶體位址 | 0x16b14b374 |
陣列的小秘密
#include <iostream>
int main() {
int arr[6] = {5, 6, 7, 1, 2, 3} ;
for (int i = 0; i < 6; i++) {
std::cout << *(arr + i) << " is same as " << arr[i] << << "\n" ;
}
return 0 ;
}
取值運算子*
用法為*(記憶體位址)
而實際上arr[i] == *(arr + i)
陣列的小秘密
上面教的東西都跟pointer有關,有興趣的可以自己去找資料,不然等我們04/08講給你聽也是可以XD
小小總結
-陣列是由相同類型的元素的集合所組成的資料結構
-陣列是儲存在一些連續的儲存格裡
-陣列的索引值是從0開始的!
-陣列通常會搭配迴圈使用