Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Recursion: Decimal to Binary

Posted on 2004-08-11
8
Medium Priority
?
1,944 Views
Last Modified: 2012-06-27
My program reads decimal numbers from an input file and outputs them in binary form.  If the numbers are negative it outputs them in 2's complement form.  The thing that is wrong with my program is that I need function main() to call function "string convert(int decimal)" and receive the decimal number and returns a string that represents the corresponding binary number in 16 bits format.  Also, I think I already have this but function "convert()" will have to call the recursive function "string dectobin(int decimal)" and receive the decimal number and returns a string that represents the corresponding binary number in free format.  Thus, it is the task of "convert()" to add the 0's (or 1's) to string returned by "dectobin()" so that the binary number is in 16 bits format.  I think the "void" part in my program is recursive and does the function "convert()".  Can anybody help me.  Here is my program and contents of input file:
Input3:
-8
123
45
72
-1534
-2341

#include <iostream>
#include <fstream>

using namespace std;

void dectobin(int inp, int base, int count = 0);

int main()
{
      ifstream inFile; //input file stream
      ofstream outFile; //output file stream
      
      inFile.open("input3.txt"); //open input file
      outFile.open("output.txt"); //open output file
      
      int decimalnum[6]; //declare array has 6 integers
      int base;
      int i;
      
      base = 2;
      
      cout<<"Enter the number in decimal: "<<endl;
      
      if(!outFile) //check for errors in output file
      {
            cout<<"Cannot open output file!"<<endl;
      }
      if(!inFile)
      {
            cout<<"Cannot open input file!"<<endl; //check for errors for input file
            return 1;
      }
      else // no errors do this
            for (i=0 ;i<6;i++) {  //loop retrieves the integers
                  inFile>>decimalnum[i]; //implement the integers from input file
                  cout<<"Decimal: "<<decimalnum[i]<<" = "; //output the integers
                  dectobin(decimalnum[i], base); //transfer from dec to bin  
                  cout<< " in Binary" <<endl;
            }
            
            
            return 0;
            
}
void dectobin(int inp, int base, int count)
{
      //first cast to 16 bits signed, then to 16 bits unsigned
      unsigned short num = (unsigned short)(short)inp;
      if(inp > 0 ||  count < 16)
      {
            dectobin(num/base, base, ++count);
            cout<<num % base;
            
      }
      
}
0
Comment
Question by:Steve3164
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 11780844
You can replace cout with a stringstream.  I added it as a param to dectobinand added a 'convert' routine to call dectobin.  It would be difficult to return a string from dectobin directly.
---------------------------------
#include <sstream>

void dectobin(int inp, int base, int count, std::stringstream &str)
{
     //first cast to 16 bits signed, then to 16 bits unsigned
     unsigned short num = (unsigned short)(short)inp;
     if(inp > 0 ||  count < 16)
     {
          dectobin(num/base, base, ++count, str);
          str << num % base;
     }
}

std::string convert(int num, int base)
{
    std::stringstream str;
    dectobin(num, base, 0, str);
    return str.str();
}

then in main loop:

    for (i=0 ;i<6;i++)
    {  //loop retrieves the integers
      std::cout<<"Decimal: "<<decimalnum[i]<<" = "; //output the integers
      std::cout << convert(decimalnum[i], base).c_str(); //transfer from dec to bin  
      std::cout<< " in Binary" <<std::endl;
    }
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 1200 total points
ID: 11781405
Or that:

#include <string>
using namespace std;


string dectobin(int num, int base);

int main()
{
     int decimalnum;
     int base;

     base = 2;

     cout<<"Enter the number in decimal: ";
     cin>>decimalnum;
     cout<<endl;
     cout<<"Decimal: "<<decimalnum<<" = " << dectobin(decimalnum, base) << " Binary" <<endl;
     cin >> base;
     return 0;

}

string dectobin(int inp, int base)
{
     //first cast to 16 bits signed, then to 16 bits unsigned
     unsigned short num = (unsigned short)(short)inp;
     string  bin(16, '0');
     unsigned short dig;
     for (int i = 15; i >= 0; --i)
     {
         dig    = (num % base);
         bin[i] = (char)dig + '0';
         num   /= base;
     }
     return bin;
}

Regards, Alex


0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11785088
Forgot to mention that is isn't recursive anymore ;-)

However, as you need a string now, recursion isn't very suitable now, though still possible:

#include <string>
using namespace std;

string dectobin(int num, int base, const string& str = "");

int main()
{
     int decimalnum;
     int base;
     base = 2;

     cout<<"Enter the number in decimal: ";
     cin>>decimalnum;
     cout<<endl;
     cout<<"Decimal: "<<decimalnum<<" = " << dectobin(decimalnum, base) << " Binary" <<endl;
     cin >> base;
     return 0;

}


string dectobin(int inp, int base, const string& str)
{
     if (str.length() < 16)
     {
         string bin = str;
         //first cast to 16 bits signed, then to 16 bits unsigned
         unsigned short num = (unsigned short)(short)inp;
         unsigned short dig;
         dig    = (num % base);
         bin  += (char)dig + '0';
         return  dectobin( (int)(num/base), base, bin);
    }
    return str;
}

Regards, Alex


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:drichards
ID: 11786680
Well, heck.  If you didn't really need recursion, you can eliminate iteration as well:

string dectobin(int inp, int base)
{
    double logbase = log((double)base);

    char val[33] = "00000000000000000000000000000000";
    int leadZeros = 31 - int(log((double)(unsigned)inp)/logbase);
    ::itoa(inp, val, base);

    return string(&val[16]);
}


Also, Alex, I believe your recursive version will produce the string in reverse order (LSB first).  I think this modification makes it right.  The only problem with this is that there is an inordinate amount of string creation, deletion, and copying.

std::string dectobin(int inp, int base, int count)
{
     std::string bin;
     if (count < 16)
     {
         //first cast to 16 bits signed, then to 16 bits unsigned
         unsigned short num = (unsigned short)(short)inp;
         unsigned short dig;
         dig    = (num % base);
         bin = dectobin((int)(num/base), base, count+1);
         bin += ((char)dig + '0');
     }
    return bin;
}
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11786982
>>   ::itoa(inp, val, base);

Seems to be wrong. Maybe that will do it???

   itoa((int)(unsigned)inp, &val[leadZeros], base);

   
>> I believe your recursive version will produce the string in reverse order

You are right, thanks for correction.

Regards, Alex
0
 
LVL 19

Expert Comment

by:drichards
ID: 11787543
Oops, cut and paste errors - you can do it without the casting, though:

        ::itoa(x, &val[leadZeros], base);
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11787792
>> you can do it without the casting

hmm, itoa prints negative signs, doesn't it? And what is 'x' ???

And maybe a strlen/strcat combination is faster than two logarithms and aone division?

Regards, Alex
0
 
LVL 19

Expert Comment

by:drichards
ID: 11788090
itoa will not print signs if the radix is not 10.  The parameter is int anyway, so it will just get implicitly converted back to signed.  'x' was my test variable.  It's equivalent to 'inp' in the code here.

I haven't done optimization testing, but it is entirely possible (and probable, all things considered) that strlen/strcat is faster.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question