[fpc-pascal] More Filter Madness

Vincent Snijders vincent.snijders at gmail.com
Fri Oct 14 08:49:36 CEST 2011


2011/10/14 Ko Hashiguchi <ko.d.hashiguchi at gmail.com>:
> Mine is more of a programming problem, rather than one native to Pascal, but
> here goes:
>
> I have a text file with many Double values. Looking more or less like below,
> but with hundreds of entries...
>
> 1.5
> 3.25
> 7.54
> 10.33
> 2.22
>
> The values listed are only for illustration. What I need to do is read the
> values in the first file and sum them up such that the output file starts
> with the first value, the second entry in the output file is the sum of the
> first AND second values, the third entry in the output file is the sum of
> the first, second and third values of the input file:

var
  infile: text;
  outfile: text;
  sum: double;
  value: double;

begin
  assign(infile, ...);
  assign(outfile, ...);
  reset(infile);
  rewrite(outfile);
  sum := 0;
  while not eof(infile) do begin
    readln(infile, value);
    sum := sum + value'
    writeln(outfile, sum);
  end;
end.

Depending on how many numbers you read and the difference between
magnitude of sum and value, you may have to do some extra work for the
numeric stability. but that is for later concern.

Vincent



More information about the fpc-pascal mailing list