{-------------------------------------------------} { This program shows how to define and use arrays } { in procedures, pass array as parameters, how to } { use file. } { Author : Xiannong Meng } { Date : April 29, 1998 } {-------------------------------------------------} program ArrayParam; uses WinCrt; { uses Windows input and output } type ScoreType = 0..100; ScoreArray = array[1..30]of ScoreType; { Example of copy one array into another } { Parameters: } { source : original array } { dest : destination array } { size : size of the array } procedure ArrayCopy( source : ScoreArray; var dest : ScoreArray; size : integer); var i : integer; { loop index } begin for i := 1 to size do dest[i] := source[i]; { alternatively, dest := source; } end; { This procedure reads information from a file } { and stores them in an array. } { Parameters: } { info : array where information is stored } { size : size of the array } procedure ReadInfo(var info : ScoreArray; var size : integer); var f : text; { text file } i : integer; { loop index } begin assign(f,'user.dat'); reset(f); { open file for reading } { The first line of the file should be a count } { of how many integers to come, followed by one } { integer per line. } { If we don't know the size, we have to do this:} { while not eof(f) do ... } readln(f,size); for i := 1 to size do readln(f,info[i]); close(f); end; { This procedure writes information to a file } { from an array. } { Parameters: } { info : array where information is stored } { size : size of the array } procedure WriteInfo(info : ScoreArray; size : integer); var f : text; { text file } i : integer; { loop index } begin assign(f,'user.new'); rewrite(f); { open file for writing } { The first line of the file should be a count } { of how many integers to come, followed by one } { integer per line. } writeln(f,size); for i := 1 to size do writeln(f,info[i]); close(f); end; { this procedure display the contents of the array, } { both the original and the copy. } procedure Display( orig, copy : ScoreArray; size : integer); var i : integer; { loop index } begin { Display } writeln(' this is the contents of original array'); for i := 1 to size do writeln(orig[i]); writeln(' this is the contents of copied array'); for i := 1 to size do writeln(copy[i]); end; { Display } var count : integer; { to store the size of the array } Scores, Scores2 : ScoreArray; { actual arrays } begin ReadInfo(Scores,count); { read info. from file and store them in Scores } ArrayCopy(Scores,Scores2,count); { copy from Scores to Scores2 } Display(Scores,Scores2,count); { display both arrays } WriteInfo(Scores2,count); { write Scores2 to a new file } end.