Question: The binToDec function should interpret a substring of characters in an array as a binary number, convert it to an integer, and return the integer.
The binToDec function should interpret a substring of characters in an array as a binary number, convert it to an integer, and return the integer. The first parameter is the array of characters; the second and third parameters are the beginning index and ending index of the substring. For example, assume A is a character array that contains the following characters.
1 0 1 1 0 1 0 0 1
The call binToDec(A, 2, 5) should convert the string of binary digits '1' '1' '0' '1' (the substring A[2] - A[5], inclusive) to the integer 13 and return it. One approach, using an algorithm for converting a binary number to a decimal number, involves evaluating the binary value from the least significant digit to the most significant digit, adding in (or not) the appropriate power of two to the decimal value. Rather than calculating the power of two for each digit, you can store the power of two in a variable and multiply it by two when you go to the next digit. (What should the power-of-two variable be initialized to for the least significant digit?)

Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
