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 |
المصفوفات والملفات
- المصفوفة تخزن العديد من القيم من نفس النوع في متغير واحد.
- الملف يسمح للبرنامج بالحفاظ على البيانات بعد توقفه.
- الحلقات هي الطريقة الطبيعية للعمل عبر كليهما.
مصفوفات أحادية البعد
- مصفوفة 1D تشبه قائمة واحدة؛ يتم العثور على كل قيمة بواسطة مؤشر (رقم موقع).
DECLARE Scores : ARRAY[1:5] OF INTEGER
Scores[1] ← 90
FOR I ← 1 TO 5
INPUT Scores[I]
NEXT I
- حلقة تملأ أو تقرأ المصفوفة بأكملها.

مصفوفة 1D تحتفظ بالعديد من القيم في متغير واحد؛ يتم العثور على كل منها بمؤشرها.
An array is:
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
An array stores many same-type values under one name; each is accessed by its index.
مصفوفات ثنائية الأبعاد
- مصفوفة 2D تشبه جدولاً به صفوف وأعمدة، تستخدم مؤشرين
[row, column].
DECLARE Grid : ARRAY[1:3, 1:3] OF INTEGER
Grid[2, 3] ← 8 // row 2, column 3
- تقرأ مصفوفة 2D باستخدام حلقة داخل حلقة (تكرار متداخل).

مصفوفة 2D هي جدول؛ يتم العثور على كل قيمة بمؤشرين، (صف، عمود).
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.
التعامل مع الملفات
- يمكن للبرنامج تخزين البيانات في ملف بحيث تبقى موجودة بعد توقف البرنامج.
- افتح الملف، استخدمه، ثم أغلقه:
OPENFILE "data.txt" FOR WRITE
WRITEFILE "data.txt", "Hello"
CLOSEFILE "data.txt"
- always أغلق الملف عند الانتهاء (استخدم
FOR READلقراءته مرة أخرى).

عداد يعد تصاعدياً بمقدار 1؛ المجموع يبني مجموعاً تراكمياً
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.
هذه البطاقة جديدة، لذلك تظهر باللغة الإنجليزية حتى تصبح ترجمتها جاهزة.
Worked example: the highest score in an array
Scores[1:5] holds 90 75 60 88 50. Write pseudocode 伪代码 to find the highest score.
Max ← Scores[1]
FOR Index ← 2 TO 5
IF Scores[Index] > Max
THEN
Max ← Scores[Index]
ENDIF
NEXT Index
OUTPUT Max
Maxstarts as the first element, 90. Each later element is compared with it: 75, 60, 88 and 50 are all smaller, soMaxstays 90 and 90 is output.- The loop variable 循环变量
Indexis the index. In a 2D array such asGrid[2, 3], the first index is the row and the second is the column.
![Scores 90, 75, 60, 88, 50: Max starts at Scores[1] = 90 and no later score is bigger, so Max stays 90](/learn-media/igcse_computer_science/assets/0803-max.png?v=1795984513)
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.
لقد فهمت الأمر
- مصفوفة 1D هي قائمة يتم الوصول إليها بمؤشر واحد؛ املأها/اقرأها بحلقة
- مصفوفة 2D هي جدول يتم الوصول إليها بـ
[row, column]؛ استخدم حلقات متداخلة - التعامل مع الملفات: OPENFILE (READ/WRITE) → READFILE/WRITEFILE → CLOSEFILE
- always أغلق الملف عند الانتهاء
ما المشكلة؟ لا يرى ذلك سوى فريق الموقع.