prev
PROGRAM TWENTY_FIVE
Implement a Pascal program which allows the recalling of a group of student marks. The program is to output the highest and lowest marks, as well as the mean.

Use an array of records to store the names and marks. Using an output file, sort the student names, marks into ascending order, so that the student with the highest mark will be written first.

The details are,

     Student 1      Joe Bloggs      56
             2      Bill Anderson   24
             3      William Tell    78
             4      Bob Crane       23
             5      Peter Hall      57
             6      Charles French  76
             7      Bryan Goldwater 65
             8      Stewart Phelps  89
             9      Dave Stevens    78
            10      Ted Rosse       64

The student name consists of 16 characters, and the student mark is an integer in the range 0 to 100. Our example has a maximum of ten students.

program prog25A (input,output,outfile);   {create student file}
const outname = 'STUDENT.DAT';
type  student = record
                 name : string[16];
                 mark : integer;
                end;
var class : array [1..10] of student;
    loopcount : integer;
    outfile : file of student;
begin
    class[1].name := 'Joe Bloggs      '; class[1].mark := 56;
    class[2].name := 'Bill Anderson   '; class[2].mark := 24;
    class[3].name := 'William Tell    '; class[3].mark := 78;
    class[4].name := 'Bob Crane       '; class[4].mark := 23;
    class[5].name := 'Peter Hall      '; class[5].mark := 57;
    class[6].name := 'Charles French  '; class[6].mark := 76;
    class[7].name := 'Bryan Goldwater '; class[7].mark := 65;
    class[8].name := 'Stewart Phelps  '; class[8].mark := 89;
    class[9].name := 'Dave Stevens    '; class[9].mark := 78;
    class[10].name := 'Ted Rosse       '; class[10].mark := 64;

{ for turbo pascal  assign( outfile, outname ); rewrite( outfile ); }
    rewrite( outfile, outname );
    for loopcount := 1 to 10 do
       write( outfile, class[loopcount] );
    writeln('Student.dat created and written.');
    close( outfile )
end.


program prog25B (input,output,infile); {read back in student file} const inname = 'STUDENT.DAT'; type student = record name : string[16]; mark : integer; end; var class : array [1..10] of student; loopcount, classsize : integer; infile : file of student; begin { for turbo pascal assign( infile, inname ); reset( infile ); } rewrite( infile, inname ); classsize := 1; while not eof(infile) do begin read( infile, class[classsize] ); classsize := classsize + 1 end; for loopcount := 1 to ( classsize - 1 ) do begin write('Student ',loopcount:2,' is ' ); writeln(class[loopcount].name,' ',class[loopcount].mark) end; close( infile ) end.
{read back, sort, write, student file} program prog25C (input, output, infile, outfile); const inname = 'STUDENT.DAT'; outname = 'STUDENT.SRT'; type student = record name : string[16]; mark : integer; end; class = array [1..10] of student; { find highest mark } function gethighest(studclass : class; sizeclass : integer) : integer; var temp, count : integer; begin temp := studclass[1].mark; count := 2; while count <= sizeclass do begin if studclass[count].mark > temp then temp := studclass[count].mark; count := count + 1 end; gethighest := temp; end; { find lowest mark } function getlowest(studclass : class; sizeclass : integer) : integer; var temp, count : integer; begin temp := studclass[1].mark; count := 2; while count <= sizeclass do begin if studclass[count].mark < temp then temp := studclass[count].mark; count := count + 1 end; getlowest := temp; end; { find mean } function getmean ( studclass : class; sizeclass : integer ) : real; var total, loop : integer; begin total := 0; for loop := 1 to sizeclass do total := total + studclass[loop].mark; getmean := total / sizeclass; end; { sort into ascending order, standard sequential sort used here } procedure sort( var studclass : class; sizeclass : integer ); var temp : student; loop, base, index : integer; begin base := 1; while base < sizeclass do begin index := base + 1; while index <= sizeclass do begin if studclass[base].mark < studclass[index].mark then begin temp.mark := studclass[base].mark; temp.name := studclass[base].name; studclass[base].name := studclass[index].name; studclass[base].mark := studclass[index].mark; studclass[index].name := temp.name; studclass[index].mark := temp.mark end; index := index + 1 end; base := base + 1 end; end; var mainclass : class; loopcount, classsize, highest, lowest : integer; mean : real; infile, outfile : file of student; begin { for turbo pascal assign( infile, inname ); reset ( infile ); assign( outfile, outname); rewrite(outfile); } reset( infile, inname ); rewrite(outfile, outname); classsize := 1; while not eof(infile) do begin read( infile, mainclass[classsize] ); classsize := classsize + 1 end; close( infile ); { find highest, lowest and average marks } highest := gethighest( mainclass, classsize - 1 ); lowest := getlowest ( mainclass, classsize - 1 ); mean := getmean ( mainclass, classsize - 1 ); { now sort into ascending order } sort( mainclass, classsize - 1 ); { now write out sorted class to outfile } for loopcount := 1 to ( classsize - 1 ) do write(outfile,mainclass[loopcount]); writeln('The highest mark was ', highest ); writeln('The lowest mark was ', lowest ); writeln('The mean mark was ', mean:3:2); close( outfile ) end.

Copyright B Brown/P Henry/CIT, 1988-1997. All rights reserved.
prev 1