471,592 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,592 software developers and data experts.

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

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

Nov 21 '08 #2
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.
Create a list of lists using a nested for loop or list comprehension

>>> 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
469 Expert 256MB
Better make that
random.choice((0, 1))
Nov 22 '08 #4
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)
  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.

Nov 26 '08 #7
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 XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.