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 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
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...
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>)
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it into a binary tree, such that each operation is stored in a node whose left subtree stores the left operand, and whose right subtree stores the right operand.
Write a C function str_to_float() that takes a numeric string as input and converts it to...
Write a C function str_to_float() that takes a numeric string as input and converts it to a floating point number. The function should return the floating point number by reference. If the conversion is successful, the function should return 0, and -1 otherwise (e.g. the string does not contain a valid number). The prototype of the function is given below int str_to_float(char * str, float * number);
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