1-D arrays · 数组 1-D
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
English
A list of the same type
- An array holds a fixed number of values of one type.
- The guide's lower bound is 1 unless a question says otherwise.
- One index, in square brackets, picks an element.
ไทย
รายการที่มีชนิดเดียวกัน
- Array เก็บค่าจำนวนคงที่ของชนิดเดียว
- ค่าต่ำสุด (lower bound) ของคู่มือนี้คือ 1 เว้นแต่โจทย์จะระบุไว้ differently
- ใช้ index เดียวในวงเล็บเหลี่ยมเพื่อเลือกองค์ประกอบ
English
ไทย
DECLARE Score : ARRAY[1:5] OF INTEGER
Score[1] ← 4
Score[2] ← 8
OUTPUT Score[1]
English
A loop visits every cell
- A
FORloop from the lower bound to the upper bound is how you fill or read the array.
ไทย
ลูปเข้าเยี่ยมทุกเซลล์
- การใช้
FORloop จาก lower bound ถึง upper bound คือวิธีเติมหรืออ่านข้อมูลใน array
DECLARE Score : ARRAY[1:3] OF INTEGER
Score[1] ← 4
Score[2] ← 8
Score[3] ← 6
DECLARE Total : INTEGER
Total ← 0
FOR I ← 1 TO 3
Total ← Total + Score[I]
NEXT I
OUTPUT Total
English
Watch out
Score[1]is the first cell when the array was declared[1:5].- The index can be an expression:
Score[I + 1].
ไทย
ระวัง
Score[1]คือเซลล์แรกเมื่อ array ถูกประกาศว่า[1:5]- Index สามารถเป็น expression ได้:
Score[I + 1]
English
Common mistakes
- Declare the array before you store into a cell.
- The index must stay inside the declared bounds.
ไทย
ข้อผิดพลาดที่พบบ่อย
- ประกาศ array ก่อนที่จะเก็บค่าลงในเซลล์
- Index ต้องอยู่ภายในขอบเขตที่ประกาศไว้
English
Now you try
- Store three numbers and total them.
ไทย
ลองดูเลย
- เก็บสามตัวเลขแล้วรวมผลลัพธ์
Declare Score as ARRAY[1:3] OF INTEGER. Store 4 in cell 1 and output that cell. · ประกาศ Score เป็น ARRAY[1:3] OF INTEGER. เก็บค่า 4 ในเซลล์ 1 และแสดงเซลล์นั้น
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
The array is filled for you. Total the three values and output the total (18). · Array ถูกเติมให้แล้ว. รวมสามค่าและเอาต์พุตผลรวม (18)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่