Please Help! need help creating a matrix!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • victory2006
    New Member
    • Nov 2008
    • 10

    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??
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Please use [CODE] tags to preserve whitespace and indention.

    MODERATOR

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      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
      >>>

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        Originally posted by bvdet
        random.choice(0 ,1)
        Better make that
        random.choice(( 0, 1))

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Oops! Thanks boxfish.

          Comment

          • victory2006
            New Member
            • Nov 2008
            • 10

            #6
            well its something liek this, i started it...

            Code:
            def createboard(dim):
                import random
                global x
                global board
                row = []
                for i in range(dim)
                    row.append(random.randint(0,1)
                        
                board = []
                for n in range(dim)

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              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

              Comment

              • boxfish
                Recognized Expert Contributor
                • Mar 2008
                • 469

                #8
                You'll need a for loop inside a for loop. The pseudocode should be more like:
                Code:
                For each row:
                    Create a new row.
                    For each element in that row:
                        Add an element to the row.
                    Add the row to the matrix.
                Hope this helps.

                Comment

                Working...