C++ Stacks Tutorial Convert a decimal to binary
#1
Posted 07 November 2008 - 09:19 PM
I'm looking for a small tutorial concerning stacks in C++.
I have an assignment where I need to write a program that uses a stack to convert a decimal number into an equivalent binary number.
Example- 12 = 1100, 52 = 110100, 3 = 11, etc.
I have very little experience with stacks.
I ask for the community's advice rather than giving my eye for a drink from the well. (Norse reference)
#2
Posted 07 November 2008 - 09:35 PM
http://freecode-freecode.blogspot.com/2007...rt-decimal.html
#3
Posted 09 November 2008 - 02:33 AM
My main problem atm preventing the program from compiling is [binaryConverter = pow(2.0,power);] I receive an error message telling me an improper convert to `int' from `double'.
Suggestions?
[Old Cold Deleted]
This post has been edited by Iscariot: 09 November 2008 - 10:34 PM
#4
Posted 09 November 2008 - 08:44 AM
#5
Posted 09 November 2008 - 12:58 PM
New code below \/
This post has been edited by Iscariot: 09 November 2008 - 04:00 PM
#7
Posted 09 November 2008 - 01:23 PM
After entering the number it sits there. I would assume its an infinite loop.
#8
Posted 09 November 2008 - 01:53 PM
As far as your issue, at what point does bit==0? I am assuming that eventually in the part where it says bit = bit - binaryConverter;, you are expecting it to count down to zero eventually. What I would do if I were you is to print out the value of bit, print out the value of binaryConverter, and print out the result once you subtracted binaryConverter from bit, just to make sure what you think is happening, really is happening. And right before the while statement, I would pring out the content of bit again, just to make sure. You should quickly narrow down the problem, which I suspect is that bit never equals zero. I bet at some point it becomes less than zero though.
#9
Posted 09 November 2008 - 02:04 PM
23 0 1.04858e+006
I changed double bit = binaryNumber to double bit = decimal
output is now
23 23 1.04858e+006
Apparently its something to do with my pow statement.
This post has been edited by Iscariot: 09 November 2008 - 02:07 PM
#11
Posted 09 November 2008 - 03:20 PM
I figured out why its outputting the exponential number.
Once it reaches 7 digits to output the program will display #.#####e# to save space or something. I originally thought it was just trash data being displayed. I'll just work off of 10.
Ok, this code gives me the outputs I asked it for. Problem is, it isn't creating 1s (20 = 0000010100, getting all 0s)
[Old Code deleted]
This post has been edited by Iscariot: 09 November 2008 - 10:33 PM
#12
Posted 09 November 2008 - 03:55 PM
#13
Posted 09 November 2008 - 04:07 PM
This is the actual problem:
Write a program that uses a stack to convert a decimal number into an equivalent binary number.
I don't know how to do stacks, so I was trying to do recursion to at least get a program that can convert the number.
I do have a choice of problems I can do, I'm thinking of trying a different one, like...
Write a program that uses a stack to print the prime factors of a positive integer in descending order.
Once again, I don't understand stacks fully, and this is a big problem. I need to have the program done by 11:00 am tomorrow.
This post has been edited by Iscariot: 09 November 2008 - 04:21 PM
#14
Posted 09 November 2008 - 06:41 PM
At the moment I'm trying to do something with an array that will store the multiples of the number in them then I will check each number individually. If a number is not prime it will be set to 0 then I will output all numbers of the array that are not 0.
Edit. Ok, I have the array set up, now I just need to check for prime numbers.
Edit2. I have my program working. Now I need to make it work using stacks. Code is below. Help appreciated in converting it.
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
void input(int& number);
void prime(int number);
int main()
{
int number;
bool rerun;
char response;
do
{
rerun = false;
input(number);
prime(number);
cout << "\nCheck another number? Y/N: ";
cin >> response;
if(response == 'Y' || response == 'y')
{
rerun = true;
system("cls");
}
else
{
system("PAUSE");
return 0;
}
}
while(rerun);
}
void input(int& number)
{
bool negative;
do
{
negative = true;
cout << "Enter a number greater than zero: ";
cin >> number;
if (number <= 0)
{
cout << "\nError. Number is either negative or zero.";
cout << "\nEnter another number.\n";
negative = true;
}
else
{
cout << "\nCalculating multiples of " << number << " . . .\n";
negative = false;
}
}
while (negative);
}
void prime(int number)
{
int counter = number;
int modNum;
int myArray[number];
int i = 0;
int arrayCount;
int arrayCompare;
int numberClone;
do
{
modNum = number % counter;
if (modNum == 0)
{
myArray[i] = counter;
cout << myArray[i] << " ";
i++;
counter--;
}
else
{
counter--;
}
}
while (counter > 0);
cout << endl;
arrayCount = i;
cout << "There are " << arrayCount << " multiples of " << number << endl;
cout << "The prime factors of " << number << " are: ";
for (int x = 0; x < arrayCount; x++)
{
arrayCompare = myArray[x];
for (int y = 1; y <= number; y++)
{
if (arrayCompare % 2 == 0 && arrayCompare != 2)
{
myArray[x] = 0;
}
else if (arrayCompare % 3 == 0 && arrayCompare != 3)
{
myArray[x] = 0;
}
else if (arrayCompare % 5 == 0 && arrayCompare != 5)
{
myArray[x] = 0;
}
else if (arrayCompare % 7 == 0 && arrayCompare != 7)
{
myArray[x] = 0;
}
}
}
for (int i = 0; i < arrayCount; i++)
{
if (myArray[i] != 0)
{
cout << myArray[i] << " ";
}
}
cout << endl;
}
This post has been edited by Iscariot: 09 November 2008 - 10:32 PM

Help



Back to top









