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 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...
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...
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,...
Let f(x) = {(C/x^n if 1≤ x <∞; 0 elsewhere)} where n is an integer >1....
Let f(x) = {(C/x^n if 1≤ x <∞; 0 elsewhere)} where n is an integer >1. a. Find the value of the constant C (in terms of n) that makes this a probability density function. b. For what values of n does the expected value E(X) exist? Why? c. For what values of n does the variance var(X) exist? Why?
Let n be a positive integer. Prove that if n is composite, then n has a...
Let n be a positive integer. Prove that if n is composite, then n has a prime factor less than or equal to sqrt(n) . (Hint: first show that n has a factor less than or equal to sqrt(n) )
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer value n and returns an approximation of e as (1 + 1/n)^n I am not even sure what the question is asking me to do but I think it is asking me to code that function. Does anyone know how to do this?
c ++ program that converts from any base to a decimal number
c ++ program that converts from any base to a decimal number
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT