An array called binary representing a number in binary has been created. Write
a function called decimal to calculate and return the corresponding decimal integer.
---------------------------------
index 0 | 1 | 2 | 3 | 4 |
---------------------------------
binary 1 | 1 | 1 | 0 | 1 |
---------------------------------
The result of calling the function decimal with the array above is 29.
My solution:
I would like to know if my solution answers the question above.
Thanks
a function called decimal to calculate and return the corresponding decimal integer.
---------------------------------
index 0 | 1 | 2 | 3 | 4 |
---------------------------------
binary 1 | 1 | 1 | 0 | 1 |
---------------------------------
The result of calling the function decimal with the array above is 29.
My solution:
int[] binary = new int [10];
int decimal (int len)
{
int d = 1;
for(int i = 0; i < len; i++)
{
d = d * 2 + binary[i];
}
return d;
}
I would like to know if my solution answers the question above.
Thanks