Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In c++, using a Queue, input an integer between 0 and 255. Convert it to binary

ID: 3571866 • Letter: I

Question

In c++, using a Queue, input an integer between 0 and 255. Convert it to binary by storing each binary digit in the queue. Example: 1 1 1 1 1 1 1 1 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0 128 64 32 16 8 4 2 1 Sum the values together to convert the binary value back to decimal. Now, I need to do the same but with a stack this time. Numbers can be between MAX_INT and 0. In c++, using a Queue, input an integer between 0 and 255. Convert it to binary by storing each binary digit in the queue. Example: 1 1 1 1 1 1 1 1 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0 128 64 32 16 8 4 2 1 Sum the values together to convert the binary value back to decimal. Now, I need to do the same but with a stack this time. Numbers can be between MAX_INT and 0. In c++, using a Queue, input an integer between 0 and 255. Convert it to binary by storing each binary digit in the queue. Example: 1 1 1 1 1 1 1 1 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0 128 64 32 16 8 4 2 1 Sum the values together to convert the binary value back to decimal. Now, I need to do the same but with a stack this time. Numbers can be between MAX_INT and 0.

Explanation / Answer

Here let us first create a stack.cpp file which can be included as library in our main program(this is just to make code more clear) , in this class we are just creating basic functions of stack

#include <iostream>
using namespace std;

const int MAX_SIZE = 100;

class stack
{
private:
int numOfItems;  
int element[MAX_SIZE];
public:
stack();
~stack();
void push(const int item);  
   int pop();      
int getnumOfItems() const;          
};

stack::stack()  
{
   numOfItems = 0;  
}

  
void stack::push(const int item)
{
   if (numOfItems < MAX_SIZE)
   {
       element[numOfItems] = item;
       ++numOfItems;
   }
   else cout << "Overflow! ";
}

int stack::pop()
{
   if (numOfItems >0)
   {
       --numOfItems;
       return (element[numOfItems]);
   }
   else
   {
       cout << "Underflow! ";
       return 0;
   }
}

int stack::getnumOfItems() const
{
   return numOfItems;
}

//Now our main class

#include<iostream>
using namespace std;
#include "stack.h"

int main()
{
int number, sum = 0;
cout << "Please enter a decimal: ";
cin >> num;
stack st; //Here we are creating object of stack class defined in previous stack.cpp file
while(num > 0)
{
sum = number % 2;
number /= 2;
st.push(sum);
  
}
while(st.getnumOfItems() > 0)
{
cout << st.pop();
}
cout << endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote