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...

  • 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

  • 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...

  • 2. Convert the following two's complement binary numbers to decimal numbers. These numbers are integers.

    2. Convert the following two's complement binary numbers to decimal numbers. These numbers are integers. a. 110101 b. 01101010 c. 10110101 3. Convert the following decimal numbers to 6-bit two's complement binary numbers and add them. Write the results in 6-bit two's complement binary number format and decimal number format. Indicate whether or not the sum overflows a 6-bit result. a. 14 + 12 b. 30 + 2 C. -7 + 20 d. -18-17 4. What is the signed/magnitude and two's complement range of...

  • Homework 1: Q1: Convert the following binary number to decimal. • 11001100100.00011 Q2: Convert the following...

    Homework 1: Q1: Convert the following binary number to decimal. • 11001100100.00011 Q2: Convert the following decimal number to binary. • 1365.1234 Q3: Convert the following both to octal and hexadecimal. • (10110100000101.1011)2 (16001.567)10 (directly convert to octal and hexadecimal without converting to binary) Q4: Convert the following hexadecimal number to decimal, octal, and base 4. . ABCD.EF Q5: Convert (375, 765)10 to base 7. Q6: Convert (12310)4 to base 5. Q7: Convert (35421)6 to decimal. Q8: Convert (1991)10 first...

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
ADVERTISEMENT