Arrays and file handling
แนะนำใหม่
13:08
การเขียนโปรแกรม
นี่คือโปรแกรมสั้นๆ อ่านคะแนนทดสอบห้าตัวและพิมพ์ผลรวม คะแนนทั้งห้าคือแปด, เก้า, เจ็ด, หก และสิบ ดังนั้นผลรวมควรจะเป็นสี่สิบ ดูเลย…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| array/əˈreɪ/ | อาร์เรย์ |
| file/faɪl/ | A named collection of data stored and managed as a unit. |
| variable/ˈveərɪəbl/ | A quantity or named value that can change or take different values. |
| data/ˈdeɪtə/ | Recorded facts, observations or values used for analysis or processing. |
| Loops | The natural way to work through both. |
| One-dimensional arrays | |
| 1D array | |
| index/ˈɪndeks/ | ดัชนี |
| READ | |
| Two-dimensional arrays | |
| 2D array/ˌtuː ˈdiː əˈreɪ/ | |
| nested/ˈnestɪd/ | การซ้อนกัน |
| iteration/ˌɪtəˈreɪʃn/ | Repeating a sequence of instructions or calculations. |
| file handling/faɪl ˈhændlɪŋ/ | การจัดการไฟล์ |
| pseudocode/ˈsuːdəʊkəʊd/ | A language-independent description of an algorithm using structured, programming-like statements. |
| loop variable/luːp ˈveərɪəbl/ | |
| OPENFILE | |
| READFILE | |
| WRITEFILE | |
| CLOSEFILE |
Array และไฟล์
- Array เก็บหลายค่าของชนิดเดียวกันในตัวแปรเดียว
- File ทำให้โปรแกรมเก็บข้อมูลไว้ได้แม้โปรแกรมหยุดทำงาน
- Loop เป็นวิธีธรรมชาติในการเข้าถึงทั้งสองอย่าง
Array แบบหนึ่งมิติ
- 1D array เหมือนรายการเดียว; ค่าแต่ละค่าหาได้จาก index (ตำแหน่งตัวเลข)
DECLARE Scores : ARRAY[1:5] OF INTEGER
Scores[1] ← 90
FOR I ← 1 TO 5
INPUT Scores[I]
NEXT I
- Loop เติมหรืออ่านทั้ง array ได้

1D array เก็บหลายค่าในตัวแปรเดียว; แต่ละค่าหาได้ด้วย index ของมัน

An array is:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
An array stores many same-type values under one name; each is accessed by its index.
Array สองมิติ
- 2D array เหมือนตารางที่มี แถวและหลัก, ใช้ สอง indexes
[row, column]
DECLARE Grid : ARRAY[1:3, 1:3] OF INTEGER
Grid[2, 3] ← 8 // row 2, column 3
- คุณอ่าน 2D array ด้วย loop ใน loop (nested iteration)

2D array คือตาราง; ค่าแต่ละค่าหาได้ด้วยสอง indexes, (row, column)

Index a 2-D array: Grid[Row, Col]
A 2-D array is a grid of values. Grid[Row, Col] reaches exactly one cell — the row first, then the column. Cambridge counts both from 1.
In grid[2, 3], which cell is accessed?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The first index is the row, the second the column — row 2, column 3.
Match each idea to what it means.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Arrays hold many values by index; a 2-D array is a grid; nested loops walk it; files are opened, used and closed.
การจัดการไฟล์
- โปรแกรมสามารถเก็บข้อมูลใน file เพื่อให้คงอยู่ได้เมื่อโปรแกรมหยุดทำงาน
- Open ไฟล์, ใช้มัน, แล้ว close มัน:
OPENFILE "data.txt" FOR WRITE
WRITEFILE "data.txt", "Hello"
CLOSEFILE "data.txt"
- ต้อง close ไฟล์เมื่อใช้งานเสร็จ (ใช้
FOR READเพื่ออ่านกลับเข้ามา)

Counter นับเพิ่มขึ้นทีละ 1; Total สร้างผลรวมสะสม
Put the steps of using a file in the correct order.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Always open first, then do the reading/writing, then close to save buffered data and free the file.
You should always close a file when you have finished using it.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Closing saves any buffered data and frees the file for other programs.
คุณเข้าใจแล้ว
- 1D อาร์เรย์ คือรายการที่เข้าถึงได้ด้วยตัวชี้ตำแหน่งหนึ่งเดียว เติมหรืออ่านข้อมูลด้วยลูป
- 2D array เป็นตารางที่เข้าถึงได้ด้วย
[row, column]; ใช้ nested loops - การจัดการไฟล์: OPENFILE (READ/WRITE) → READFILE/WRITEFILE → CLOSEFILE
- ต้อง close ไฟล์เมื่อใช้งานเสร็จ
Scores มีไว้ 90 75 60 88 50 with the first index 1. What is Scores[4]?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Counting from index 1: 90, 75, 60, 88.
Data in an array is lost when the program stops, but data written to a file is kept.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Arrays live in RAM while the program runs; a file is saved to storage.
การ์ดนี้เป็นการ์ดใหม่ จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะพร้อม
You've got it
- a 1D array is a list reached by one index; fill/read it with a loop
- a 2D array is a table reached by
[row, column]; use nested loops - file handling: OPENFILE 打开文件 (READ/WRITE) → READFILE 读取文件/WRITEFILE 写入文件 → CLOSEFILE 关闭文件
- always close a file when done
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้