Monday, December 12, 2011

Please write a program that converts binary to decimal numbers?

Binary to Decimal Conversion


-expand the binary number into a notation. The right significant bit will start at 0 exponent counting towards the left significant bit. Add the sum of product of exponentiation based 2.


Ex:


Convert 1012 to________10











Solution:





1012 =1x 22 + 0 x 21 + 1 x 20





=4+0+1





=5





Answer=510





Problem Solving: Write a program that asks a user to enter binary numbers as String (array of characters with size 20). Convert that binary numbers into an equivalent decimal number. Display the decimal number equivalent of that binary number on screen.





1. Declare a variable for the stack with a size of 20 elements; array of integers.





2. Traverse the elements of the string convert them into integer numbers push them into stack.





3. Perform a repetitive loop computation for the sum of products by popping the contents of the stack. It will repeat by the length of the string.





4.Display the decimal number on the screen.


use C++|||Please write a program that does your own homework!|||What you've posted in your question is confusing, because there are superscripts and subscripts that aren't being displayed. E.g., by 1012 you mean 101 base 2, which is indeed 5 base 10, for which you wrote 510; and you wrote 22 for 2 squared.





The instructions for your assignment have you using a stack and exponentiation, so you'll need to do that. That's not how I would do it, however, so I'll show you a different approach -- easier and more efficient, I think. The problem statement also suggests you use an array of char for user input. In C++, a string object is much better, so I would urge you to use that instead.





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;climits%26gt;





using namespace std;





bool isValidBinaryStr(const string%26amp;);


int str2bin(const string%26amp;);


const size_t bitsPerInt = sizeof(int) * CHAR_BIT;





int main(int argc, char *argv[]) {


聽 聽 string in;





聽 聽 while (true) {


聽 聽 聽 聽 cout %26lt;%26lt; endl %26lt;%26lt; "Enter up to " %26lt;%26lt; bitsPerInt %26lt;%26lt; " 1s and 0s: ";


聽 聽 聽 聽 getline(cin,in);


聽 聽 聽 聽 if ((in.size() %26gt; 0) %26amp;%26amp; (isValidBinaryStr(in) == true)) {


聽 聽 聽 聽 聽 聽 cout %26lt;%26lt; str2bin(in) %26lt;%26lt; endl;


聽 聽 聽 聽 }


聽 聽 }


聽 聽 return 0;


}





//


// Convert string of bits to an integer


//


int str2bin(const string %26amp;s) {


聽 聽 int n = 0, j = 0;


聽 聽 string::const_reverse_iterator i = s.rbegin();





聽 聽 for (i = s.rbegin(); i != s.rend(); i++,j++) {


聽 聽 聽 聽 n |= (*i - '0') %26lt;%26lt; j;


聽 聽 }


聽 聽 return n;


}





//


// Return true if all characters of s are either '1' or '0'.


//


bool isValidBinaryStr(const string %26amp;s) {


聽 聽 string::const_iterator i;


聽 聽 bool valid = true;





聽 聽 for (i = s.begin(); (i != s.end()) %26amp;%26amp; (valid == true); i++) {


聽 聽 聽 聽 valid = ((*i == '1') || (*i == '0'));


聽 聽 }


聽 聽 return valid;


}








#if 0





Sample run:





Enter up to 32 1s and 0s: 1010


10





Enter up to 32 1s and 0s: 1111


15





Enter up to 32 1s and 0s: 101001110010


2674





Enter up to 32 1s and 0s: 101010


42





#endif|||Chez, Nigel is quite right. Please do your own homework. But if you show us what you have so far, we might be able to point out any major flaws in approach. Remember that in binary each digit is worth twice as much as the next digit down.





And 1012 is not a binary number.








Added: Since cja below gave you some code, here is my solution in Perl:








echo "101" | perl -ane '{ chop; grep {$T=$T*2+$_;} split //; print "$T\n"; }'








Change the echo "101" to any binary-looking value you need. Programme prints the decimal equivalent.

No comments:

Post a Comment