Hey guys! I'm trying to write a recursive function to take a number in binary form and change it to decimal. As of now I am having the dumbest problem. I can't seem to take 2 to a variable power. This is probably something so basic but I'm drawing a blank. (Oh, and I haven't written a clause to prevent any numbers outside of 0 &1 yet so please ignore that little part).

As of now, I can't even test if I have the right idea for my recursive function because I can't compile due to some issue with taking 2 to a particular power.

Here is my recursive function called binary:

int binary(const int data[], int position)
{
    int result;
    
    if(position==0)
    {
     result=data[position]*pow(2,position);
     }
     else
     {
     result=(data[position]*pow(2,position))+binary(data, position-1);
     }
     return result;
}

Let me know if you need my entire code! Thanks in advance for any suggestions. Oh, and I do have the cmath header in there so I know that much isn't the problem...:yawn:

And this is the error message I get:
D:\MyPrograms\Recursion>g++ Num4.cpp -o test4.exe
Num4.cpp: In function `int binary(const int*, int)':
Num4.cpp:44: error: call of overloaded `pow(int, int&)' is ambiguous
D:/mingw32_snapshot_02-10-2009/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../in
clude/math.h:156: note: candidates are: double pow(double, double)
D:/mingw32_snapshot_02-10-2009/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../in
clude/c++/3.4.5/cmath:361: note: long double std::pow(long doubl
...and it goes on...

Edited 6 Years Ago by NitaB: forgot something

This link shows the function prototype that pow function takes. Make
sure you match one of the function prototype.

So for example this code :

pow(2,2);

will not work since pow does not overload pow(int,int) . But this any of these will work :

pow(2.0,2.0); 
pow(2.0,2);
pow(2.0f,2.0f);
pow(2.0f,2);

hi firstPerson! I just saw your response but I already got it. Had to to do some typecasting.

Edited 6 Years Ago by NitaB: n/a

The more pressing question now is why my function isn't giving me the correct answer when it's anything other than the base case.:-/
I "think" my logic is correct so I'm stumped...

Here it is now:

int binary(int data[], int position)
{
    double result;
    int decimal;
    
    if(position==0)
    {
     result=data[position]*pow((double)2,(double)position);
     }
     else
     {
     result=(data[position]*pow((double)2,(double)position))+binary(data, (double)position-1);
     }
     return result;
}

Okay, I've figured out what's going wrong! If I enter a number like 1, 111, 10101, etc, I get the correct answer. If I enter a number like 10(or anything unsymmetrical) I get the wrong answer. Soooo, my guess is that somehow I am going through the numbers in my array in the wrong order. *sighs*

If you have an 8 digit binary number, you want to have it so you're multiplying the value in position n with 2^(7-n). You could send in the size of the number in digits as a third parameter to your function that wouldn't change.

Hey jonsca! I see what you're saying but unfortunately the way I sorta "defined" position was merely length-1. I honestly have drawn out everything on paper and for the life of me cannot figure out why it basically computes the decimal number backwards. When I put in 100, I get the answer I would've gotten for 001 and vice versa. Crazy!

Here's my ENTIRE code:

#include <iostream>
#include <cmath>
using namespace std;

int binary(int data[], int position);

int main()
{
    int length;
    int tempNum;
    int i;
    
    cout<<"Enter the length of the binary number to be converted: "<<endl;
    cin>>length;
    
    int array[length];
    cout<<"Enter binary number one digit at a time. Hit return after each digit."<<endl;
    for(i=0; i<length; i++)
    {
     cin>>tempNum;
     array[i]=tempNum;
    }

    cout<<"The binary number you entered is: ";
    for(i=0; i<length; i++)
    {
     cout<<array[i];
    }
    cout<<endl;
    
    cout<<"The decimal form of the binary number you entered is: ";
    cout<<binary(array, length-1)<<endl;

    system("pause");
    return 0;
}
//*****************************************************************************
int binary(int data[], int position)
{
    double result;
    
    if(position==0)
    {
     result=data[position]*pow((double)2,(double)position);
     }
     else
     {
     result=(data[position]*pow((double)2,(double)position))+binary(data, (double)position-1);
     }
     return result;
}

Thanks again for the suggestions!

Hey back. If you trace your code you can see it: result=data[digits-1-position]*pow((double)2,(double)position); would do the trick (since you do want the low indexes to be multiplied by the higher values of 2^n and vice versa).

int binary(int data[], int position,int digits)
{
......
......
......       +binary(data,position-1,digits);  
}

digits gets passed all the way through and doesn't change with the recursion. You always have it around. There might be a better way but that was my thought.

Edited 6 Years Ago by jonsca: n/a

Ah, seems like it should work but I actually had tried that too. It gets rid of the problem with "unsymmetrical" binary numbers but anything with leading or trailing zeroes is always off by 1(either 1 too much or 1 too little).
This is what I did:

#include <iostream>
#include <cmath>
using namespace std;

int binary(int data[], int position, int length);

int main()
{
    int length;
    int tempNum;
    int i;
    
    cout<<"Enter the length of the binary number to be converted: "<<endl;
    cin>>length;
    
    int array[length];
    cout<<"Enter binary number one digit at a time. Hit return after each digit."<<endl;
    for(i=0; i<length; i++)
    {
     cin>>tempNum;
     array[i]=tempNum;
    }

    cout<<"The binary number you entered is: ";
    for(i=0; i<length; i++)
    {
     cout<<array[i];
    }
    cout<<endl;
    cout<<endl;
    
    cout<<"The decimal form of the binary number you entered is: ";
    cout<<binary(array, length-1, length)<<endl;

    system("pause");
    return 0;
}
//*****************************************************************************
int binary(int data[], int position, int length)
{
    double result;
    
    if(position==0)
    {
     result=data[position]*pow((double)2,(double)position);
     }
     else
     {
     result=(data[length-1-position]*pow((double)2,(double)position))+binary(data, position-1, length);
     }
     return result;
}

For example: 10, 01, 1000, 0001, etc...These numbers are always off.

Edited 6 Years Ago by NitaB: n/a

Correction/clarification: if there are leading zeroes, the final answer is always 1 less than what it should be. If there are trailing zeroes, the final answer is always 1 more than what it should be.

This article has been dead for over six months. Start a new discussion instead.