Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Sorting a list of zeros and ones.

  1. Jul 8, 2007 #1
    Assume i have an initial list of n-2 columns and 2^(n-2) rows, where initially:
    all the entries are zero.
    i wnt to sort it out that eveantually we'll get a truth table for this.
    i.e if we have n-2=2 rows and 4 columns, then:
    Code (Text):

    0 0
    1 0
    0 1
    1 1
     
    obvously we have here a jump first we initialise the array to give us everywhere zero, afterwards i need to insert via a loop the entries of 1, i.e in the first column it should be changing the zeros in a[1][1] and a[3][1] i.e we have a jump of two places and in the next column a jump of three places, but i don't know how to write it in the loop condition, any thoughts?
    the exam is tommorow, so your help would be tremendous for my success, thanks in advance.
     
  2. jcsd
  3. Jul 8, 2007 #2

    -Job-

    User Avatar
    Science Advisor

    You mean you want to generate something like this:
    Code (Text):

    ... 0 0 0 0
    ... 0 0 0 1
    ... 0 0 1 0
    ... 0 0 1 1
    ... 0 1 0 0
    ... 0 1 0 1
    ... 0 1 1 0
    ... 0 1 1 1
    ... 1 0 0 0
    ... 1 0 0 1
    ... 1 0 1 0
    ... 1 0 1 1
    ... 1 1 0 0
    ... 1 1 0 1
    ... 1 1 1 0
    ... 1 1 1 1
    ... . . . .
        . . . .
        . . . .
     
    ?
     
  4. Jul 8, 2007 #3

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Is there some particular reason you need to do this with nested for loops and complicated conditions? There are many simpler approaches.

    - Warren
     
  5. Jul 13, 2007 #4
    yes job, something like that.


    chroot, iv'e learned loops, so i guess this is the way i shouldv'e solved it.

    thanks anyway.
     
  6. Jul 13, 2007 #5

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    If I were doing it, I'd just use a single for loop that increments a counter. To extract each bit from the counter, I'd use a set of masks, one for each bit.

    - Warren
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Sorting a list of zeros and ones.
  1. Selection sort? (Replies: 2)

  2. Sorting algorithm? (Replies: 2)

  3. Perfect sort (Replies: 2)

  4. Merge sort (Replies: 1)

Loading...