S4CS / Mar. 2003 p. 1

KwunTongGovernmentSecondary School

S. 4 Computer Studies 2002/2003

Assignment #9

Objective : ProcedureDeadline:

  1. Write a Pascal program to accept two decimal values and display the binary multiplication of them in the following format.

Enter the first integer (0 to 127) : 108

Enter the second integer (0 to 127) : 117

01101100

x 01110101

------

01101100

01101100

01101100

01101100

01101100

------

0011000101011100

Your program should include a procedure dec_to_bin( ) to convert an decimal integer, dec, into the binary number bin with count bits. Here is the procedure header,

procedure dec_to_bin(count, dec:integer; var bin:string);

For example, the execution of the following invocation will give a result ‘0101’ to the string variable b:

dec_to_bin(4, 5, b);

  1. Write a Pascal program to implement the following game with 8 vertical tubes each capable of holding 6 balls. The players put a ball into one of the tubes in turn. If a player has 4 of his / her balls lined up horizontally, vertically or diagonally, he / she wins.

The following is the screen layout of the program.

Part of the program has been pre-written for you and the codes are shown on page 2. You can download the file from the school web page.

Note that the array tubes[ ] represents the positions of the eight tubes with six elements, numbered from bottom to the top. E.g. the bottom element of the third tube is tubes[3, 1]. Each array top[ ] element contains the position where a new element to be added to that tube. E.g. The final values of top[1] to top[8] for the example on last page are 1,2, 3, 2, 3, 4, 5 and 1.

program p1a9_2003;

uses crt;

var

top : array[1..8] of integer;

tubes : array[1..6, 1..8] of char;

(* Here are two global variables that can be used within all procedure. You can define other variables for the main program. *)

procedure initialize;

(* This procedure initializes all the values in the array top[]

and tubes[]. *)

procedure check_win(ch : char; var win : boolean);

(* This procedure checks if any player wins after a round. It contains the procedure check_4() to check if the 4 consecutive positions contain the ball of the player of the turn. *)

var (* ... *)

procedure check_4 (x, y, stepx, stepy : integer;

ch : char; var w : boolean);

var

i, j : integer;

equal : boolean;

begin

equal := true;

for i := 0 to 3 do

if tubes[x+stepx*i, y+stepy*i] > ch then

equal := false;

if equal then w := true

end;

begin

(* ... *)

end;

procedure disp_tubes;

(* This procedure displays the content of all the eight tubes. *)

begin

(* Here is the general algorithm of main program:

1. Initialize the content of array top[] and tubes[]

2. Enter the symbols for the two player

3. repeat steps until someone wins and

3.1 display the content of tubes

3.2 change turn, i.e. player 1 to player 2, or vice versa

3.2 accept and validate a tube number (from 1 to 8 of which the

tube is not full)

3.3 update arrays top[] and tubes[]

3.4 check if there is the player wins after his/her turn

4. Display result

*)

end.

Note that the procedure check_4( ) works in this way:

The parameter x and y specify one position on the tubes and stepx and stepy specify the direction of the 4 consecutive positions to be checked. E.g. check_4(5, 3, -1, 1, ‘*’, win) will check if all elements tubes tubes[5, 3], tubes[4, 4], tubes[3, 5], tubes[2, 6] contain a character ‘*’. If yes, win will become true; otherwise win will become false.

* You can change any code in the pre-written part except that the procedures must be used as instructed.

** The underlined items are input by user.

S4 Assignment #9page 1