# Thread: Binary sequence in lexicographic order

1. ## 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. 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. Originally Posted by tabstop
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. You should visit the tutorial page then.

5. 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.

6. you know you can assign using the bracket operators too.