Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

array and binary

$
0
0
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:
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

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>