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?
When you first create an object from a
structorclass, the constructor runs - but no other function (so not yourinitfunction). If you want that to run, you need to call it explicitly: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::vectorinstead 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
structthough, you may as well switch to using aclass. In C++, structs and classes are equivalent (with the minor tweak that the default access modifier in a struct ispublic, whereas in classes it'sprivate), so don't let that put you off.