Question
FOR C++. NEED HELP ! Define a function int binaryToDecimal(char b[]) that returns the decimal number corresponding to nonnegative binary number b. For example if
FOR C++. NEED HELP !
Define a function int binaryToDecimal(char b[]) that returns the decimal number corresponding to nonnegative binary number b. For example if b is "11011" then binaryToDecimal(b) will return 27.
Note that b might contain leading zeros for example "011" is equal to 3, just like "11". Use the following algorithm:
Declare an int variable temp to store partial results. Initialize temp to 0, then scan b from left to right. For each new bit (a '0' or a '1') multiply temp by 2 and add the value represented by that bit (a 0 or a 1).
Driver used to test your code:
int binaryToDecimal(char b[]);
int main() {
char b[30]; cin >> b;
cout << "b = " << b << endl;
cout << "result = " << binaryToDecimal(b) << endl;
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started