HomeWork Help!

Can some one help me or give me hints on how to write the following program:



http://i60.tinypic.com/3588nz8.png
Last edited on
Looks like you want to do permutations with a set of numbers. Though not really sure what you need help on as you have not provided any code and you didn't even copy the assignment here but instead post a picture of it.

Question 4 (Programming) Write a program that prompts the user to enter a positive number N and
prints the set of all binary (
{0, 1}-valued) strings. For example, if the user enters 3, the program prints
                                             0 0 1                                             
                                             0 1 0                                             
                                             0 1 1                                             
                                             1 0 0                                             
                                             1 0 1                                             
                                             1 1 0                                             
                                             1 1 1                                             
I am trying to develop a code that generates each column as I have noticed that it follows a pattern. But it is so hard to implement the math!!!

How would you approach this problem? Would you try to generate the numbers row by row?
Definitely row by row, they are subsets.
http://stackoverflow.com/questions/1851134/generate-all-binary-strings-of-length-n-with-k-bits-set


We need 2 variables for this. 1) A string of 0's and 1's (the binary number) and 2) keep track of position of current bit we are working with, current bit position will start at index 0 (right most)

We want to start with 0 0 1 for this algorithm to work.

Step 1) if current bit is unset we set it, else we unset all bits goto step 2, then return to step 1.
Step 2) shift current index to the left
Step 3) if current index is > size break, else set the bit
Step 4) shift current index to the right most bit
Step 5) Go to step 1


(May be a few flaws in the steps but sounds right to me)

Step 1) if current bit is not set goto step 7
Step 2) unset all bits
Step 3) shift current index left
Step 4) if current index > size goto Z, else set current index
Step 5) shift current index to rightmost bit
Step 6) goto step 1
Step 7) set current bit
Step 8) shift current index left
Step 9) if current index is <= size goto step 1, else break



May still have a few logic problems but I am in a hurry sorry.
Last edited on
Topic archived. No new replies allowed.