1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
irina1246 [14]
3 years ago
12

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 binar

y. 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
Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.
Ex: If the input is:
6
the output is:
110
Your program must define and call the following two functions. The function integer_to_reverse_binary() should return a string of 1's and 0's representing the integer in binary (in reverse). The function reverse_string() should return a string representing the input string in reverse.
def integer_to_reverse_binary(integer_value)
def reverse_string(input_string)
Note: This is a lab from a previous chapter that now requires the use of a function.
Computers and Technology
1 answer:
scZoUnD [109]3 years ago
6 0

Answer:

#include <iostream>//header file

#include <string>//header file

using namespace std;

string integer_to_reverse_binary(int integer_value)//defining a method integer_to_reverse_binary  

{

   string ret = "";//defining string variable

   while (integer_value > 0) //using while loop to check integer_value value is greater than 0

   {

       ret += '0' + (integer_value % 2);//adding zeros in remainder value

       integer_value /= 2;//holding quotient value

   }

   return ret;//return string value

}

string reverse_string(string input_string)//defining a method reverse_string that holds a parameter user_String  

{

   string result;//defining a string variable  

   for (int i = 0; i < input_string.length(); ++i)//use for loop to calculate value  

   {

       result += input_string[input_string.length()-i-1];//add value in result variable

   }

   return result;//result result variable value

}

int main()//defining main method  

{

   int num;//defining integer variable

   string str;//defining string variable

   cin >> num;//input num value

   str = integer_to_reverse_binary(num);//use str variable to call the integer_to_reverse_binary method

   cout << reverse_string(str) << endl;//printing the reverse_string method value

   return 0;

}

Output:

6

110

Explanation:

In this code two string method "integer_to_reverse_binary and reverse_string" is defined that holds one parameter "integer_value and input_string".

In the first method a string variable is defined, that use the while loop to check integer value is greater than 0 and add zeros in the value and return its value as a string.

In the second it reverse the string value and store into the result variable, and in the main method the "num and str" variable is defined, and in the num it takes integer value and pass into the above method and print its return value.    

You might be interested in
Adam is so good at playing arcade games that he will win at every game he plays. One fine day as he was walking on the street, h
klio [65]

Answer:

what the answer chaoices

Explanation:

brainlest me please

3 0
3 years ago
A project manager types a document and prints it. He is using _____. hardware software hardware and software
garri49 [273]
Both hardware and software
4 0
3 years ago
Read 2 more answers
What is the name of the file manager in Microsoft Windows? The file manager used in Microsoft Windows is
miss Akunina [59]

It is called File Explorer

5 0
4 years ago
Read 2 more answers
A school’s administration stores the following data for each student in an online system: name, class, and five electives. Selec
aleksandr82 [10.1K]

Answer:

Array of Strings

Explanation:

I believe the best data type for this information would be an Array of Strings. This array would have a fixed size of 5 elements. One for each one of the five electives that the student will have. Then the individual electives will be String elements that are saved in the 5 indexes of the Array. This would allow all of the electives to be bundled into a single variable and accessed together or individually by the user. This would be the best and most efficient data type to store this information.

5 0
3 years ago
What is computer forensics? Where and how would an IT auditor use thisresource?
riadik2000 [5.3K]

Answer:

Learning how to acquire

Explanation:

4 0
2 years ago
Other questions:
  • The ? Tool removes blemishes and imperfections by sampling pixels around the spot and then paints withh matching texture, transp
    10·1 answer
  • You want to boot a linux system into singer user mode/ what options might you add to a linux kernel options list in a boot loade
    9·1 answer
  • Tom installs a Telnet application on his Windows Vista system. When he first starts the program, Windows Firewall pops up with a
    10·1 answer
  • The internet shopping cart is an example of which version of the web?
    11·1 answer
  • Marie uses DHTML to create a website for her college. How is DHTML more helpful than HTML?
    13·2 answers
  • Select the correct answer.
    7·2 answers
  • A deluxe meal, represented by a DeluxeMeal object, includes a side dish and a drink for an additional cost of $3. The DeluxeMeal
    14·1 answer
  • They have requested a 1- to 2-page memo that describes the hospital's protections against a ransomware attack.
    10·1 answer
  • Which results are expected in a personality test but not a skills assessment?
    13·1 answer
  • Green Fields Landscaping Company sells evergreen trees which are priced by height. Customers have a choice of purchasing a tree
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!