stack and pointers with binary conversion

Page 1 of 1

5 Replies - 3347 Views - Last Post: 21 February 2008 - 06:56 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=43647&amp;s=f15a1e4f3d98f3ac219fdbc6fcd90bae&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 jajuje2000

Reputation: 0
• Posts: 39
• Joined: 14-February 08

stack and pointers with binary conversion

Posted 18 February 2008 - 12:22 PM

hi again, I need help with using pointers from an array with the size of 100 with stack method. i only written the binary conversion and stack methods. so please help with the rest thanks.

1. I will need to create A stack which has elements inserted or deleted on the top of the list. It is a first in last out ordering of elements. I will maintain a pointer to the top of the stack that is an array. Write functions for inserting an element (call it push), deleting an element (call it pop), checking if the stack is full ( call it check_full) and checking if it is empty (call it check_empty).

2. An integer variable p that holds the subscript value of the array is a pointer to stack. The value of p is the location where the next element should be inserted or removed. The value of p, the array itself and the element to be inserted should be passed to the push function. The array and the address of p should be passed to the pop function, which should return the top most element in the stack. Use these functions of a stack to convert numbers from base 10 to base 2(binary), base 8(Octal) or base 16(hexadecimal). Given a number 100, the following process can be used to convert it to base 2. Divide 100 by 2, push the remainder into the stack, divide the quotient by 2 and push the remainder into the stack and keep doing this(dividing the quotient by 2) till the quotient is 0.

3. Then pop all elements (print them out when a number is popped from the stack and the number you get is the binary representation of 100. To get the hexadecimal representation of 100, divide the number by 16 till the quotient is 0. For a hexadecimal representation, i have to use characters A, B, C, D, E and F for values from 10 through 15 respectively.

The main part of the program should ask for the number and the new base and have a while loop in which you do the mathematics and call the function push. Have another while or for loop to do the pop operations.

```
#include <iostream>

using namespace std;

#define MAX 100		// MAXIMUM STACK CONTENT

class stack
{

private:
int arr[MAX];// Contains all the Data

int top;		//Contains location of Topmost Data pushed onto Stack

public:
stack()		 //Constructor
{
top=-1;	  //Sets the Top Location to -1 indicating an empty stack
}

void push(int a)  // Push ie. Add Value Function
{
top++;		// increment to by 1
if(top<MAX)
{
arr[top]=a;  //If Stack is Vacant store Value in Array
}
else
{
cout<<"STACK FULL!!"<<endl;
top--;
}
}

int pop()				  // Delete Item. Returns the deleted item
{
if(top==-1)
{
cout<<"STACK IS EMPTY!!!"<<endl;
return NULL;
}
else
{
int data=arr[top];	 //Set Topmost Value in data
arr[top]=NULL;	   //Set Original Location to NULL
top--;			   // Decrement top by 1
return data;		 // Return deleted item
}
}
};

int main()
{

stack a;
char arr[100];
int index = 99;
arr[index] = '\0';
int p;

cout << "what number do you want to use for conversion? \n" << endl;
cin >> p;

int neg;
neg = 0;
if(p < 0) {
neg = 1;
p *= -1;

}
if (p <= 100)
{
do
{
int d;
d = p % 2;
arr[--index] = (char)(d + '0');

p = p >> 1;
}
while( p > 0);

if(neg) {
arr[--index] = '-';
}
cout << "\ncongrats your binary number is: \n" << endl;
cout << &arr[index] << endl;
}
else
{
cout << "Invalid operation choice." << endl;
}

int n, r[10], i;
cout << "enter a number to convert to hexadecimal: " << endl;
cin >> n;

for(int i=0;n!=0;i++)
{
r[i]=n%16;
n=n/16;
}
i--;
for(;i>=0;i--)
{
if(r[i]==10)
cout << "A";
else if(r[i]==11)
cout << "B";
else if(r[i]==12)
cout << "C";
else if(r[i]==13)
cout << "D";
else if(r[i]==14)
cout << "E";
else if(r[i]==15)
cout << "F";
else
cout << "%d" << r[i] << endl;

}
cout << "\n" << endl;

return 0;
}

```

Is This A Good Question/Topic? 0

Replies To: stack and pointers with binary conversion

#2 jajuje2000

Reputation: 0
• Posts: 39
• Joined: 14-February 08

Re: stack and pointers with binary conversion

Posted 18 February 2008 - 07:35 PM

can anyone help me???

This post has been edited by jajuje2000: 19 February 2008 - 12:21 AM

#3 jajuje2000

Reputation: 0
• Posts: 39
• Joined: 14-February 08

Re: stack and pointers with binary conversion

Posted 19 February 2008 - 07:43 PM

alrigh then ill try to finish it up myself then.

#4 Nayana

• DIC Hawk - 나야나 नयन:

Reputation: 31
• Posts: 824
• Joined: 14-November 07

Re: stack and pointers with binary conversion

Posted 19 February 2008 - 07:59 PM

First in last out is easy.

Just have an array of all your stack elements.
Have an int that points to the current index.

If the stack is empty, the int should be -1

#5 Nayana

• DIC Hawk - 나야나 नयन:

Reputation: 31
• Posts: 824
• Joined: 14-November 07

Re: stack and pointers with binary conversion

Posted 19 February 2008 - 08:13 PM

When you add an element, first increase the index. Then add the element.
To remove an element, delete the element, then decrease the index.

#6 jajuje2000

Reputation: 0
• Posts: 39
• Joined: 14-February 08

Re: stack and pointers with binary conversion

Posted 21 February 2008 - 06:56 AM

cool man thanks for the tip

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }