By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

Please Help! need help creating a matrix!

P: 10
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
Share this Question
Share on Google+
7 Replies


Curtis Rutland
Expert 2.5K+
P: 3,256
Please use [code] tags to preserve whitespace and indention.

MODERATOR
Nov 21 '08 #2

bvdet
Expert Mod 2.5K+
P: 2,851
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
Expert 100+
P: 469
random.choice(0,1)
Better make that
random.choice((0, 1))
Nov 22 '08 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Oops! Thanks boxfish.
Nov 22 '08 #5

P: 10
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
Expert 2.5K+
P: 3,256
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
Expert 100+
P: 469
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.