Question

C++

Convert a Number from Binary to Decimal using a stack: The language of a computer is a sequence of Os and 1s. The numbering s

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code

#include<bits/stdc++.h>
using namespace std;
int binToDeci(string s){
// stack for
   stack<int> st;
// vector for storing weights
   std::vector<int> weight;
   int k=0;
   for (auto i:s)
   {
       weight.push_back(k++);
       st.push(i-'0');
   }

// reverse the vector weight
   reverse(weight.begin(),weight.end());

// variable for storing decimal
   int ans=0;

   while(!st.empty()){
       ans+=st.top()*(1<<weight[k-1]);
       k--;
       st.pop();
   }

   return ans;
  
}
//main function
int main()
{
   cout<<"enter a binary string\n";
   string s;
   cin>>s;
   cout<<"Decimal for "<<s<<" value is : "<<binToDeci(s)<<endl;
}

screenshot

i 4 #include<bits/stdc++.h> using namespace std; int binToDeci(string s) { // stack for stack int> st; vector for storing wei

sample output

[pp]$ g++ stack.cpp [pp]$ ./a.out enter a binary string 11000101 Decimal for 11000101 value is : 197 [pp]$ ./a.out enter a bi

please upvote if it helped. Feel free to ask any query.

Add a comment
Know the answer?
Add Answer to:
C++ Convert a Number from Binary to Decimal using a stack: The language of a computer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using assembly 8086 , Convert a binary string to decimal. Accept buffered user input in the...

    using assembly 8086 , Convert a binary string to decimal. Accept buffered user input in the form of a 16-bit binary string. Convert to unsigned integer and print to the screen. if the user enters less than 16 bits, you may pad the binary number with zeros on the left. Sample Execution: Enter a 16-bit binary number: 1001111001011100 The decimal unsigned integer equivalent is 40540 Enter a 16-bit binary number: 01100 The decimal unsigned integer equivalent is 12

  • using assembly 8086, Convert a binary string to decimal. Accept buffered user input in the form...

    using assembly 8086, Convert a binary string to decimal. Accept buffered user input in the form of a 16-bit binary string. Convert to signed integer and print to the screen. If the user enters less than 16 bits, or input illegal character return an error message and prompt for input again. Sample Execution: Enter a 16-bit binary number: 100111100101110 The decimal signed integer equivalent is -24996 Enter a binary number: 11001011100 Error! Please enter exactly 16-bits: 100111100101110 Enter a 16-bit...

  • 1. What decimal number is represented by the following excess 8 notation? 2. Convert 1111 from...

    1. What decimal number is represented by the following excess 8 notation? 2. Convert 1111 from excess eight representation to its equivalent base ten binary form 3. With two's complement signed binary representation, what is the range of numbers as written in binary and in decimal for an eight-bit cell? (lowest to highest) 4. Convert -7 from decimal to binary, assuming seven-bit two’s complement binary representation 5. Convert 111 1010 from binary to decimal assuming seven bit two's complement binary...

  • Programming Exercise 4.6 Use the strategy of the decimal to binary conversion and the bit shift...

    Programming Exercise 4.6 Use the strategy of the decimal to binary conversion and the bit shift left operation defined in Project 5 bits = input("Enter bit string: ") bits = bits[1:] + bits[0] print ("Result:", bits) to code a new encryption algorithm. The algorithm should Add 1 to each character’s numeric ASCII value. Convert it to a bit string. Shift the bits of this string one place to the left. A single-space character in the encrypted string separates the resulting...

  • (3 pts) Consider an unsigned fixed point decimal (Base10) representation with 8 digits, 5 to the...

    (3 pts) Consider an unsigned fixed point decimal (Base10) representation with 8 digits, 5 to the left of the decimal point and 3 to the right. a.      What is the range of the expressible numbers?    b.      What is the precision?    c.       What is the error?    ______________________________________________________________________________   (3 pts) Convert this unsigned base 2 number, 1001 10112, to each base given below   (Note: the space in the binary string is purely for visual convenience) Show your work. Using...

  • Why do computers use the binary number system instead of the decimal number? Convert 43_10 to...

    Why do computers use the binary number system instead of the decimal number? Convert 43_10 to binary and hexadecimal. Convert 100111_2 to hexadecimal and decimal Convert 110101_2 to its 2's complement representation. What is the largest Hex value that can be moved into 8-bit register 16-bit register What are the decimal equivalents of these Hex values?

  • 1. Convert the decimal number +164 and -164 to 9-bit binary numbers according to Sign magnitude,...

    1. Convert the decimal number +164 and -164 to 9-bit binary numbers according to Sign magnitude, One’s complement, and Two’s complement 2. Convert the binary number 111011010 to base 10 decimal form (our regular number system) treating it as each of the following representations: Sign magnitude, One’s complement, and Two’s complement

  • 3.8) Convert the hexadecimal number 0x15 to a decimal number. 3.9) Convert the hexadecimal number 0x19...

    3.8) Convert the hexadecimal number 0x15 to a decimal number. 3.9) Convert the hexadecimal number 0x19 to a decimal number. 3.10) Convert the decimal number -35 to an 8-bit two’s complement binary number. 3.11) Convert the decimal number -32 to an 8-bit two’s complement binary number. 3.12) Assuming the use of the two’s complement number system find the equivalent decimal values for the following 8-bit binary numbers: a)10000001 b)11111111 c)01010000 d)11100000 e)10000011 3.13) Convert the base 8 number 204 to...

  • a) Convert the binary number 101102 to base 8. b) Convert the decimal number 47.4410 to...

    a) Convert the binary number 101102 to base 8. b) Convert the decimal number 47.4410 to base 12. Find the base 12 fraction up to 6 digits to the right of the fraction point

  • 1. Do the following base conversions. (10 pts)       a. Convert the binary number 110100 to...

    1. Do the following base conversions. (10 pts)       a. Convert the binary number 110100 to decimal                                                                 b. Convert the decimal number 94 to hexadecimal                           c. Convert the binary number 10111011 to hexadecimal                             d. Convert the hexadecimal number B3 to binary                                   e. Convert the hexadecimal number 39E to binary                     2. The Starship Enterprise is visiting a planet where the inhabitants have 7 fingers. Not surprisingly, they use a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT