Question

In: Computer Science

C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This...

C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This function takes a single parameter, a non-negative integer, and returns a string corresponding to the binary representation of the given value.

  • Your function should be named decimalToBinaryRecursive
  • Your function should take a single argument
    • An integer to be converted to binary
  • Your function should not print anything
  • Your function should use recursion instead of a loop. Note that if you try to use a loop, your code will get an error message.
  • Your function should return the binary representation of the given value as a string.


For Example:

  • decimalToBinaryRecursive(0) should return "0"
  • decimalToBinaryRecursive(1) should return "1"
  • decimalToBinaryRecursive(8) should return "1000"

don't use this code as part of the answer

  string s = d == 0 ? "0" : "1";

Solutions

Expert Solution

#include <iostream>
#include <string>
using namespace std;

string decimalToBinaryRecursive(int n) {
    if(n == 0) {
        return "0";
    } else if(n == 1) {
        return "1";
    } else {
        int d = n % 2;
        string s;
        if (d == 0) {
            s = "0";
        } else {
            s = "1";
        }
        return decimalToBinaryRecursive(n/2) + s;
    }
}

int main() {
    cout << decimalToBinaryRecursive(0) << endl;
    cout << decimalToBinaryRecursive(1) << endl;
    cout << decimalToBinaryRecursive(8) << endl;
    return 0;
}




Related Solutions

Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are not allowed to use library functions for conversion. The output should look exactly as follows: DECIMAL      BINARY                     HEXDECIMAL                      BCD 0                      0000 0000                   00                                            0000 0000 0000 1                      0000 0001                   01                                            0000 0000 0001 2                      0000 0010                   02                                            0000 0000 0010 .                       .                                   .                                               . .                       .                                   .                                               . 255                  1111 1111                   FF                                            0010 0101 0101
Write a user-defined MATLAB function that converts real numbers in decimal form to binary form. Name...
Write a user-defined MATLAB function that converts real numbers in decimal form to binary form. Name the function b = deciTObina (d), where the input argument d is the number to be converted and the output argument b is a 30-element-long vector with 1s and 0s that represents the number in binary form. The first 15 elements of b store the digits to the left of the decimal point, and the last 15 elements of b store the digits to...
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
C programming Rewrite the following function using no loops, and only tail call recursion double question5...
C programming Rewrite the following function using no loops, and only tail call recursion double question5 (int in) { int i; int result; for (result = rand(), i = 0; i < in; i += 3) { result /= i; result += rand(); } return result; }
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...
In C++, Do some research to find the definitions of single recursion, binary recursion, and multiple...
In C++, Do some research to find the definitions of single recursion, binary recursion, and multiple recursion. What kinds of problems lend themselves to these different forms of recursion? Please be as detailed as possible. Thank you!
Use recursion function to derive computation time for Binary Search by drawing a recursion tree diagram...
Use recursion function to derive computation time for Binary Search by drawing a recursion tree diagram and using algebra calculation.
-> > Use recursion to write a C++ function for determining if a strings has more...
-> > Use recursion to write a C++ function for determining if a strings has more vowels than consonants. Please include the pseudo code so that I can understand better with simple English as much as possible.
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT