Binary sequence in lexicographic order

This is a discussion on Binary sequence in lexicographic order within the C++ Programming forums, part of the General Programming Boards category; Here is my assigment's problems: Required: Write a C++ program that writes all binary sequences of length 15 in increasing ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    Binary sequence in lexicographic order

    Here is my assigment's problems:

    Required: Write a C++ program that writes all binary sequences of length 15 in increasing lexicographic order into a file "binary15.txt".

    My solution so far is like this:
    vector<int>v(15);
    for(int i=0; i<v.size(); i++)
    cout << v[i];
    cout << endl;

    v.pop_back();
    v.push_back(1);
    for(int i=0; i<v.size(); i++)
    cout << v[i];
    cout << endl;

    v.pop_back();
    v.pop_back();
    v.push_back(1);
    v.push_back(0);
    for(int i=0; i<v.size(); i++)
    cout << v[i];
    cout << endl;

    v.pop_back();
    v.push_back(1);
    for(int i=0; i<v.size(); i++)
    cout << v[i];
    cout << endl;


    However, the output only shows the first 4 lines of the binary sequence of length 15 each and I realize if I am going to write all the sequences, the commands that I have to repeat if using vector are too abundant.

    Anyone have better idea of how to do this question in a smarter way? I have to submit the assignment by tomorrow. Hope anyone of you may help me in this. Thank you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So why not make a loop? You will have to pay attention to the patterns -- how to programmatically get from one to the next, rather than doing it yourself.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    Quote Originally Posted by tabstop View Post
    So why not make a loop? You will have to pay attention to the patterns -- how to programmatically get from one to the next, rather than doing it yourself.
    Making a loop? How to do that? Can you please guide me on how to start? I am a newbie in programming.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should visit the tutorial page then.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    all binary sequences of length 15 in increasing lexicographic order
    That's the same as outputting the numbers 0 to 32767 in order, in binary, padded with leading zeros.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    you know you can assign using the bracket operators too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you order your game code?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-05-2006, 05:26 PM
  2. Binary Trees
    By wvu2005 in forum C Programming
    Replies: 7
    Last Post: 10-15-2005, 04:59 PM
  3. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 07:35 PM
  4. wsprintf and format specifiers
    By incognito in forum Windows Programming
    Replies: 2
    Last Post: 01-03-2004, 09:00 PM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21