menu prev next

RECORDS CONTAINING ARRAYS
Records can also contain arrays as a field. Consider the following example, which shows a record called month, whose element name is actually an array.


	type  monthname = packed array[1..4] of char;
	      month = RECORD
	                 days : integer;
	                 name : monthname
	              END;
	var  this_month : month;
	     this_month.days    := 31;  this_month.name[0] := 'J';
	     this_month.name[1] := 'a'; this_month.name[2] := 'n';
	     this_month.name := 'Feb ';


SELF TEST 26


	program  RECORD_TEST (output);
	type     time = RECORD
	                    hours, minutes, seconds : integer
	                END;

	procedure timeupdate ( var newtime : time );
	begin
	     newtime.seconds := newtime.seconds + 1;
	     if  newtime.seconds = 60  then
	     begin
	        newtime.seconds := 0;
	        newtime.minutes := newtime.minutes + 1;
	        if  newtime.minutes = 60  then
	        begin
	            newtime.minutes := 0;
	            newtime.hours := newtime.hours + 1;
	            if  newtime.hours = 24  then
	                newtime.hours := 0
	         end
	     end
	end;

	var  test_times : array [1..3] of time;
	     loop : integer;
	begin
	     test_times[1].hours   := 11;
	     test_times[1].minutes := 59;
	     test_times[1].seconds := 59;
	     test_times[2].hours   := 12;
	     test_times[2].minutes :=  0;
	     test_times[2].seconds :=  0;
	     test_times[3].hours   :=  1;
	     test_times[3].minutes := 29;
	     test_times[3].seconds := 59;
	     for loop := 1 to 3 do
	     begin
	        writeln('Time is ',test_times[loop].hours,':',
	                 test_times[loop].minutes,':',test_times[loop].seconds);
	        timeupdate(test_times[loop]);
	        write('One second later its ');
	        writeln(test_times[loop].hour,s':',test_times[loop].minutes,
	                ':',test_times[loop].seconds)
	     end
	end.

Click here for answer


Copyright B Brown/P Henry, 1988-1999. All rights reserved.
menu prev next