Question

In: Computer Science

In C++, type a function function(int n, int base) that converts a positive integer x to...

In C++, type a function function(int n, int base) that converts a positive integer x to any base between 2 and 9. The function HAS to do this using a stack, and using methods from below:

+isEmpty(): boolean

+push(newEntry: ItemType): boolean

+pop(): boolean

+peek(): ItemType

(Also, type a program to test the function).

Hint: could use simple iteration continually divides the decimal number by base and keeps track of the remainder by using a stack.

Solutions

Expert Solution

Program Code Snapshot

Program Sample Input/Output Screenshot

Program Code to copy

#include <bits/stdc++.h>
using namespace std;

// declare Stack class with given methods
class Stack
{
public:
        vector<int> data;
        void push(int val)
        {
                data.push_back(val);
        }
        int pop()
        {
                int val = data.back();
                data.pop_back();
                return val;
        }
        int peek()
        {
                return data.back();
        }
        bool isEmpty()
        {
                return data.size() == 0;
        }
};

// converts a positive integer x to any base between 2 and 9
int func(int n, int base)
{
        //  could use simple iteration continually divides the decimal
        // number by base and keeps track of the remainder by using a stack.
        Stack stack;
        while (n)
        {
                int remainder = n % base;
                stack.push(remainder);
                n /= base;
        }
        int result = 0;
        //pop stack and fill result
        while (!stack.isEmpty())
        {
                result = result * 10 + stack.pop();
        }
        return result;
}

int main()
{
        //program to test our function
        int n, base;
        cout << "Enter number to convert: ";
        cin >> n;
        cout << "Enter base to covert in: ";
        cin >> base;
        cout << n << " is base " << base << " = " << func(n, base);
}

Related Solutions

Write a C function boolean isPrime (int n), that would take a positive integer n as...
Write a C function boolean isPrime (int n), that would take a positive integer n as a parameter and return true or false whether the number is a prime number. You should check the range of n and print proper message (For example, if n is a genitive number, you should print out an error message).
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the...
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the corresponding binary number. Do the conversion by repeatedly dividing the number n by 2 using integer division, keepting track of the remainders, until the number is reduced to 0. The remainders written in reverse order form the binary number string. Do integer division of 5 by 2, so that N//2 is 2 with remainder 1. Now divide 2 by 2 to get 1 with...
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer:...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer: ");     scanf("%d", &number);     result = sum(number);     printf("sum = %d", result);     return 0; } int sum(int n) {     if (n != 0)         return n + sum(n-1);     else         return n; } What does the code above do?
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts...
c++ programming: Write a function called baseConverter(string number, int startBase, int endBase) in c++ which converts any base(startBase) to another (endBase) by first converting the start base to decimal then to the end base. Do not use library. (bases 1-36 only)
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n == 0) return 0; else if (n==1) return 1; else return fib(n-1) + fib (n-2); } For this programming assignment, write and test an ARMv8 program to find Fibonacci (n). You need to write a main function that calls the recursive fib function and passes an argument n. The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie...
Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie Germain prime that is greater or equal to n.
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and v = 17,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT