F5 Pascal Programming Revision – 27 Mar 2002 LAB : Game of Life 3 - readin the file 'abc.txt' 'o' means a living cell '-' means a dead cell count the no. of living neighbour of each cell for a dead cell, it will become alive in next generation, if it has exactly 3 living neighbours for a living cell, it will become dead in next generation, if it has less than 2 / more than living neighbours for all other conditions, the cell status will remain unchange write a program to display generation by generation Hints 1) use a 2-D array to store the existing life configuration 2) use a 2-D array to store the life config of next generation ANSWER program life(input, output); const maxrow=8; maxcol=10; var ifile : text; map, nextmap : array[1..maxrow,1..maxcol] of boolean; {T:alive, F:dead} again:char; row,col:integer; generation : integer; procedure Initialize; var i,j : integer; begin for i:=1 to maxrow do for j:=1 to maxcol do nextmap[i, j]:=false; end; procedure ReadMap; var tmp : string; i,j : integer; begin assign(ifile, 'abc.txt'); reset(ifile); for i:=1 to maxrow do begin readln(ifile,tmp); for j:=1 to maxcol do if tmp[j]='o' then map[i,j]:=true else map[i,j]:=false; end; close(ifile); end; {end of READMAP} function nbrcount(i,j:integer):integer; var x,x1,x2,y,y1,y2,count:integer; begin count := 0; x1:=i-1; x2:=i+1; y1:=j-1; y2:=j+1; P.1 of 5 F5 Pascal Programming Revision – 27 Mar 2002 if i=1 then x1:=1; if i=maxrow then x2:=maxrow; if j=1 then y1:=1; if j=maxcol then y2:=maxcol; for x := x1 to x2 do for y := y1 to y2 do if (map[x,y]=true) then count:= count+1; if map[i,j]=true then count:=count-1; nbrcount:=count; end; procedure WriteMap; var i,j, count: integer; begin for i:=1 to maxrow do begin for j:=1 to maxcol do if (map[i,j]=true) then write ('o') else write ('-'); writeln; end; writeln; end; begin Initialize; ReadMap; writeln('Initial Life Configuration'); WriteMap; generation:=1; repeat for row:= 1 to maxrow do for col:= 1 to maxcol do case nbrcount(row,col) of 0,1 : nextmap[row,col]:=false; 2 : nextmap[row,col]:=map[row,col]; 3 : nextmap[row,col]:=true; 4,5,6,7,8 : nextmap[row,col]:=false; end; map:=nextmap; writeln(generation, ' Generation'); WriteMap; Readln(again); generation:=generation+1; until (again='N'); end. P.2 of 5 F5 Pascal Programming Revision – 27 Mar 2002 LAB : SORTING AND SEARCHING - readin the files ‘F5A.txt’, ‘F5B.txt’, ‘F5C.txt’, ‘F5D.txt’, ‘F5E.txt’ - store the data in five 2-D arrays F5A, F5B, ..., F5E : array [1..10, 1..2] of string; - write a SORTING procedure which accept two parameters : X, ClassX X : 1 (for sort by name), 2 (for sort by score) ClassX can be F5A, F5B... F5E - write a SEARCHING procedure which accept three parameters : X, infoX, ClassX X : 1 (for search by name), 2 (for search by score) infoX : the value to be searched ClassX can be F5A, F5B,... F5E - display the result ANSWER program sortsearch(input, output); type classtype = array[1..10, 1..2] of string; var F5A, F5B, F5C, F5D, F5E : classtype; infile : text; choice : char; class : string; n : integer; info : string; procedure READIN(var classX : classtype); var i:integer; begin for i := 1 to 10 do begin readln(infile, classX[i,1]); readln(infile, classX[i,2]); end; end; procedure SORT(X:integer; var classX : classtype); {perform bubble sort on classX} {X = 1 means sort by name } { = 2 means sort by score} var pass, i : integer; temp : string; begin for pass := 1 to 9 do for i := 1 to (10-pass) do if (CONDITION) then begin SWAP end; for i := 1 to 10 do writeln(classX[i,1]:20,classX[i,2]:5); end; {refer to textbook Pascal Vol 2 for appropriate statements for CONDITION & SWAP} procedure SEARCH(X:integer; infoX:string; var classX : classtype); {perform binary search on classX} {X = 1 means search by name } { = 2 means search by score} var found : boolean; i, j, k : integer; begin P.3 of 5 F5 Pascal Programming Revision – 27 Mar 2002 found := false; i := 1; j := 10; repeat k := (i+j) div 2; if infoX = classX[k,X] then found := true else if infoX < classX[k,X] then j := k-1 else i := k+1; until found or (i>j); if found then writeln('SEARCH successful : ', classX[k,1], ' else writeln('SEARCH failed'); end; begin assign(infile, reset(infile); READIN(F5A); close(infile); assign(infile, reset(infile); READIN(F5B); close(infile); assign(infile, reset(infile); READIN(F5C); close(infile); assign(infile, reset(infile); READIN(F5D); close(infile); assign(infile, reset(infile); READIN(F5E); close(infile); ', classX[k,2]) 'F5A.txt'); 'F5B.txt'); 'F5C.txt'); 'F5D.txt'); 'F5E.txt'); repeat write('Enter (1) for sort, (2) for search, (x) for exit : '); readln(choice); case choice of '1': begin write('Enter the class to sort (e.g. F5A) : '); readln(class); write('Enter (1) sort by name, (2) sort by score : '); readln(n); if class='F5A' then SORT(n, F5A) else if class='F5B' then SORT(n, F5B) else if class='F5C' then SORT(n, F5C) else if class='F5D' then SORT(n, F5D) else if class='F5E' then SORT(n, F5E); end; '2': begin write('Enter the class to search (e.g. F5A) : '); readln(class); write('Enter (1) search by name, (2) search by score : '); readln(n); write('Enter the information to search : '); P.4 of 5 F5 Pascal Programming Revision – 27 Mar 2002 readln(info); if class='F5A' then else if class='F5B' else if class='F5C' else if class='F5D' else if class='F5E' end; SEARCH(n, info, F5A) then SEARCH(n, info, then SEARCH(n, info, then SEARCH(n, info, then SEARCH(n, info, end; until (choice='X') or (choice='x') end. P.5 of 5 F5B) F5C) F5D) F5E);