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
DECLARE an array
- An array stores many values under one name.
DECLARE Score : ARRAY[1:5] OF INTEGERmakes room for 5 integers.- The numbers
1:5are the first and last index.
ไทย
DECLARE แอลเลย์
- แอลเลย์ เก็บหลายค่าภายใต้ชื่อเดียว
DECLARE Score : ARRAY[1:5] OF INTEGERสร้างพื้นที่สำหรับ integer 5 ตัว- ตัวเลข
1:5คือ index แรกและสุดท้าย
DECLARE Score : ARRAY[1:5] OF INTEGER
Score[1] ← 40
OUTPUT Score[1] // → 40
English
Arrays count from 1
- The first item is
Score[1], the last isScore[5]. - (This is different from Python and JavaScript, which count from 0.)
ไทย
แอลเลย์นับจาก 1
- รายการแรกคือ
Score[1], รายการสุดท้ายคือScore[5] - (สิ่งนี้ต่างจาก Python และ JavaScript ที่นับจาก 0)
DECLARE Name : ARRAY[1:3] OF STRING
Name[1] ← "Ada"
Name[2] ← "Sam"
OUTPUT Name[2] // → Sam
English
Loop through an array
- A
FORloop visits each index in turn.
ไทย
ลูปผ่านแอลเลย์
- ลูป
FORจะเข้าถึงแต่ละดัชนีตามลำดับ
DECLARE Score : ARRAY[1:3] OF INTEGER
Score[1] ← 10
Score[2] ← 20
Score[3] ← 30
FOR I ← 1 TO 3
OUTPUT Score[I] // 10, 20, 30
NEXT I
English
Watch out
- The index must stay inside the declared range:
Score[6]is out of[1:5]. ARRAY[1:5]has 5 cells, numbered 1 to 5 — not 0 to 4.
ไทย
ระวัง
- Index ต้องอยู่ในช่วงที่ประกาศ:
Score[6]อยู่นอก[1:5] ARRAY[1:5]มี 5 เซลล์, numbering 1 ถึง 5 — ไม่ใช่ 0 ถึง 4
English
Common mistakes
- Declare an array with its bounds:
DECLARE A : ARRAY[1:N] OF INTEGER. - Index only within the declared range.
ไทย
ข้อผิดพลาดที่พบบ่อย
- ประกาศแอลเลย์พร้อมขอบเขต:
DECLARE A : ARRAY[1:N] OF INTEGER - Index เฉพาะในช่วงที่ประกาศ
English
Now you try
- Declare an array, fill it, then read or loop over it.
- Press Check answer to test it.
ไทย
ลองดูเลย
- ประกาศแอลเลย์, เติมข้อมูล, แล้วอ่านหรือลูปผ่านมัน
- กด Check answer เพื่อทดสอบ
Declare an array A of 3 integers. Store 10, 20, 30 in positions 1, 2, 3, then output A[2] (the answer is 20). · ประกาศอาร์เรย์ A ขนาด 3 จำนวนเต็ม. เก็บ 10, 20, 30 ในตำแหน่ง 1, 2, 3, แล้วแสดงผล A[2] (คำตอบคือ 20)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
The array A holds 5, 8, 2. Use a FOR loop to add them and output the total (it is 15). · อาร์เรย์ A เก็บ 5, 8, 2. ใช้ลูป FOR เพื่อรวมค่าทั้งหมดและแสดงผลรวม (คือ 15)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
The array A holds 3, 9, 2, 7. Find the largest value and output it (it is 9). · อาร์เรย์ A เก็บ 3, 9, 2, 7. หา ค่าสูงสุด และแสดงผล (คือ 9)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่