Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* Mistake: I have uploaded this code as a guest, because of whatever reason the browser forgot my credentials, so I was not logged on... stupid thing. *)
- {$mode objfpc}
- program students;
- uses sysutils, strutils;
- const DIM = 20;
- FNAME = 'teste';
- TAB = ' ';
- type TArrayInt = array of smallint;
- TFileInt = file of smallint;
- var grades, negative_grades : TArrayInt;
- n : smallint;
- GradeFile : TFileInt;
- procedure FillArray(var arr : TArrayInt; dimension : longint; const prompt : string);
- begin
- setlength(arr, dimension-1);
- for dimension := low(arr) to high(arr) do begin
- write(AnsiReplaceText(prompt, '@{COUNT}', IntToStr(dimension)));
- readln(arr[dimension]);
- end;
- end;
- procedure CopyArrayToFile(const arr : TArrayInt; out f : TFileInt);
- var elem : smallint;
- begin
- for elem in arr do
- write(f, elem);
- end;
- procedure GetArrayFromFile(var f : TFileInt; out arr : TArrayInt);
- var i : smallint = 0;
- begin
- seek(f,0);
- SetLength(arr, FileSize(f));
- while not eof(f) do begin
- read(f, arr[i]);
- inc(i);
- end;
- end;
- function CountPositive(arr : TArrayInt; const minimum : smallint = 0) : smallint;
- var elem : smallint;
- begin
- CountPositive := 0;
- for elem in arr do
- if elem >= minimum then
- inc(CountPositive);
- end;
- function CountPositive(var f : TFileInt; const minimum : smallint = 0) : smallint; overload;
- var arr : TArrayInt;
- begin
- GetArrayFromFile(f, arr);
- CountPositive := CountPositive(arr, minimum);
- end;
- procedure GetNegative(arr : TArrayInt; out neg : TArrayInt; const minimum : smallint = 0);
- var elem : smallint;
- begin
- for elem in arr do
- if elem < minimum then begin
- SetLength(neg, length(neg)+1);
- neg[length(neg)-1] := elem;
- end;
- end;
- procedure GetNegative(var f : TFileInt; out neg : TArrayInt; const minimum : smallint = 0); overload;
- var arr : TArrayInt;
- begin
- GetArrayFromFile(f, arr);
- GetNegative(arr, neg, minimum);
- end;
- function Average(arr : TArrayInt) : real;
- var elem : smallint;
- begin
- Average := 0.0;
- for elem in arr do
- Average := Average + elem;
- Average := Average / length(arr);
- end;
- function Average(var f : TFileInt) : real;
- var arr : TArrayInt;
- begin
- GetArrayFromFile(f, arr);
- Average := Average(arr);
- end;
- begin
- assign(GradeFile, FNAME);
- FillArray(grades, 5, 'Write the @{COUNT} grade: ');
- rewrite(GradeFile);
- CopyArrayToFile(grades, GradeFile);
- close(GradeFile);
- reset(GradeFile);
- writeln('There is/are ', CountPositive(GradeFile), ' positive grades.');
- GetNegative(GradeFile, negative_grades, 10);
- writeln('Negative grades:');
- for n in negative_grades do
- writeln(TAB, n);
- writeln('Average = ', Average(GradeFile));
- close(GradeFile);
- readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment