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;
}

New Topic/Question
Reply




MultiQuote




|