# Ch07 陣列 > 上一章: [Ch06 函式庫、格式化輸出](https://hackmd.io/@sunfrancis12/SyVV-RoGT) > 下一章: [Ch08 字串處理]( https://hackmd.io/@sunfrancis12/BykpM0iza) > 回目錄: [NTCU程式戰鬥營C講義](https://hackmd.io/@sunfrancis12/ByfdXdjG6) ## 什麼是陣列 還記得我們之前有講到變數的部分嗎? 我們把變數形容成一個容器(箱子),並且可以存放相對應的資料 而陣列其實就是一整排的箱子,並且為每個箱子都貼上一個編號 ![image.png](https://hackmd.io/_uploads/BJjQFdOm6.png) ## 陣列的命名 在C語言裡面,要命名一個陣列跟命名一個變數的方法差不多 但是要注意必須先幫陣列規劃空間,也就是箱子的數量 ```c= 格式: {變數型態} {名稱}[{大小}] int a[40]; //一個擁有40格空間的整數陣列 float b[50]; //一個擁有40格空間的浮點數陣列 ``` ## 陣列的索引 正如我們前面提到的,每個陣列裡面的空間都有其相對印的編號 要注意的是在程式的世界裡面,陣列的編號是從0開始數起(0,1,2,3 ...) ![image.png](https://hackmd.io/_uploads/HJjmTudQ6.png) 如果我們要尋找陣列裡面某個編號的空間裡存放的値,我們必須以以下方式索引 ```C= 格式: {名稱}[{位置}] printf("%d",a[3]) //a陣列的第三格空間的値 printf("%f",b[50]) //b陣列的第50格空間的値 ``` 我們可以使用`{}`的方式來初始化陣列內部的値 ![image.png](https://hackmd.io/_uploads/SJ0SyKum6.png) 範例程式: ```c= #include<stdio.h> int main(){ int a[3] = {3,6,7}; printf("a[0] = %d\n",a[0]); printf("a[1] = %d\n",a[1]); printf("a[2] = %d\n",a[2]); } ``` 輸出結果: ```c= a[0] = 3 a[1] = 6 a[2] = 7 ``` :::info 在輸出陣列的索引値時,我們要使用與陣列變數型態相對應的格式化字串 ::: ## 使用迴圈搭配陣列 我們可以結合我們之前學到的for迴圈,把`i`當作索引値,印出陣列的內容 ```c= #include<stdio.h> int main(){ int a[10] = {3,6,7,8,9,2,4,5,1,0}; //i的値為0~9 for(int i=0;i<10;i++){ //把a[0] ~ a[9]的値都印出 printf("a[%d] = %d\n",i,a[i]); } } ``` 輸出結果: ```c= a[0] = 3 a[1] = 6 a[2] = 7 a[3] = 8 a[4] = 9 a[5] = 2 a[6] = 4 a[7] = 5 a[8] = 1 a[9] = 0 ``` 我們也可以透過輸入的方式來賦予陣列每個空間的値 ```c= #include<stdio.h> int main(){ int a[10]; //i的値為0~9 for(int i=0;i<10;i++){ printf("input the vaule of a[%d]: ",i); scanf("%d",&a[i]); } //i的値為0~9 for(int i=0;i<10;i++){ printf("a[%d] = %d\n",i,a[i]); } } ``` 輸入內容: ```c= 3 25 236 25 2 2362 754 458 458 369 ``` 輸出結果: ```c= input the vaule of a[0]: 3 input the vaule of a[1]: 25 input the vaule of a[2]: 236 input the vaule of a[3]: 25 input the vaule of a[4]: 2 input the vaule of a[5]: 2362 input the vaule of a[6]: 754 input the vaule of a[7]: 458 input the vaule of a[8]: 458 input the vaule of a[9]: 369 a[0] = 3 a[1] = 25 a[2] = 236 a[3] = 25 a[4] = 2 a[5] = 2362 a[6] = 754 a[7] = 458 a[8] = 458 a[9] = 369 ``` <a style="color:orange">【例題】</a> > 輸入10個整數,而且每個數字的範圍為1-1000 > 請輸出所有輸入的整數 > 請輸出最大以及最小的整數,還有他們各自在陣列中的索引直 範例輸入: ```c= 10 6 683 502 3 73 68 385 83 64 ``` 範例輸出: ```c= input the vaule of a[0]: 10 input the vaule of a[1]: 6 input the vaule of a[2]: 683 input the vaule of a[3]: 502 input the vaule of a[4]: 3 input the vaule of a[5]: 73 input the vaule of a[6]: 68 input the vaule of a[7]: 385 input the vaule of a[8]: 83 input the vaule of a[9]: 64 a[0] = 10 a[1] = 6 a[2] = 683 a[3] = 502 a[4] = 3 a[5] = 73 a[6] = 68 a[7] = 385 a[8] = 83 a[9] = 64 最大值: a[2] = 683 最小值: a[4] = 3 ``` 範例程式: ```c= #include<stdio.h> int main(){ int a[10]; int max=0,min=1001,max_i,min_i; //最大值,最小值,最大值的索引值,最小值的索引值 //i的値為0~9 for(int i=0;i<10;i++){ printf("input the vaule of a[%d]: ",i); scanf("%d",&a[i]); if(a[i]>max){ max = a[i]; max_i = i; } if(a[i]<min){ min = a[i]; min_i = i; } } //i的値為0~9 for(int i=0;i<10;i++){ printf("a[%d] = %d\n",i,a[i]); } printf("最大值: a[%d] = %d\n",max_i,max); printf("最小值: a[%d] = %d",min_i,min); } ``` <a style="color:red">【另解】</a> 我們可以只使用最大值和最小值的索引值,完成題目的需求 ```c= #include<stdio.h> int main(){ int a[10]; //i的値為0~9 for(int i=0;i<10;i++){ printf("input the vaule of a[%d]: ",i); scanf("%d",&a[i]); } int max_i=a[0],min_i=a[0]; //最大值的索引值,最小值的索引值 for(int i=0;i<10;i++){ printf("a[%d] = %d\n",i,a[i]); if(a[min_i]>a[i]) min_i = i; //最小值的索引值大於當前陣列的索引直(a[i]),則替換 if(a[max_i]<a[i]) max_i = i; //最大值的索引值小於當前陣列的索引直(a[i]),則替換 } printf("最大值: a[%d] = %d\n",max_i,a[max_i]); printf("最小值: a[%d] = %d",min_i,a[min_i]); } ``` <a style="color:orange">【例題】</a> > 請輸出1-100輸出總共多少個質數,並將所有質數放到一個陣列裡面 範例輸出: ```c= 1 是質數 2 是質數 3 是質數 5 是質數 7 是質數 11 是質數 13 是質數 17 是質數 19 是質數 23 是質數 29 是質數 31 是質數 37 是質數 41 是質數 43 是質數 47 是質數 53 是質數 59 是質數 61 是質數 67 是質數 71 是質數 73 是質數 79 是質數 83 是質數 89 是質數 97 是質數 100以內的質數共25個 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 ``` 範例程式: ```c= #include<stdio.h> int main(){ int prime[100]; //存放質數的陣列 int prime_count=0; for(int i=1;i<=100;i++){ //從1跑到100 int sum=0; //sum為因數(扣掉1和自己)的數量 for(int j=2;j<i;j++){ if(i%j==0) sum++; } if(sum==0 || i==2){ printf("%d 是質數\n",i); prime[prime_count] = i; prime_count++; } } printf("\n100以內的質數共%d個\n",prime_count-1); for(int i=0;i<prime_count;i++){ printf("%2d ",prime[i]); if((i+1)%10==0 && i!=1) printf("\n"); } } ``` --- > 上一章: [Ch06 函式庫、格式化輸出](https://hackmd.io/@sunfrancis12/SyVV-RoGT) > 下一章: [Ch08 字串處理]( https://hackmd.io/@sunfrancis12/BykpM0iza) > 回目錄: [NTCU程式戰鬥營C講義](https://hackmd.io/@sunfrancis12/ByfdXdjG6)