Arrays of Records

The program RecordArray demonstrates how an array of records may be declared, initialized and edited. It then processes data in the array of records and outputs the top theory mark and the student(s) that achieved it.

program RecordArray;
{$APPTYPE CONSOLE}
uses
  SysUtils;
type
  TStudent = record
    Forename, Surname : string;
    TheoryMark, PracticalMark : integer;
  end;
const
  MAX = 3;
var
  Students : array[1 .. MAX] of TStudent;
  Count, TopTheory : integer;
begin
  //initialization
  for Count := 1 to MAX do
    begin
      with Students[Count] do
        begin
          Forename := '';
          Surname :='';
          TheoryMark := 0;
          PracticalMark := 0;
        end;
    end;
  //data entry
  write('Which record would you like to edit, 1 to ', MAX, ' (100 to quit)? ');
  readln(Count);
  while Count <= MAX do
   begin
     writeln('Current contents of record ',Count);
     with Students[Count] do
       begin
         writeln('Forename: ', Forename);
         writeln('Surname: ', Surname);
         writeln('Theory Mark: ', TheoryMark);
         writeln('Practical Mark: ', PracticalMark);
         writeln;
         write('Forename? '); readln(Forename);
         write('Surname? ');  readln(Surname);
         write('Theory Mark? ');  readln(TheoryMark);
         write('Practical Mark? ');  readln(PracticalMark);
         writeln;
         write('Which record would you like to edit, 1 to ', MAX, ' (100 to quit)? ');
         readln(Count);
       end;
   end;
  //processing data in arrays of records
  TopTheory := Students[1].TheoryMark;
  for Count := 2 to MAX do
    begin
      if Students[Count].TheoryMark > TopTheory then
        begin
          TopTheory := Students[Count].TheoryMark;
        end;
    end;
  writeln('Top theory mark: ', TopTheory);
  writeln('Obtained by:');
  for Count := 1 to MAX do
    begin
      if Students[Count].TheoryMark = TopTheory then
        begin
          writeln(Students[Count].Forename, ' ', Students[Count].Surname);
        end;
    end;
  readln;
end.

Arrays of records can be searched and sorted, new records added in their correct position depending on some field, and records deleted. Common algorithms for achieving these tasks also apply to arrays of other data types and are covered thoroughly in the tutorials Lists, Stacks and Queues and Sorting and Searching.

Programming - a skill for life!

Single records and arrays and files of records