#include <iostream>
#include <cmath>
using namespace std;
//stores binary representation of B in the array binary
void convert_binary(int N, [B]int *binary = new int[0][/B], int *total=0, int counter = -1)
{
if (N > 0)
{
//begining
if (counter == -1)
{
//declares parameter
counter = ceil(log((double)N)/log((double)2));
binary = new int[counter];
*total =counter;
for(int i=0; i<*total;i++)
binary[i] = 0;
}
//stores the digit
binary[counter] = N%2;
cout<<"Counter: "<<counter<<" N%2: "<<N%2<<" binary["<<counter<<"]: "<<binary[counter]<<endl;
//updates parameters
counter--;
N = N/2;
convert_binary(N,binary,total,counter);
}
else
{
cout<<"ENDING ... "<<endl;
for(int i=0; i<*total;i++)
cout<<binary[i]<< " ";
cout <<endl<<"ENDED "<<endl; [B]// this provides correct output 0 1 0 1 0 1 .. I am inside method[/B]
}
}
int main()
{
int * binary = new int[10];
int *total = new int(10);
convert_binary(21,binary,total);
for(int i=0; i<*total;i++)
cout<<binary[i]<< " "; [B]//no changes to the array![/B]
cout<<endl<<"FINISH"<<endl;
return 0;
}