Question: in C++ you are going to use single dimensional array and a pointer to implement a stack. A stack is a list which has elements
in C++ 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.
Write your functions such that you can convert a number of any base to a number in any other base (Assume only the above 4 bases). So, you should be able to input a binary number and convert it to an octal equivalent or any other combination.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
