BleepingComputer.com: C++ Stacks Tutorial

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

C++ Stacks Tutorial Convert a decimal to binary

#1 User is offline   Iscariot 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 42
  • Joined: 17-October 08

Posted 07 November 2008 - 09:19 PM

Hello World.
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 User is offline   Crizz44 

  • Senior Member
  • PipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 496
  • Joined: 13-August 05
  • Gender:Female
  • Location:Virginia

Posted 07 November 2008 - 09:35 PM

I don't know anything about the C++ code , but see if this will help :thumbsup:

http://freecode-freecode.blogspot.com/2007...rt-decimal.html

#3 User is offline   Iscariot 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 42
  • Joined: 17-October 08

Posted 09 November 2008 - 02:33 AM

This is what I have so far for my program. Once I have this working, I'll convert it to a stack program.
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 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 09 November 2008 - 08:44 AM

It is telling you that there is going to be a loss of precision because the operation pow(2.0,power) is going to return a double. You are trying to assign a floating point value to an int, which doesn't work. Maybe try assigning the result of the operation to a double instead.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#5 User is offline   Iscariot 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 42
  • Joined: 17-October 08

Posted 09 November 2008 - 12:58 PM

Now its compiling, but it seems to be stuck in an infinite loop.
New code below \/

This post has been edited by Iscariot: 09 November 2008 - 04:00 PM


#6 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 09 November 2008 - 01:11 PM

Quote

seems to be stuck in an infinite loop.


Seems to be, or is?
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#7 User is offline   Iscariot 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 42
  • Joined: 17-October 08

Posted 09 November 2008 - 01:23 PM

It asks me for a decimal number, so I input a number to convert to binary.
After entering the number it sits there. I would assume its an infinite loop.

#8 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 09 November 2008 - 01:53 PM

I edited your posts to remove the nested quotes. I am a pretty sharp guy, so I don't need to be reminded of what I just said, especially since I can just scroll up a bit if I forget. :thumbsup:

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.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#9 User is offline   Iscariot 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 42
  • Joined: 17-October 08

Posted 09 November 2008 - 02:04 PM

For [cout << "\n" << decimal << " " << bit << " " << binaryConverter;] after declaring the variable bit in function decToBin, I received the following output for an input of 23:
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


#10 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 09 November 2008 - 02:57 PM

Why are you initializing power to 20?
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#11 User is offline   Iscariot 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 42
  • Joined: 17-October 08

Posted 09 November 2008 - 03:20 PM

A power of 20 would yield a maximum of 1048576, and I don't see why I would need to convert a number larger than this. If I needed to, I could make the number larger, but for now I'm just using 20 just to get the program to work.

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 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 09 November 2008 - 03:55 PM

I guess I don't understand exactly what the application is supposed to do then. I thought you were converting a binary number to a decimal. I am going to be giving my students an assignment like this in a few weeks. At any rate, if that is what you are trying to do here, then power should be initialized to the length-1 of the input string, since it is binary to decimal conversion requires that the list be zero indexed. I do not particluarly care for do-while loops because as you already discovered, it is too easy to go into an infinite loop. I prefer a for loop that just loops over the input.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#13 User is offline   Iscariot 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 42
  • Joined: 17-October 08

Posted 09 November 2008 - 04:07 PM

The problem I'm doing is on page 1178, problem 8, of C++ Programming, program design including data structures by D.S. Malik

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 User is offline   Iscariot 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 42
  • Joined: 17-October 08

Posted 09 November 2008 - 06:41 PM

I decided to work on the other problem, which I made good progress on until I ran into a problem. I'm trying to find the prime number multiples of a user-inputted number. At the moment I can get it to output all the multiples of the number. My problem is in the prime function. I need to get it to output ONLY the prime multiples of the number, so something like 4 is not a prime multiple of 16, only 1, 2, and 16 are.
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


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users