配列
導入済み
17:29
データ型と構造体
プログラムが格納するすべての値にはデータ型が必要であり、適切なものを選ぶことが重要です。ある商品が在庫にあるかどうかを格納する場合来说、yesという単語を書き込むこともできます…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| variables/ˈveərɪəblz/ | A quantity or named value that can change or take different values. |
| array/əˈreɪ/ | 配列 |
| row/rəʊ/ | A horizontal sequence of entries in a table or array, or a single record in a database table. |
| algorithm/ˈælɡərɪθəm/ | A finite sequence of precise steps for solving a problem or carrying out a task. |
| declarations | Statements that give variables their names and data types, and arrays their bounds. |
| pseudocode/ˈsuːdəʊkəʊd/ | A language-independent description of an algorithm using structured, programming-like statements. |
| data structure/ˈdeɪtə ˈstrʌktʃə/ | A way of organising and storing data to support particular operations. |
| data type/ˈdeɪtə taɪp/ | A classification of values that determines their representation and permitted operations. |
| identifier/aɪˈdentɪfaɪə/ | A name used to identify a program element such as a variable, function or class. |
| index/ˈɪndeks/ | 添字 |
| lower bound/ˈləʊə baʊnd/ | 下界 |
| upper bound/ˈʌpə baʊnd/ | 上界 |
| element/ˈelɪmənt/ | 要素 |
| dimension/daɪˈmenʃn/ | 次元 |
| column/ˈkɒlʌm/ | A vertical arrangement of entries, or a named field in a database table. |
| Nested loops/ˈnestɪd luːps/ | Loops placed inside other loops, with the inner loop running for each outer loop cycle. |
| outer loop/ˈaʊtə luːp/ | The loop that contains another loop and controls how often it runs. |
| linear search/ˈlɪnɪə sɜːtʃ/ | 線形探索 |
| Initialise/ɪˈnɪʃəlaɪz/ | To give a variable its starting value before it is used. |
| bubble sort/ˈbʌbl sɔːt/ | バブルソート |
| swap/swɒp/ | To exchange two values so that each takes the other's place. |
| flag/flæɡ/ | A stored indicator of a condition or state, often represented by a Boolean value or a single bit. |
| temporary variable | A variable that holds a value for a short time while other values change. |
| stepwise-refinement | The process of breaking a task into smaller, more detailed steps. |
| sorted array | An array whose elements are arranged in a chosen order. |
| overwrite/ˌəʊvəˈraɪt/ | To replace a stored value with another value, losing the original. |
シート14C
- シネマには300席がある。予約システムは
Seat1A、Seat1B、Seat1Cという名前の300個の変数を持っているわけではない。1つの配列を持っており、あなたのチケットはその中のアドレスである: 行14、席C。 - 1つの名前で、多数の値を持ち、それぞれが数字で見つけられる。行を追加してもコードは変わらず、数字をループさせればすべての席を確認できる。
- Paper 2 のアルゴリズムのほとんどは配列を遍历する: 検索、合計、ソート、最大値の探索など。
- この授業は語彙、宣言、および検定官が疑似コードと文章で求める4つのアルゴリズムである。

語彙
- 配列とは、1つの識別子の下に固定数の同じデータ型の要素を保持するデータ構造であり、各要素はインデックスによってアクセスされる。
- 下限と上限は、有効な最初のインデックスと最後のインデックスである。要素数は上限 − 下限 + 1である。
- 次元とは、要素に必要とするインデックスの数である: リストなら1つ、表なら2つ。
ThisArray[n] ← 42において、配列は1次元であり、インデックスはINTEGER変数nであり、そのインデックスにある要素に42が割り当てられる。

1つの識別子、各要素のためのインデックス、両端の境界

An array stores:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
An array is an ordered collection of same-type items accessed by index. (A record groups different types.)
An array is a data structure holding many values of the ______ type under one name.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Each value is reached by its index.
DECLARE Marks : ARRAY[0:99] OF INTEGER declares an array of ____ elements.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Upper bound minus lower bound plus one: 99 − 0 + 1 = 100. Both bounds are valid indices.
worked example: タスクが必要とする配列の宣言
- 宣言には、識別子、境界、データ型が必要である。
- 小数点を含む可能性がある120の読み取り値:
DECLARE Data : ARRAY[1:120] OF REAL。 - 150行2列のテキストの表:
DECLARE Names : ARRAY[1:150, 1:2] OF STRING。 - 数量を聞かれたら答える:
[0:99]は99ではなく100の要素を持つ。
Which declaration holds a table of 150 rows and 2 columns of text?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Two dimensions, each with a lower and upper bound, and the element type. The second option is one long list; the third has no type; the fourth has no lower bounds.
1D配列の処理
DECLARE Names : ARRAY[1:5] OF STRING
Names[3] ← "Cara"
FOR i ← 1 TO 5
OUTPUT Names[i]
NEXT i
- 下限から上限までの
FORループは、各要素を一度だけ訪問する。 - 合計、カウント、最大値、最小値については、ループ前に累加変数を設定し、内部で更新する。
2-次元配列
DECLARE Grid : ARRAY[1:3, 1:4] OF INTEGER
Grid[2, 3] ← 99 // row 2, column 3
- 第1のインデックスは行、第2のインデックスは列である。嵌套ループですべてのセルを訪問する: 外側ループは行、内側ループは列。
- 単一のシーケンスには1Dを、データに自然な2つの次元がある場合は2Dを使う: 座席のグリッドや、学生と科目別の得点表など。

