Sorting a list of zeros and ones.

  • Thread starter Thread starter MathematicalPhysicist
  • Start date Start date
  • Tags Tags
    List Sorting
Click For Summary

Discussion Overview

The discussion revolves around generating a truth table from a list of zeros and ones, specifically focusing on how to implement this using loops in programming. Participants explore different methods for constructing the table, including the use of nested loops and simpler alternatives.

Discussion Character

  • Homework-related, Technical explanation, Debate/contested

Main Points Raised

  • One participant describes the initial setup of a list with zeros and the desired output format for a truth table, seeking help with the loop conditions to insert ones.
  • Another participant confirms the desired output format, providing an example of how the truth table should look.
  • One participant questions the necessity of using nested loops and suggests that simpler methods may exist for generating the truth table.
  • A different participant proposes using a single for loop with a counter and bit masks to extract each bit, indicating a potential alternative approach.

Areas of Agreement / Disagreement

Participants express differing opinions on the best approach to solve the problem, with some advocating for nested loops while others suggest simpler methods. No consensus is reached on a single preferred solution.

Contextual Notes

The discussion does not clarify the specific programming language being used or the exact requirements of the assignment, which may affect the proposed solutions.

Who May Find This Useful

Students or individuals interested in programming techniques for generating binary representations or truth tables, particularly those preparing for exams or assignments in computer science or related fields.

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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
5
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
9
Views
3K