Generate binary numbers

Hi, I am trying to generate all the binary numbers of length 3, and in order. I'm not allowed to use any special libraries or stuff like that to get the solution. I should get

000
001
010
011... etc

So far I am having problems getting the sequence to continue (ie, I have only gotten the first two lines). This is how I plan to work this problem:

1. Start with arrays A and B, assign all to zero. The array B will be the one that produces my final answer.
2. Apply the condition for a number of reps equal to 2^3 = 8.
3. Print out the values of B.
4. Set new values of A as B, and then reset B to zero before starting a new repetition.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
int main(int argc, char *argv[])
{
    int A[3];
    int B[3];
    for(int i=0;i<3;i++)
    {
            A[i]=0;
            cout << A[i];
    } 
    cout << endl;
                  
    // Assign all zero

    for(int i=0;i<3;i++)
    {
            A[i]=0;
            B[i]=0;
    }

     
     for(int k=0;k<8;k++)
     {
      
             // Condition
              
             for(int i=0;i<3;i++)
             {
             
                     if(i<2)
                     B[i] = A[i];
            
                     if(i=2)
                     B[i] = 1;
            
                     if(i>2)
                     B[i]=0;
              }
   

        for(int i=0;i<3;i++)
        cout << B[i];
        cout << endl;
        
    
        for(int i=0;i<3;i++)
        A[i] = B[i]; 
        
        }
          

        
    system("PAUSE");
    return EXIT_SUCCESS;
}
You need to remember the difference between the value zero and the human-readable character zero.

Also, you don't need two arrays. You only need one.


Take a moment to consider binary addition.

000 + 1 --> 001

The last zero turned into a one. Now:

001 + 1 --> 010

The last digit was already a one, so it was turned back into a zero and we added one to the remaining (leftmost two) digits.

Let's formulate that into some simple rules:

1. Adding '0' and '1' produces '1'.
2. Adding '1' and '1' produces '0', plus we need to apply the rules to the digits to the left of the one just changed.


Here we go. Given:
000
+ 1

Apply rule 1: (zero and one makes one)
= 001


Next:
001
+ 1

Apply rule 2: (first, turn that one into zero)
--> 000
(second, apply the rules to what remains)
000
+ 1

Apply rule 1: (zero and one makes one)
= 010


And again:
010
+ 1

Apply rule 1:
= 011


And one more time:
011
+ 1

Apply rule 2: (first, turn the one into a zero)
--> 010
(second, apply the rules to what remains on the left)
010
+ 1

Apply rule 2: (first, turn the one into a zero)
--> 000
(second, apply the rules to what remains on the left)
000
+ 1

Apply rule 1: (turn the zero into a one)
= 100


I suggest you write a little function that does this for you. It might have a prototype like:

void add_one_at( char* s, int index );

This function needs to apply both Rule 1 and Rule 2. Remember, Rule 2 involves calling the function again. That is, the function may need to call itself to apply the rules again. Don't forget to check that you don't call the function with an index less than zero. (The index is where you are adding one.)


Then, all you need is your initial array, and to use the function on it 23-1 times:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
  {
  char binary_digits[] = "000";

  cout << binary_digits << endl;
  for (int n = 0; n < 7; n++)
    {
    add_one_at( binary_digits, 2 );  // 2 is the index of the one's position
    cout << binary_digits << endl;
    }

  return 0;
  }

Hope this helps.
Last edited on
Thanks Duoas! It did.

I did it both ways, using your method based on the ones position and also my condition, which is based on the rightmost zero. Thanks :)
Topic archived. No new replies allowed.