nilai.txt holds three students and their scores. The report prints three rows and every score is 0, then an average of 0.00. The program exits with status 0, so nothing flags a problem.
Key takeaways
- A procedure parameter without
varis a copy, so filling the array inside the procedure leaves the caller's array empty and the report prints zeros. - The program still exits with status 0, and
fpcwarnsVariable "data" does not seem to be initializedat the call that was supposed to fill it. varon the parameter hands the procedure the caller's array, so the records it fills survive the call.reset,eofandreadlnread the file, and:0:2is what turns the average into78.33instead of7.8333333333333329E+001.
The report that prints zeros
Save the data as nilai.txt: one student per line, name and score separated by a semicolon.
andi;78
budi;92
citra;65
The starter is nilai.pas. It defines a record for one student and an array type for the class. muat fills the array from the file and lapor prints it.
program nilai;
type
siswa = record
nama: string[20];
skor: integer;
end;
daftar = array[1..20] of siswa;
var
data: daftar;
jumlah: integer;
procedure muat(data: daftar; var jumlah: integer);
var
f: text;
baris: string;
pemisah, kode: integer;
begin
jumlah := 0;
assign(f, 'nilai.txt');
reset(f);
while not eof(f) do
begin
readln(f, baris);
pemisah := pos(';', baris);
jumlah := jumlah + 1;
data[jumlah].nama := copy(baris, 1, pemisah - 1);
val(copy(baris, pemisah + 1, length(baris)), data[jumlah].skor, kode);
end;
close(f);
end;
procedure lapor(data: daftar; jumlah: integer);
var
i, total: integer;
begin
total := 0;
for i := 1 to jumlah do
begin
writeln(data[i].nama, ' ', data[i].skor);
total := total + data[i].skor;
end;
writeln('rata-rata ', (total / jumlah):0:2);
end;
begin
muat(data, jumlah);
lapor(data, jumlah);
end.
The command below writes the data file, then compiles. -B rebuilds even when an old binary is lying around, and -vw keeps the compile quiet except for warnings.
$ printf 'andi;78\nbudi;92\ncitra;65\n' > nilai.txt && fpc -B -vw nilai.pas
nilai.pas(48,12) Warning: Variable "data" does not seem to be initialized
nilai.pas(48,12) names line 48, the call muat(data, jumlah), which is supposed to fill the array. The compiler can see that the array passed there never gets a value back.
The checker
check_nilai.pas reads the program's output from the pipe, one line at a time, and compares line i with harap[i]. Those are the four lines the file should produce: three students and the average. A matching line prints PASS. A different line prints FAIL with the expected text and the text it got. Output with more than four lines, or fewer, fails too. The last line reports how many of the four passed, and ExitCode is set to 1 when any failed.
program check_nilai;
var
harap: array[1..4] of string = ('andi 78', 'budi 92', 'citra 65', 'rata-rata 78.33');
baris: string;
i, gagal: integer;
begin
i := 0;
gagal := 0;
while not eof(input) do
begin
readln(baris);
i := i + 1;
if i > 4 then
begin
writeln('FAIL baris ', i, ': keluaran lebih panjang dari yang diharapkan');
gagal := gagal + 1;
end
else if baris = harap[i] then
writeln('PASS baris ', i, ': ', baris)
else
begin
writeln('FAIL baris ', i, ': harap "', harap[i], '", dapat "', baris, '"');
gagal := gagal + 1;
end;
end;
if i < 4 then
begin
writeln('FAIL cuma ', i, ' baris, seharusnya 4');
gagal := gagal + 1;
end;
writeln(4 - gagal, ' of 4 checks passed');
if gagal > 0 then
ExitCode := 1;
end.
The starter fails all four lines.
$ fpc -vw check_nilai.pas && ./nilai | ./check_nilai
FAIL baris 1: harap "andi 78", dapat " 0"
FAIL baris 2: harap "budi 92", dapat " 0"
FAIL baris 3: harap "citra 65", dapat " 0"
FAIL baris 4: harap "rata-rata 78.33", dapat "rata-rata 0.00"
0 of 4 checks passed
A copy went in, a copy came out
procedure muat(data: daftar; var jumlah: integer) has var on jumlah and no var on data. A parameter without var is passed by value, which in Pascal means the procedure gets its own copy of the array. It fills that copy with the three students, closes the file, and returns. The copy is discarded when muat returns.
jumlah did come back, because it carries var. That is why lapor prints three rows: the count is real and the rows behind it are empty. Each row prints an empty name and a zero score, which shows up as 0 with a leading space. With every score at 0, the total is 0 and the average prints as 0.00. Meanwhile the data in the main program was never written to, which is exactly what the warning said.
One word on the parameter
Put var in front of data in muat. The procedure now receives the caller's array instead of a copy of it.
program nilai;
type
siswa = record
nama: string[20];
skor: integer;
end;
daftar = array[1..20] of siswa;
var
data: daftar;
jumlah: integer;
procedure muat(var data: daftar; var jumlah: integer);
var
f: text;
baris: string;
pemisah, kode: integer;
begin
jumlah := 0;
assign(f, 'nilai.txt');
reset(f);
while not eof(f) do
begin
readln(f, baris);
pemisah := pos(';', baris);
jumlah := jumlah + 1;
data[jumlah].nama := copy(baris, 1, pemisah - 1);
val(copy(baris, pemisah + 1, length(baris)), data[jumlah].skor, kode);
end;
close(f);
end;
procedure lapor(data: daftar; jumlah: integer);
var
i, total: integer;
begin
total := 0;
for i := 1 to jumlah do
begin
writeln(data[i].nama, ' ', data[i].skor);
total := total + data[i].skor;
end;
writeln('rata-rata ', (total / jumlah):0:2);
end;
begin
muat(data, jumlah);
lapor(data, jumlah);
end.
The warning is gone and the checker passes. Every line matches, so the checker never sets ExitCode to 1 and prints 4 of 4 checks passed.
$ fpc -B -vw nilai.pas && ./nilai | ./check_nilai
PASS baris 1: andi 78
PASS baris 2: budi 92
PASS baris 3: citra 65
PASS baris 4: rata-rata 78.33
4 of 4 checks passed
lapor still takes data without var, and that is correct. It only reads the array, so working on a copy changes nothing it prints.
When the average prints in scientific notation
Delete the :0:2 from the writeln in lapor and run the checker again. The first three lines still pass and the fourth fails with rata-rata 7.8333333333333329E+001. A real printed without a format string goes out in scientific notation, and it keeps a second space after the label. The checker catches this because it compares whole lines, and rata-rata 78.33 is not that line.
:0:2 means no minimum width and two decimals. The integer scores need no field at all, because an integer already prints without decimals.
When the file is missing
Move nilai.txt aside and run the program. reset on a file that is not there stops the program with Runtime error 2 and exit status 2, before any row is printed. The lines under that error are addresses inside your own binary.
The capstone does not handle a missing file, because the file is the input the program was written for. When fpc warns that a variable does not seem to be initialized, check the line it names first: a procedure that needs to fill a parameter requires var.