jeudi 24 décembre 2020

How to fill a matrix following this rule

I have to create a matrix with random numbers 0 or 1. The problem is that the number of 1 must be entered by the user and the 1 cells must follow a rule: if a cell is 1, the eight cells around it must necessarily be 0.

Here an example of correct output:

User entered: 8

1 0 0 1 0 0
0 0 0 0 0 1
0 1 0 0 0 0
0 0 0 0 0 1
1 0 1 0 0 0

Here an example of not correct output:

User entered: 8

1 1 0 1 0 0
0 0 0 0 0 1
0 1 0 1 0 0
0 0 0 0 0 1
1 0 1 0 0 0

This is not correct because at line 1 there are two 1 close.

My biggest problem is that I can't figure out how to insert 1s randomly since the number of 1s is entered by the user. Any advice?




Aucun commentaire:

Enregistrer un commentaire