Sorting a list of zeros and ones.

  • Thread starter Thread starter MathematicalPhysicist
  • Start date Start date
  • Tags Tags
    List Sorting
AI Thread Summary
The discussion revolves around generating a truth table from an initial array of zeros, specifically for a scenario where there are n-2 columns and 2^(n-2) rows. The user is seeking help on how to implement a loop that fills in the array with binary values, transitioning from zeros to ones in a structured manner. The desired output is a systematic arrangement of binary combinations, resembling a truth table. Suggestions include using nested loops with specific conditions to manage the transitions, though some participants recommend simpler methods, such as using a single loop with bit manipulation techniques. The urgency of the request is emphasized due to an upcoming exam.
MathematicalPhysicist
Science Advisor
Gold Member
Messages
4,662
Reaction score
372
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:
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.
 
Technology news on Phys.org
You mean you want to generate something like this:
Code:
... 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
... . . . .
    . . . .
    . . . .
?
 
Is there some particular reason you need to do this with nested for loops and complicated conditions? There are many simpler approaches.

- Warren
 
yes job, something like that.


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

thanks anyway.
 
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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top