I'm reviewing fro my exams and on a multiple choice paper that I did my teacher commented that stacks would be the best data structure to use when converting a decimal to binary. Can someone please explain why this is so?
Using stacks to convert from decimal to binary
Started by J-Camz, Jun 09 2009 12:22 PM
5 replies to this topic
#1
Posted 09 June 2009 - 12:22 PM
|
|
|
#2
Posted 09 June 2009 - 12:28 PM
It depends somewhat on your algorithm, but in general you are using repeated modulus and integer division by 2. You store the modulus on the stack, where the first stored value is the 1's place, second is 2's, third is 4's, etc. Then you can pop the stack to display the digits in standard order.
#3
Posted 09 June 2009 - 12:32 PM
I see
Thank you!
I really appreciate the quick reply. :)
Thank you!
I really appreciate the quick reply. :)
#4
Posted 15 June 2009 - 08:43 AM
#5
Posted 15 June 2009 - 10:58 AM
Common stack are not that powerful .. use dynamic data structure . try coding the same program with link-list .. its will seem to be difficult but you will have greater control over the program
#6
Posted 15 June 2009 - 11:23 AM
I won't comment on your post awazdo...
#include <stack>
#include <iostream>
int MyNumber = 495;
int base = 2;
int main(void){
std::stack<int> stack;
while(MyNumber != 0){
int remainder = MyNumber%base;
int quotient = MyNumber/base;
stack.push(remainder);
MyNumber = quotient;
}
while(!stack.empty()){
std::cout << stack.top();
stack.pop();
}
return 0;
}


Sign In
Create Account


Back to top









