Welcome to Westonci.ca, the Q&A platform where your questions are met with detailed answers from experienced experts. Explore in-depth answers to your questions from a knowledgeable community of experts across different fields. Get detailed and accurate answers to your questions from a dedicated community of experts on our Q&A platform.

Can someone help me out I don't understand how to do this problem at all
---------------------------------------------------------------------------------------------------------------------------
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:

As long as x is greater than 0
Output x % 2 (remainder is either 0 or 1)
x = x / 2

Your program must define and call the following two functions. The IntegerToReverseBinary() function should return a string of 1's and 0's representing the integer in binary (in reverse). The ReverseString() function should return a string representing the input string in reverse.

string IntegerToReverseBinary(int integerValue)
string ReverseString(string userString)
----------------------------------------------------------------------------------------------------------------------------
here is the general format it gave:

#include
using namespace std;

/* Define your functions here */

int main() {
/* Type your code here. Your code must call the functions. */

return 0;
}


Sagot :

tonb

Answer:

#include <bits/stdc++.h>

#include <string>  

using namespace std;

string IntegerToReverseBinary(int integerValue)

{

 string result;

 while(integerValue > 0) {

   result += to_string(integerValue % 2);

   integerValue /= 2;

 }

 return result;

}

string ReverseString(string userString)

{

 reverse(userString.begin(), userString.end());

 return userString;

}

int main() {

 string reverseBinary = IntegerToReverseBinary(123);

 string binary = ReverseString(reverseBinary);

 cout << binary << endl;

 return 0;

}

Explanation:

The string reverser uses a standard STL function. Of course you could always write your own, but typically relying on libraries is safer.