27.5k views
3 votes
Nine coins are placed in a 3x3 matrix with some face up and some face down. you can represent the state of the coins using a 3x3 matrix with values 0 (heads) and 1 (tails). here are some examples: 0 0 0 1 0 1 1 1 0 0 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 1 each state can also be represented using a binary number. for example, the preceding matrices correspond to the numbers: 000010000 101001100 110100001 there are a total of 512 possibilities, so you can use decimal numbers 0, 1, 2, 3,...,511 to represent all the states of the matrix. write a program that prompts the user to enter a number between 0 and 511 and displays the corresponding matrix with the characters h and t.

User Eyvind
by
8.5k points

1 Answer

2 votes
Hello,

Here is the program in QB64

CONST Ordre = 3
DIM SHARED mat(1 TO Ordre, 1 TO Ordre) AS INTEGER, n AS INTEGER
CALL ReadN
CALL MakeMat
CALL SeeMat
END

SUB ReadN
SHARED n AS INTEGER
DIM k AS INTEGER
INPUT "0<=n<=511"; k
IF k < 0 OR k > (2 ^ (Ordre * Ordre)) - 1 THEN PRINT "erreur de valeur de n"; n: END
n = k
END SUB

SUB MakeMat
SHARED mat() AS INTEGER, n AS INTEGER
DIM i AS INTEGER, j AS INTEGER, x AS INTEGER
x = n
FOR i = 1 TO Ordre
FOR j = 1 TO Ordre
mat(i, j) = x MOD 2
x = INT(x / 2)
NEXT j
NEXT i
END SUB

SUB SeeMat
SHARED mat() AS INTEGER
DIM i AS INTEGER, j AS INTEGER
FOR i = 1 TO Ordre
FOR j = 1 TO Ordre
IF mat(i, j) = 0 THEN
PRINT "h|";
ELSE
PRINT "t|";
END IF
NEXT j
PRINT " "
NEXT i
END SUB




User Needfulthing
by
8.8k points