Build reports from records and text files
Choose a record for related fields
A product code is STRING because leading zeroes may matter. Its quantity is INTEGER because it is counted. A record keeps these fields under one identifier; an array stores several products.
Read files without losing the final line
Open for READ, test EOF before reading, process each successful read, then close. Convert a numeric STRING only after validation. An empty file is a legitimate test, not an error in the problem.
OPENFILE "demo.txt" FOR WRITE
WRITEFILE "demo.txt", "4"
WRITEFILE "demo.txt", "7"
CLOSEFILE "demo.txt"
DECLARE Line : STRING
DECLARE Total : INTEGER
Total ← 0
OPENFILE "demo.txt" FOR READ
WHILE NOT EOF("demo.txt") DO
READFILE "demo.txt", Line
Total ← Total + STR_TO_NUM(Line)
ENDWHILE
CLOSEFILE "demo.txt"
OUTPUT Total
This self-contained demonstration creates its own file. In a question where the file already exists, do not open it for WRITE: that would erase the data.
For the integer-line report, the starter supplies IsIntegerText. It accepts ASCII digits with an optional leading + or -; it rejects decimals, spaces, exponent notation and empty/sign-only lines. IS_NUM is broader: a string such as "1.5" is numeric but does not meet this task’s integer format.
Worked example: which product to reorder
Question: three products are stored as records. Output the code of the one with the lowest quantity, and how many are left.
TYPE Product
DECLARE Code : STRING
DECLARE Quantity : INTEGER
ENDTYPE
DECLARE Stock : ARRAY[1:3] OF Product
DECLARE I, Lowest : INTEGER
Stock[1].Code ← "0042"
Stock[1].Quantity ← 9
Stock[2].Code ← "0107"
Stock[2].Quantity ← 3
Stock[3].Code ← "0215"
Stock[3].Quantity ← 5
Lowest ← 1
FOR I ← 2 TO 3
IF Stock[I].Quantity < Stock[Lowest].Quantity THEN
Lowest ← I
ENDIF
NEXT I
OUTPUT "Reorder ", Stock[Lowest].Code, " (", Stock[Lowest].Quantity, " left)"
The output is Reorder 0107 (3 left). The loop keeps the index Lowest, not the quantity, because the answer also needs the code from the same record. It starts from record 1, which is real data, so no made-up starting value is needed. With <, a tie keeps the earlier product.
Common mistakes
Stock.Code[2]instead ofStock[2].Code. The index picks the record first, then the dot picks the field.- Declaring
Code : INTEGER. The code"0042"would become 42 and lose its zeroes. - Keeping only the lowest quantity. You then no longer know which code it belongs to.
- Calling
READFILEbefore testingEOF. On an empty file there is nothing to read. - Forgetting
CLOSEFILE. Every opened file is closed before the program ends.
Explain your choices
In the reasoning box, explain why product codes should not be INTEGER, why mixed fields suit a record, and why EOF is checked before READFILE. Then solve the two reports, including their empty/no-match cases.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
The checker supplies sales.txt. A valid integer line has one or more ASCII digits, optionally preceded by + or -, with no spaces. Ignore every other line, including decimals and exponent notation. Use the supplied IsIntegerText helper. Output the number of valid lines, then their integer total. Close the file.
לחץ על הרץ כדי לראות את התוצא כאן.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Read three products: a code, then an integer quantity, for each. Store them in an array of Product records. Output the code with the greatest quantity. If two are equal, output the earlier one.
לחץ על הרץ כדי לראות את התוצא כאן.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
Read Limit, then three product codes and integer quantities. Define TYPE Product and store an array of records. Output codes whose quantity is BELOW Limit, in input order; if none, output none.
לחץ על הרץ כדי לראות את התוצא כאן.
Explain your choice of data structure and loop. Identify one boundary case and predict its result. For a subroutine, explain what is returned or changed in the caller.
נשמר להצעה זו בדפדפן זה. עיין בעצמך או עם המורה; הסברים אלו אינם מסומנים אוטומטית.
עיין בהסבר שלך לפי הקריטריונים הבאים
- My types and data structure match the data and the question.
- I justified the loop and checked its first and last iterations.
- Initial values and rejected operations preserve the intended state.
- My test includes input, purpose and expected result.
- I distinguished returning a value from printing and changing caller state.