Hi, im making a program to convert any number to binary using the division method. Its just dividing the number by 2 and saving the remainders (which will either 1 or 0 ).
At first I tought of just printing out the remainder saved in the variable rem, but there were some problems with 1 digit numbers, and the remainders are not in the correct order. For example the remainders of 10 are: 0101, but this of course isnt 10 in binary, you would have to start with the las bit, and go backwards, so it would be 1010.
So later i thought, why not putting the remainder inside an array? So thats what i did.
heres what i wrote:
ok, here is my question: If the number is 10, there are 4 elements in the array (0101), but i cant know that the number is 10, because its the user who has input that number so i cant know how many elements have been used in my array. How can i program this thing to print in the screen all the elements of the array, without knowing how many elements are there ( i cant do this: cout<< rem[0]<<rem[1]...), and how can i reverse that order, so that the last element is the first one.Code:
...
int rem[100];
int i;
int n;
...
...
i = n;
c = 0;
while ( i > 2)
{
a = n / 2;
rem[c] = n % 2;
n = a;
i--;
c++;
if ( n == 0)
{
break;
}
}
Maybe its a stupid question, but im studying this for the first time :)
waiting anxiously for your answer!! thanks in advance.