Page 1 of 1

Write a recursive function to implement the recursive algorithm of Exercise (reversing the elements of an array between

Posted: Tue Jul 12, 2022 8:15 am
by answerhappygod
Write a recursive function to implement the recursive algorithm of Exercise (reversing the
elements of an array between two indices). Also, write a program to test your function.
Exercise
Write a recursive function to implement the recursive algorithm of Exercise (multiplying two
positive integers using repeated addition). Also, write a program to test your function.
Exercise
Write a recursive function to implement the recursive algorithm of Exercise (determining the
number of ways to select a set of things from a given set of things). Also, write a program to test
your function.
Exercise
In the section "Converting a Number from Decimal to Binary," in this chapter, you learned how to
Is convert a decimal number into the equivalent binary number. Two more number systems, octal
(base 8) and hexadecimal (base 16), are of interest to computer scientists. In fact, in C++, you
can instruct the computer to store a number in octal or hexadecimal.
The digits in the octal number system are 0, 1,2, 3, 4, 5, 6, and 7. The digits in the hexadecimal
number system are 0, 1,2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. So A in hexadecimal is 10 in
decimal, B in hexadecimal is 11 in decimal, and so on.
The algorithm to convert a positive decimal number into an equivalent number in octal (or
hexadecimal) is the same as discussed for binary numbers. Here, we divide the decimal number
by 8 (for octal) and by 16 (for hexadecimal). Suppose ab represents the number a to the base b.
For example, 7510 means 75 to the base 10 (that is decimal), and 8316 means 83 to the base 16
(that is, hexadecimal). Then:
75310 = 13618
75310 = 2F116
The method of converting a decimal number to base 2, or 8, or 16 can be extended to any
arbitrary base. Suppose you want to convert a decimal number n into an equivalent number in
base b, where b is between 2 and 36. You then divide the decimal number n by b as in the
algorithm for converting decimal to binary.
Note that the digits in, say base 20, are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, G, H, I, and J.
Write a program that uses a recursive function to convert a number in decimal to a given base b
where b is between 2 and 36. Your program should prompt the user to enter the number in
decimal and in the desired base.
Test your program on the following data:
9098 and base 20
692 and base 2
753 and base 16