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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!