469,270 Members | 1,731 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Please Help! need help creating a matrix!

i need help to make a board such as this:

-----------Col 0--Col 1--Col 2
Row 0------1------1--------0
Row 1------0------0--------1
Row 2------1------1--------1

(Please ignore the dashes, they were uses to space things out)

where the user enters a number between 3 to 6
and this number will be distinguished as variable 'dim'

The board will be a square matrix of size dim x dim. The contents of
each cell in that matrix will be a 0 or a 1. The computer will populate the matrix with 0ís and 1ís
randomly.

how do u do this??
Nov 21 '08 #1
7 2367
Curtis Rutland
3,256 Expert 2GB
Please use [code] tags to preserve whitespace and indention.

MODERATOR
Nov 21 '08 #2
bvdet
2,851 Expert Mod 2GB
Have you attempted to code this yourself? If you would show some effort, others would be more willing to help.
Hints:
random.choice(0,1)
Create a list of lists using a nested for loop or list comprehension

Example:
>>> for i in range(3):
... for j in range (3):
... print i,j
...
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
>>>
Nov 21 '08 #3
boxfish
469 Expert 256MB
random.choice(0,1)
Better make that
random.choice((0, 1))
Nov 22 '08 #4
bvdet
2,851 Expert Mod 2GB
Oops! Thanks boxfish.
Nov 22 '08 #5
well its something liek this, i started it...

Expand|Select|Wrap|Line Numbers
  1. def createboard(dim):
  2.     import random
  3.     global x
  4.     global board
  5.     row = []
  6.     for i in range(dim)
  7.         row.append(random.randint(0,1)
  8.  
  9.     board = []
  10.     for n in range(dim)
Nov 26 '08 #6
Curtis Rutland
3,256 Expert 2GB
Please enclose your posted code in [CODE] [/CODE] tags (See How to Ask a Question). Code tags preserve indention and uses a monospaced font.

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [CODE] [/CODE] tags in future.

MODERATOR
Nov 26 '08 #7
boxfish
469 Expert 256MB
You'll need a for loop inside a for loop. The pseudocode should be more like:
Expand|Select|Wrap|Line Numbers
  1. For each row:
  2.     Create a new row.
  3.     For each element in that row:
  4.         Add an element to the row.
  5.     Add the row to the matrix.
Hope this helps.
Nov 26 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by duikboot | last post: by
3 posts views Thread by Lodewijk van Haringhal | last post: by
6 posts views Thread by James Walker | last post: by
5 posts views Thread by TrvlOrm | last post: by
5 posts views Thread by tabani | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.