Hello,
im trying to write a recursive algorithm to generate the following binary sequence.
ie basically:
for 3:
000
001
010
011
100
101
110
111
i tried the iterative method, it works fine.
this is what i did:
for (i = 0 to pow(2, n) ) {
//generate the binary form of no.
}
However, when i tried the following method(recursion) it does not work:
#include <iostream>
using namespace std;
int arr[10];
void pattern(int level, int a)
{
int i;
if (level == 2) {
for (i = 0; i < 2; i++) {
cout << arr[i];
}
cout << endl;
} else {
for (i = 0; i < 2; i++) {
arr[i] = !arr[i];
pattern(level + 1, arr[i]);
}
}
}
int main()
{
pattern(0, 1);
return 0;
}
It works for the above value(ie n = 2). But does not work for higher values. Can anyone please help?
Thanks.