Question

C++ Write a function that returns a string of 1's and 0's. This needs to be...

C++
Write a function that returns a string of 1's and 0's. This needs to be the binary representation of a number.
Do not use loops. Use only recursion. Do not change the function's parameters or add more parameters. Do not change the main program.


#include
#include

string bin(int number)
{
//Code here
}

int main()
{
   cout << bin(43) << endl; // Should be 101011
   return 0;
}

Homework Answers

Answer #1
#include <iostream>
#include <string>

using namespace std;

string bin(int number) {
    if (number == 0) {
        return "0";
    } else if (number == 1) {
        return "1";
    } else {
        string s = bin(number / 2);
        if (number % 2 == 0) {
            s += "0";
        } else {
            s += "1";
        }
        return s;
    }
}

int main() {
    cout << bin(43) << endl; // Should be 101011
    return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ Write a recursive function that reverses the given input string. No loops allowed, only use...
C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. I had asked this before but the solution I was given did not work. #include #include using namespace std; void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sammy";    reverse(name);    cout << name << endl; //should display...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
Define a function SetHeight, with int parameters feetVal and inchesVal, that returns a struct of type...
Define a function SetHeight, with int parameters feetVal and inchesVal, that returns a struct of type WidthFtIn. The function should assign WidthFtIn's data member numFeet with feetVal and numInches with inchesVal. #include <iostream> using namespace std; struct WidthFtIn {    int numFeet;    int numInches; }; /* Your code goes here */ int main() {    WidthFtIn objectSize;    int feetVal;    int inchesVal;    cin >> feetVal >> inchesVal;    objectSize = SetHeight(feetVal, inchesVal);    cout << "Size: "...
6.25.1: Unit testing C++ Add two more statements to main() to test inputs 3 and -1....
6.25.1: Unit testing C++ Add two more statements to main() to test inputs 3 and -1. Use print statements similar to the existing one (don't use assert). #include <iostream> using namespace std; // Function returns origNum cubed int CubeNum(int origNum) { return origNum * origNum * origNum; } int main() { cout << "Testing started" << endl; cout << "2, expecting 8, got: " << CubeNum(2) << endl; /* Your solution goes here */ cout << "Testing completed" << endl;...
C++ question. Please explain the code and how it resulted in the output. Explain each line...
C++ question. Please explain the code and how it resulted in the output. Explain each line along with the aspects of "buffers" if possible. Everything is listed below. Code below: #include <iostream> #include <string> using namespace std; int main() {    cout << boolalpha;    cout << static_cast<bool>(1) << endl;    cout << static_cast<bool>(0) << endl;    string s = "AAARGH!!!";    if (s.find("AAA")) { cout << 1 << endl; }    if (s.find("RGH")) { cout << 2 << endl;...
Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow...
Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example output for userNum = 40: 20 10 5 2 1 ..... my code: #include <iostream> using namespace std; int main() { int userNum; userNum = 40; /* Your solution goes here */ while (userNum != 1){ userNum = userNum/2; cout << userNum << " ";   } cout << endl; return 0; } ........ but as a result i...
Write a C++ program that processes orders for our company Widgets and More. Your program will...
Write a C++ program that processes orders for our company Widgets and More. Your program will read the product code and quantity for all the products in an order from a file, order.txt. You will be given a sample order.txt with the starter code. There are two items on each line of the file the first is a letter that represents the product and the second is an integer representing the number bought. Based on the product and quantity, you...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns the number with its digits reversed, then printout the return result. For example, the value of reverseDigit(12345) is 54321; the value of reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532) is -235.    Modify the following program to make a correct output. /* // Name: Your Name // ID: Your ID // Purpose Statement: ~~~ */ #include <iostream>...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are pointers or constant pointers. Directions: Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS = 3; // may only be declared within the main function string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Be sure to compile using g++ -std=c++11 helpWithGradesPtr.cpp Write a C++ program to run a menu-driven program with the...
C++ visual studios this function has to run twice. and the "count" variable needs to be...
C++ visual studios this function has to run twice. and the "count" variable needs to be "2" on the second run. How can i do that? The parameters can not change as well void printArray(const int *array, int n) {    int count = 1;    char check;    if (check == 'k')    {        count++;    }    cout << "Value " << count << " array contents." << endl;    cout << "------------------------" << endl;   ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT