Non-Recursive gray code

I have to write a program that needs to print out gray code based on the amount of columns the user wants. For example if they choose three, I have 8 rows with gray code from 0 to 7.

I need help with the algorithm that I need to use. I have to use for loops and they must be contained within the main function.
This is what I have so far. It only prints out 0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int i, j;
    int num = 4;
    int numberOfRows = pow(2,num);
    for (int i = 0; (i < numberOfRows); i++)
    {
        for (int j=0; (j < num); j++)
         {
            if ((j % num / pow(2,num)) < (num /(pow(2,num)+1)))
                cout << "0\t";
            else
                cout << "1\t";

         }
         cout<<"\n";
    }

    return 0;
}
Topic archived. No new replies allowed.