C++ HELP!! Number Converter In this project you are going to use single dimensio
ID: 640962 • Letter: C
Question
C++ HELP!!
Number Converter
In this project you are going to use single dimensional array and a pointer to implement a stack. A stack is a list which has elements inserted or deleted on the top of the list. It is a first in last out ordering of elements. You 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). The check_full and check_empty functions are called from within push (before every push operation) and pop (before every pop operation) respectively. If the stack is full or empty, the element cannot be inserted or popped, hence print an error statement and exit the program at that point. Use an array of size 100.
An integer pointer 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 pointer p, the array itself and the element to be inserted should be passed to the push function. The array and the pointer 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.
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, you should 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.
Here's what I have so far:
#include
#include
using namespace std;
bool check_full(int[], int*);
bool check_empty(int stack[], int* p);
void push(int*, int[], int, int*, int);
void pop(int*, int[], int*);
int main()
{
const int SIZE = 100;
int stack[SIZE];
int* p = stack;
int* top = stack;
//setting entire stack to null
for (int setnull = 0; setnull < 99; setnull++)
{
*p = NULL;
p++;
}
//return pointer to top of the stack
p -= 99;
char run;
int toconversion, base;
do{
do
{
cout << "Do you want to convert a number? (y/n): ";
cin >> run;
cout << "What is the base you want to convert to? 2, 8, 16: ";
cin >> base;
cout << "Enter the number to be converted: ";
cin >> toconversion;
} while (base != 2 && base != 8 && base != 16);
if (base == 2)
{
cout << "Your decimal number in binary is: ";
while (toconversion / 2 != 0)
{
push(p, stack, toconversion, top, base);
toconversion /= 2;
pop(p, stack, top);
}
}
if (base == 8)
{
cout << "Your decimal number in binary is: ";
while (toconversion / 2 != 0)
{
push(p, stack, (toconversion % 2), top, base);
toconversion /= 2;
pop(p, stack, top);
}
}
if (base == 16)
{
cout << "Your decimal number in hecadecimal is: ";
while (toconversion / 2 != 0)
{
push(p, stack, (toconversion % 2), top, base);
toconversion /= 2;
pop(p, stack, top);
}
}
} while (run != 'n');
return 0;
}
void push(int* p, int stack[], int toconversion, int* top, int base)
{
if (check_full(stack, top) == true)
{
cout << "error stack is full ";
exit(1);
}
toconversion %= base;
*p = toconversion;
cout << *p;
if (base == 16 && *p == 10)
{
cout << "A";
}
else if (base == 16 && *p == 11)
{
cout << "B";
}
else if (base == 16 && *p == 12)
{
cout << "C";
}
else if (base == 16 && *p == 13)
{
cout << "D";
}
else if (base == 16 && *p == 14)
{
cout << "E";
}
else if (base == 16 && *p == 15)
{
cout << "F";
}
p++;
}
void pop(int* p, int stack[], int* top)
{
if (check_empty(stack, top) == true)
{
cout << "error stack is empty";
exit(1);
}
*p = NULL;
p--;
}
bool check_full(int stack[], int* p)
{
bool full;
for (int count = 0; count < 99; count++)
{
p++;
if (*p == NULL)
{
full = false;
}
else if (*p != NULL)
{
full = true;
}
}
p -= 99;
return full;
}
bool check_empty(int stack[], int* p)
{
bool full;
for (int count = 0; count < 99; count++, p++)
{
if (*p == NULL)
{
full = false;
}
else if (*p != NULL)
{
full = true;
}
}
p -= 99;
return full;
}
Explanation / Answer
#include #include using namespace std; bool check_full(int[], int*); bool check_empty(int stack[], int* p); void push(int*, int [], int, int*, int); void pop(int*, int [], int*); int main() { const int SIZE = 100; int stack[SIZE]; int* p = stack; int* top = stack; //setting entire stack to null for (int setnull = 0; setnull < 99; setnull++) { *p = NULL; p++; } //return pointer to top of the stack p -= 99; char run; int toconversion, base; do{ do { cout >run; cout > base; cout > toconversion; } while (base != 2 || base !=8 || base != 16); if (base == 2) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.