From: mabht@levels.unisa.edu.au
Newsgroups: unisa.cis.dslss
Subject: Chapter 3 Answers
Date: 3 Sep 92 13:36:44 +0930
Organization: University of South Australia

Chaper 3 Answers

11)
	Error 1   Temp^ := Ptr^     { Temp is undefined }
	          Add New ( Temp )

	Error 2 The first writeln should be
		  Writeln (Ptr^,Temp^);

23)
	a)	4 bytes
	b)	None
	c)	3600 bytes
	d)	StudentList^[1].ID := 100;
	e)	For Index := 1 to 100 do
i			Writeln (studentlist^[Index].ID);
	f)      for Index := 1 to 100 do
			begin
			with studentlist^[index] do
				end;
				totalhours := totalhours + currenthours;
				currenthours := 0;
				end;
			end;
	
32)

a)  The specification

readfixedstring (data, number, string)

function : reads number of characters from data file and returns
		them in string.
Input :	data (text)
	number (integer)
Preconditions: EOF will no be encountered;
		EOLN will be treated as a blank;
		0 < Number <= MaxString
output:	String (stringtype)
postconditions:	string = string made up of number of characters
		beginning at the current position on data file.

b) the algorithm:

readfixedstring

for pos := 1 to number do
	read next character and place in string

set string length


c) the procedure

procedure readfixedstring (	VAR data : Text;
				Number   : integer;
				VAR String : stringtype);

VAR
	Pos : indextype;

Begin
for pos := 1 to Number do
	read ( data, string.chars.[pos]);
string.length := number;
end;


33)

a)  The specification

function :	inserts instring into string at teh position specified, 
		moving the following characters in string to make room
		for the new string.  Error flag is set if the resulting
		string length exceeds maximum string length.

Input:		string : stringtype
		instring : stringtype
		pos : indextype

percondictions:	string and instring not undefined
		1 <= pos <= length (string) + 1

output:		string (changed)
		error : boolean

postconditions:	string = orignal string with instring inserted beginning
		at position pos.  
		If error, string is returned unchanged.
		Error = length of string + length of instring exceeds
		maximum string length.	


b) procedure 1

procedure insert ( VAR string : stringstype;
		   instring   : stringtype;
		   pos        : indextype;
		   VAR error : boolean);

var
	tstring : stringtype;
	suberror : boolean;

begin
error := (string.length + instring.length > maxstring);

if not error then
	begin
	substring (string, pos, string.length - pos + 1, tstring, suberror);
	substring (string, 1, pos - 1, string, suberror);
	concat (string, instring, suberror);
	concat (string, tstring, suberror);
	end;
end;
	
		
 
 
c) procedure 2
 
procedure insert ( VAR string : stringstype;
                   instring   : stringtype;
                   pos        : indextype;
                   VAR error : boolean);
 
var 
	index : indextype;

begin

error := (string.length + instring.length > maxstring);

if not error then
        begin
	for index := string.length downto pos do
		string.chars[index + instring.length] := string.chars[index];
	for index := 1 to instring.length do
		string.cahrs[pos + index - 1] := instring.chars[index];
	string.length := string.length + instring.length;
	end
end;
	
#! rnews 497