Unit 6 · Arrays
Unit 6 · Arrays
Start Here / 開始做題
- Read Input and Output first. 中文:先找輸入及輸出。
- Write down the variables you need. 中文:先決定變數。
- Trace the sample once by hand. 中文:用範例手算一次。
- Write, run, then compare every output character. 中文:執行後逐字比較輸出。
Unit Dashboard / 單元地圖
| Learn | Meaning | Use |
|---|---|---|
| 1D array | fixed-size list | marks and numbers |
| index | positions 0 to N-1 | read every item |
| 2D array | rows and columns | tables and matrices |
| function | reusable named calculation | multiply and average |
Quick Concepts / 重點
| Syntax | Student note |
|---|---|
int arr[100]; | Reserve 100 integer positions. |
arr[i] | The first element is arr[0]. |
maximum = arr[0]; | Safe start for maximum/minimum. |
arr[row][col] | Use nested loops for a matrix. |
From the 2022/23 Notebook / 補充
int multiply(int a, int b) { return a * b; }
// For an average, divide by 2.0 to keep decimals.
| Extra task | Input | Output |
|---|---|---|
| Test Average | 4 / 61 62 63 64 | 62.50 |
| Replace Even with 0 | 1 2 3 4 5 6 | 1 0 3 0 5 0 |
| Positive / Negative Sums | 1 -2 3 -4 5 -6 | 9 / -12 |
| Sort Four Values | 8 2 6 1 | 1 2 6 8 |
C++ Syntax / C++ 語法
This complete program reads an array and prints its maximum value. 中文:可直接執行,找出陣列最大值。
#include <iostream>
using namespace std;
int main() {
int n, arr[100];
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
int maximum = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > maximum) maximum = arr[i];
}
cout << maximum << endl;
return 0;
}
Yellow Syntax Guide / 黃色語法重點
| Syntax | Meaning |
|---|---|
int arr[100]; | create space for 100 integers |
arr[i] | access position i |
maximum = arr[0]; | start from the first value |
if (arr[i] > maximum) | compare a new value |
Dry Run / 手算追蹤
For 15 8 27 19: start maximum=15; compare 8, 27, 19; final answer is 27.
中文:最大值先設為第一個元素,再逐個比較。
Examples / 例題
Example 6-1 · Print an Array
題目說明 / Problem Statement
Read N integers into an array and print them in order. 中文:讀入並輸出陣列。
輸入格式 / Input Format
Line 1: N. Line 2: N integers. 中文:第一行 N;第二行 N 個整數。
輸出格式 / Output Format
Print values separated by spaces. 中文:以空格輸出。
限制 / Constraints
1 ≤ N ≤ 100
範例輸入 / Sample Input
5
12 24 36 48 60
範例輸出 / Sample Output
12 24 36 48 60
Hint / 提示:Array index starts at 0.
Reference Answer / 參考答案
#include <iostream>
using namespace std;
int main() {
int n, arr[100];
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n; i++) {
if (i > 0) cout << ' ';
cout << arr[i];
}
cout << '\n';
return 0;
}
Example 6-2 · Sum and Average
題目說明 / Problem Statement
Read N marks and print their sum and average. 中文:計算總分及平均分。
輸入格式 / Input Format
Line 1: N. Line 2: N integers. 中文:第一行 N;第二行 N 個整數。
輸出格式 / Output Format
Print sum, then average to 2 decimal places. 中文:依序輸出總和及兩位小數平均。
限制 / Constraints
1 ≤ N ≤ 100
範例輸入 / Sample Input
4
78 85 92 80
範例輸出 / Sample Output
335
83.75
Hint / 提示:Use a loop and double average.
Reference Answer / 參考答案
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n, arr[100];
cin >> n;
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
cout << sum << '\n';
cout << fixed << setprecision(2) << double(sum) / n << '\n';
return 0;
}
Example 6-3 · 2D Matrix
題目說明 / Problem Statement
Read a 3 × 3 matrix and print it in matrix form. 中文:讀入並輸出 3 × 3 陣列。
輸入格式 / Input Format
Three lines, each with three integers. 中文:三行,每行三個整數。
輸出格式 / Output Format
Print the same 3 × 3 matrix. 中文:輸出相同矩陣。
限制 / Constraints
Each value is between -1000 and 1000.
範例輸入 / Sample Input
1 2 3
4 5 6
7 8 9
範例輸出 / Sample Output
1 2 3
4 5 6
7 8 9
Hint / 提示:Use arr[row][col].
Reference Answer / 參考答案
#include <iostream>
using namespace std;
int main() {
int arr[3][3];
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) cin >> arr[r][c];
}
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
if (c > 0) cout << ' ';
cout << arr[r][c];
}
cout << '\n';
}
return 0;
}
ClassWork / 課後練習
Complete every task independently. Submit source code and a screenshot of a successful run. 中文:獨立完成,提交程式碼及成功輸出截圖。
ClassWork 11-1 · Array Maximum and Minimum
題目說明 / Problem Statement
Given N integers, print the largest and smallest values. 中文:找最大值及最小值。
輸入格式 / Input Format
Line 1: N. Line 2: N integers. 中文:第一行 N;第二行 N 個整數。
輸出格式 / Output Format
Line 1: maximum. Line 2: minimum. 中文:第一行最大;第二行最小。
限制 / Constraints
1 ≤ N ≤ 100
範例輸入 / Sample Input
6
15 8 27 19 3 21
範例輸出 / Sample Output
27
3
Hint / 提示:Initialise from the first array element.
ClassWork 11-2 · Count Even Values
題目說明 / Problem Statement
Given N integers, count how many values are even and print their sum. 中文:統計偶數數量及總和。
輸入格式 / Input Format
Line 1: N. Line 2: N integers. 中文:第一行 N;第二行 N 個整數。
輸出格式 / Output Format
Line 1: count. Line 2: sum. 中文:第一行數量;第二行總和。
限制 / Constraints
1 ≤ N ≤ 100
範例輸入 / Sample Input
5
3 8 1 6 5
範例輸出 / Sample Output
2
14
Hint / 提示:Use % 2 == 0.
ClassWork 11-3 · Reverse Array
題目說明 / Problem Statement
Read N integers and print the array from last to first. 中文:倒序輸出陣列。
輸入格式 / Input Format
Line 1: N. Line 2: N integers. 中文:第一行 N;第二行 N 個整數。
輸出格式 / Output Format
Print values in reverse order. 中文:倒序輸出。
限制 / Constraints
1 ≤ N ≤ 100
範例輸入 / Sample Input
5
1 2 3 4 5
範例輸出 / Sample Output
5 4 3 2 1
Hint / 提示:Loop from N - 1 down to 0.
ClassWork 12-1 · Selection Sort
題目說明 / Problem Statement
Sort N integers in ascending order using selection sort. 中文:使用選擇排序。
輸入格式 / Input Format
Line 1: N. Line 2: N integers. 中文:第一行 N;第二行 N 個整數。
輸出格式 / Output Format
Print sorted values separated by spaces. 中文:以空格輸出排序結果。
限制 / Constraints
1 ≤ N ≤ 100; do not use library sort.
範例輸入 / Sample Input
5
4 2 9 1 6
範例輸出 / Sample Output
1 2 4 6 9
Hint / 提示:For each position, select the smallest remaining value.
ClassWork 12-2 · Even Digits Sum
題目說明 / Problem Statement
Given a positive integer, find the sum of its even digits. 中文:計算所有偶數位的總和。
輸入格式 / Input Format
One integer N. 中文:一個整數 N。
輸出格式 / Output Format
Print the sum of even digits. 中文:輸出偶數位總和。
限制 / Constraints
1 ≤ N ≤ 10^9
範例輸入 / Sample Input
57231
範例輸出 / Sample Output
2
Hint / 提示:Use % 10 and / 10.
ClassWork 12-3 · Matrix Row Sums
題目說明 / Problem Statement
Read a 3 × 3 matrix and print the sum of each row. 中文:輸出每一行的總和。
輸入格式 / Input Format
Three lines, each with three integers. 中文:三行,每行三個整數。
輸出格式 / Output Format
Print three row sums, one per line. 中文:每行輸出一個總和。
限制 / Constraints
Each value is between -1000 and 1000.
範例輸入 / Sample Input
1 2 3
4 5 6
7 8 9
範例輸出 / Sample Output
6
15
24
Hint / 提示:Use an outer row loop and an inner column loop.