# Answered Essay: Write a recursive function (with no loops or static variables) named binToDec that takes a string object as a parameter. T

Write a recursive function (with no loops or static variables) named binToDec that takes a string object as a parameter. The string will consist of 1s and 0s, representing a binary number. The function should return an int giving the decimal equivalent. For example,

```cout << binToDec("101010") << endl;
```

should print out 42.

We can calculate this conversion as 1*25 + 0*24 + 1*23 + 0*22 + 1*21 + 0*20. This is, incidentally, the same as how decimal numbers work, except the base is different. For example “1027” is 1*103 + 0*102 + 2*101 + 7*100.

Notice that the indices of the characters count in the opposite direction as the exponents do.

You may not use any built-in base-conversion functionality of C++.

You may write a helper function that meets the specifications and calls your recursive function with additional arguments.

You may use the string::substr() function if you want.

#include<iostream>

#include<stdlib.h> /* header file for atoi() and itoa() functions */

#include<cmath> /* header file for pow() function */

using namespace std;

int decimal,weight; /* global variables, all the global variables have the initial value zero(0), so decimal=0, weight=0 */

int binToDec(char number[10]) /* function definition starts here, and it is taking a string as its argument and returns an integer */

{

int bit,n; /* local variables */

char n1[10]; /* local variable */

n=atoi(number); /* converts the string to number and storing in n */

if(n>0) /* if n is greater than 0 */

{

bit=n%10; /* identifying the right most digit */

decimal=decimal+bit*pow(2,weight); /* calculating the partial sum (by multiplying the digit with its position value in binary) */

n=n/10; /* updating the number by dividing with 10 */

weight++; /* incrementing the weight value */

itoa(n,n1,10); /* converting the integer n to string and storing in n1, 10 is the base of the number system */

binToDec(n1); /* recursive call to binToDec() */

}

else return decimal; /* returning the value of decimal to the main() function */

}

int main() /* main() function definition */

{

char number[10]; /* string declaration */

cout<<“Enter a binary number:”;

cin>>number; /* taking a binary number into number */

cout<<“The equivalent decimal number is:”<<binToDec(number); /* function call to binToDec() */

return 0;

}

Output:

Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

Try it now!

## Calculate the price of your order

Total price:
\$0.00

How it works?

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.