Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using C++ (specifically stacks structures of C++), write a converter that converts a binary to decimal. This is the code I have so far but

Using C++ (specifically stacks structures of C++), write a converter that converts a binary to decimal.

This is the code I have so far but I can't get it to work:

/* for this program, the algorithm I am trying to use for binary-decimal conversion is to convert each digit by different powers of 2, and then add them up to get the decimal value. For example: binary 1101 = 1*2^0 + 0*2^1 + 1*2^2 + 1*2^3 = 1 + 0 + 4 + 8 = decimal 13 I am still confused on how to use push, pop, and top.*/

#include #include #include

using namespace std;

int main() { stack intStack; cout << "the binary is 1101, convert to decimal please?" << endl; intStack.push(1); intStack.push(0); intStack.push(1); intStack.push(1);

int base = 2; // base of 2 int power = 0; // power of 2 int decimal = 0; // the decimal that binary is converted to

while (!intStack.empty()) {

decimal = intStack.top() * (base^power); power++; intStack.pop(); cout << "The decimal is: " << decimal << endl; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Distributed Relational Database Architecture Connectivity Guide

Authors: Teresa Hopper

4th Edition

0133983064, 978-0133983067

More Books

Students also viewed these Databases questions