Grid[row, column] 、常にこの順序で

Index a 2-D array by [row, column]
A 2-D array is a grid. Grid[row, column] reaches exactly one cell — change the row and column to see which value you land on.
In Grid[2, 3], which cell is accessed?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The first index is the row, the second the column — so row 2, column 3.
このカードは修正されたため、翻訳が更新されるまで英語で表示されます。
Worked example: a linear search that can say "not found"
- A linear search 线性查找 checks elements in turn; the example below checks the whole array and records the last matching index, if any.
FoundAt ← -1
FOR i ← 1 TO n
IF A[i] = Target THEN
FoundAt ← i
ENDIF
NEXT i
IF FoundAt = -1 THEN
OUTPUT "Not found"
ELSE
OUTPUT "Found at ", FoundAt
ENDIF
-1is outside the indices1ton, so it means "not found" in this example. Initialise 初始化 it before the loop and test it after. A search that never says "not found" loses a mark.
A linear search finds a value by:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
A linear search examines elements one by one from the start until it finds the target (or reaches the end).
Setting FoundAt to -1 before a linear search lets the program report "not found" after the loop.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
For an array indexed from 1 to n, -1 is not a valid index, so if it is unchanged after the loop the target was not in the array.
最大値、そしてそれがどこにあるか
Largest ← A[1]
Position ← 1
FOR i ← 2 TO n
IF A[i] > Largest THEN
Largest ← A[i]
Position ← i
ENDIF
NEXT i
OUTPUT Largest, " at ", Position
Largestの初期値として配列の先頭要素を使用し、0を指定してはならない。配列に負の値のみが含まれている可能性があるためである。- 同じ形状が数え上げられたり出力されたりする場合:未使用のマーカーである
""や-1と比較し、異なるものだけを数える。
このカードは修正されたため、翻訳が更新されるまで英語で表示されます。
Bubble sort
- A bubble sort 冒泡排序 makes repeated passes through the array comparing adjacent pairs and swapping those out of order, until a pass makes no swaps 交换.
- After each pass the largest unsorted value has bubbled to the end, so the next pass can stop one place earlier.
Limit ← n // n is the number of elements in Data
REPEAT
Swapped ← FALSE
FOR Index ← 1 TO Limit - 1
IF Data[Index] > Data[Index + 1] THEN
Temp ← Data[Index]
Data[Index] ← Data[Index + 1]
Data[Index + 1] ← Temp
Swapped ← TRUE
ENDIF
NEXT Index
Limit ← Limit - 1
UNTIL Swapped = FALSE

Each pass carries the largest remaining value to the end
Put the steps of one bubble-sort pass, and its ending, in order.
難易度 3/5 · 200 コイン + 今日初回達成時の小ボーナス
Reset the flag, sweep and swap, shrink the limit, stop when a whole pass made no swap.
演習例:バブルソートのマーカー位置
- スキャンで交換が行われなくなるまで繰り返す外側ループ;
Swappedフラグは各スキャンの開始時にFALSEにリセットされ、IF内でTRUEに設定される。 - 一時変数を用いた3行の swap。2行目が値を失う。
- shrinking limit は、最大値が既に末尾にあるため、各スキャンごとに1ずつ減少する。
- 言葉で表すと、ステップワイズリファインメント問題において:整列するまで繰り返す;各スキャンで隣接ペアを比較する;順序が逆なペアがあれば交換する;各スキャン後、最大の不整値が末尾に来る。

Which features earn marks in an efficient bubble sort? Select すべて that apply.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Flag, swap with a temporary, shrinking limit: those are the marks. Copying the array is not part of the algorithm.
演習例:削除と挿入
- アイテムの削除: リニア検索でインデックスを見つける;その後の全要素を先頭方向へ1つずらして隙間を埋める;最後の要素を未使用としてマークするか、カウントを減らす。
- 整列された配列への挿入: 要素が大きい最初のインデックスを見つける;最後から始めて、その要素とそれ以降の全要素を末尾方向へ1つずらす;隙間に新しい値を格納する。
- 隙間を開く際は end 側から移動し、閉じる際は start 側から移動否则、動かそうとしている値を上書きしてしまう。

An array holds many items of the SAME type reached by index, while a record groups fields of (possibly) DIFFERENT types reached by name.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
A 2-D array suits a grid (rows × columns); a record suits one thing described by several named fields.
見落としやすいポイント
[0:99]は 100 個の要素を持つ。両端を含めて数える。- インデックスは
INTEGERです。宣言には境界だけでなく type も必要です。 Grid[row, column]: 行が先。これらを逆転させると、ネストループ内のすべてのセルが誤って参照される。- swap には一時変数が必要;search には "not found" パスが必要です;バブルソートは固定回数のスキャン後ではなく、スキャンで交換が行われなくなった時点で終了します。
理解できましたか
- 配列は、lower と upper bound の間の index でアクセスできる、同一型 要素 の固定数を一つの識別子の下に保持する;count = upper − lower + 1
- 1-D はリスト、2-D はネストループで遍历される表
[row, column];境界 and タイプで宣言する - linear search:
FoundAt ← -1、ループ、インデックスを格納、ループ後にテスト;最大値:A[1]から始め、位置を維持する - bubble sort: 一時変数を用いた隣接比較・スワップのスキャン、
Swappedフラグ、shrinking limit、スキャンで交換が行われなくなるまで
何が問題でしたか?この内容はサイトの運営チームだけが確認します。