Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in C++ Define a function string toBaseB(int n, int b); That returns the base b respresentation of n. Your function should work for any b

in C++

Define a function string toBaseB(int n, int b); That returns the base b respresentation of n. Your function should work for any b in the set {2,3,...,36}. If b is out of that range, then your function should return "Error".

Read the following carefully because it's telling you how to write the function...

If b is less than 2 or greater than 36 your function will return the string "Error".

The other base case will be when b in range and n is zero. In that case just return the string "0". For the recursive case: Call the function with n replaced by n/b. Assign the result to a variable called temp. If temp is equal to the string "0" then change temp to the empty string "" . (By doing that you will avoid an answer with leading zeroes.) Now get the value of the last digit using n%b. Assign that to a variable of type int called digit. Note that digit will be always be a value between 0 and b-1. We need to convert digit into a char now. That's done by using digit + '0' if digit is from 0 to 9 or (digit - 10) + 'A' if digit is bigger than 9. (That's because digits after 9, are represented by 'A', 'B' 'C' etc.) To handle negative numbers, call the function recursively on -n, save the result into temp then return "-" + temp (i.e. put "-" on the front).

Examples: the number 3131 base 36 would be represented by "2EZ" and -3131 would be represented by "-2EZ". Use recursion. (No loops allowed.) .

The driver used to test your code is below:

string toBaseB(int n, int b) ;

int main() {

int n;

int b;

cin >> n >> b;

cout << toBaseB(n,b) << endl;

return 0;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Oracle8I Building Efficient Databases

Authors: Jonathan Lewis

1st Edition

0201715848, 978-0201715842

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago