Hi,
I'm trying to write a function which takes a positive int and converts it into binary via repeatedly dividing by 2, storing the remainders in an array and printing them out in reverse.
This is what I've got so far.
void Dec2BinaryDivision(int a)
{
bool begin = false;
int rem[31]={}, i=0;
if (a == 0) //trivial case
{
cout << 0;
return;
}
/*for (i=0; i<32; i++)
cout << rem[1] << " ";*/
cout << "wat";
do //1s and 0s into array
{
rem[i] = a%2;
a = ((a/2) + rem[i]);
i++;
} while (a != 0);
i = 31;
while (!begin) //searches for the end of leading zeroes
{
if(1 == rem[i])
{
begin = true;
}
i--;
}
for (i; i>=0; i--) //prints array in reverse
{
cout << rem[i];
}
cout << endl << "wat";
}
Something very similar to this was somewhat functional earlier, but this only prints one 'wat' (cout << "wat" is a checkpoint) and then crashes. If anyone can see any obvious flaws in the function I'd really appreciate if you could show me.
Thanks