Converting Decimal Number to Binary Number using stack

1.8k views Asked by At

I am trying to convert a decimal number to binary number using stacks and I have to use structs.

Now from my understanding of structs, we can have member functions in them. And stack follows a LIFO strategy.

We can create a stack by making a struct, declaring some members, and have some functions to initialize and work on these members.

So, I tried declaring a struct with above mentioned things and my understanding, but I still do not seem to get the concept right. I believe it is an error on my part. But reading forums still does not help as we have not been taught classes yet and on every forum, structs and classes are intermixed.

Here is my code so far, any help and guidance regarding the concept and logic would be much appreciated.

#include<iostream>
using namespace std;

struct bin{
int num[15];
int ci;

void init()
{
    ci = 0;
    for (int i = 0;i < 15;i++)
        num[i] = -1;
}

void push(int n)
{
    num[ci] = n;
    ci++;
}
int pop()
{
    int temp = num[--ci];
    num[ci] = -1;
    return temp;
}
};

int main()
{
int inp, count = 0;
bin var;
cout << "Enter a decimal number to convert into binary: ";
cin >> inp;

while (inp != 0)
{
    int rem = inp % 2;
    cout << "rem= " << rem << endl;
    inp /= 2;
    cout << "inp= " << inp << endl;
    var.push(rem);
    count++;
}
cout << "\nYour binary is: ";
while (count != 0)
{
    cout << var.pop();
    count--;
}

return 0;
}

I tried my best to find the mistakes but couldn't. So,in the end I simply used arrays and implemented the code as follows

#include<iostream>
using namespace std;

void push(int bin[], int n, int &ci);
void init(int bin[], int &count, int &ci);
int pop(int bin[], int &ci);
void display(int bin[], int &count, int &ci);
void findBinary(int bin[], int &count, int&ci, int &inp);

int main()
{
int inp, count, ci;
int bin[20];
char c = '\0';

init(bin,count,ci);

    cout << "Enter a decimal number to convert into binary: ";
    cin >> inp;

    findBinary(bin, count, ci, inp);
    display(bin, count, ci);

return 0;
}

void init(int bin[], int &count, int &ci)
{
count = 0;
ci = 0;

for (int i = 0;i < 20;i++)
    bin[i] = -1;
}

int pop(int bin[], int &ci)
{
    int temp = bin[--ci];
    return temp;
}

void push(int bin[], int n, int &ci)
{
    bin[ci] = n;
    ci++;
}

void display(int bin[], int &count, int &ci)
{
    cout << "\nYour binary is: ";
    while (count != 0)
    {
        cout << pop(bin, ci);
        count--;
    }
    cout << endl;
}

void findBinary(int bin[], int &count, int&ci, int &inp)
{
    while (inp != 0)
    {
        int rem = inp % 2;
        inp /= 2;
        push(bin, rem, ci);
        count++;
    }
}

So my questions are: 1. When we write a function within a struct, does the function runs when we create a struct type object? 2. Is the method I used to implement stack using struct in the first example correct?

2

There are 2 answers

0
hnefatl On

When you first create an object from a struct or class, the constructor runs - but no other function (so not your init function). If you want that to run, you need to call it explicitly:

bin var;
var.init();

In terms of the implementation of your functions: you should use bounds-checking to ensure that you don't push more items than you can handle, or pop more than are in the queue. Besides that I can't see much wrong with them. Consider using a std::vector instead of an array, but as you're just starting to learn about C++, you'll likely cover that later in your course.


By the time you're adding constructors to a struct though, you may as well switch to using a class. In C++, structs and classes are equivalent (with the minor tweak that the default access modifier in a struct is public, whereas in classes it's private), so don't let that put you off.

1
Betelhem Mesele On

why don't you do the problem in this way ,I fixed it just like this .

#include

using namespace std;

#define MAX_STACK 10

int top=-1;

struct bin

{

int num[MAX_STACK];

bool isEmpty()

{

return (top < 0); }

void push(int newItem) {

// if stack has no more room for // another item

if (top >= MAX_STACK-1)

cout<<"Stack Overflow!";

else{

++top;

num[top] = newItem;

}

}

int pop()

{

if(top < 0)

{

//top points to nothing i.e stack is empty

cout << "Stack Underflow \n";

return 0; }

else

{

/*decrement the top pointer and it

points to the next top element in the stack*/

int d = num[top--];

return d;

}

}

};

int main()

{

int value, count = 0;

bin var;

int rem;

cout << "Enter a decimal number to convert into binary: ";

cin >> value;

while (value != 0)

{ rem = value % 2;

cout << "rem= " << rem << endl;

var.push(rem);

count++;

value /= 2; }

cout << "\nYour binary is: ";

while (count != 0) {

cout << var.pop();

count--;

}

return 0;

}