ReadAndSum3



Program ReadSum3;

{ This program extends the program ReadAndSum2 by not only
  reading the five values to sum from a file and
  also a Name (which comes before the number list in
  the file) but it can read an arbitrary number of such records.

  It also uses a Header procedure and outputs to a file.

  IMPORTANT: The file 'rs_data3.txt' MUST EXIST on the a: drive.
  See procedure ReadFive for file data format details.
}

type
   tp_Array5 = array[1..5] of real;  { array of 5 numbers }
   tp_String40 = string[40];         { our name string TYPE }

var
   infile  : text;
   outfile : text;
   name    : tp_String40;
   nums    : tp_Array5;
   the_sum : real;


{ ----------------------------------------------------------------
  ReadFive
  Read first a NAME into name and then five numbers (of type real)
  into num_list[] from the file assigned to in_file.
  Note we assume the name is the first thing in the file, the name
  is on its own line and each number is on a separate line.
  ---------------------------------------------------------------- }

procedure ReadFive(var in_file : text;
                   var num_list : tp_Array5; var name : tp_String40);
var
   i : integer;    { i is a local variable of ReadFive }

begin

   { first we read the name }
   readln(in_file, name);

   { then we go after the numbers }
   i := 1;
   while (i <= 5) do
   begin
      readln(in_file, num_list[i]);
      i := i + 1;
   end; { while }

end; { ReadFive }


{ ----------------------------------------------------------------
  CalcSum
  Calculate the sum of the numbers in num_list[]
  ---------------------------------------------------------------- }

procedure CalcSum(num_list : tp_Array5; var sum : real);
var
   i : integer;    { i is a local variable of CalcSum }

begin
   sum := 0.0;   { initialize sum to be zero }

   i := 1;
   while (i <= 5) do
   begin
      sum := sum + num_list[i];
      i := i + 1;
   end; { while }

end; { CalcSum }


{ ----------------------------------------------------------------
  Header
  Print a nice header to our output file.
  ---------------------------------------------------------------- }

procedure Header(VAR out : TEXT);
begin

   writeln(out, 'Name                      Sum');
   writeln(out, '------------------------- --------');

end; { Header }


{ ----------------------------------------------------------------
  main program begins
  ---------------------------------------------------------------- }

begin
   { First we must open the file for reading }
   assign(infile, 'a:rs_data3.txt');
   reset(infile);
   assign(outfile, 'a:rs_out3.txt');
   rewrite(outfile);

   Header(outfile);

   while (NOT eof(infile)) DO
   Begin
      ReadFive(infile, nums, name);
      CalcSum(nums, the_sum);

      writeln(outfile, name:25-length(name),' ':26-length(name), the_sum:0:2);  { may need to format this a better way }
   End;

   { always be sure to close the files }
   close(outfile);
   close(infile);

end. { main program }



Here is a file to test the above program on - be sure to put it on a floppy in the a:\ drive - or alter the assign statements in the program.