answer.
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]
2 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]2 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
2. Because technology is always changing, there are new applications being developed constantly. (1 point)
Zepler [3.9K]
True because we need new tech
4 0
2 years ago
Read 2 more answers
Identify the articulation site that allows us to nod our head ""yes"".
Luden [163]

Answer:

Occipital bone-atlas

Explanation:

The occipital bone is a bone that covers the back of your head; an area called the occiput. The occipital bone is the only bone in your head that connects with your cervical spine (neck).  The occipital bone surrounds a large opening known as the foramen magnum.

The foramen magnum allows key nerves and vascular structures passage between the brain and spine. Namely, it is what the spinal cord passes through to enter the skull. The brainstem also passes through this opening.

The foramen magnum also allows 2 key blood vessels traversing through the cervical spine, called the vertebral arteries, to enter the inner skull and supply blood to the brain

8 0
2 years ago
Return 1 if ptr points to an element within the specified intArray, 0 otherwise.
Paladinen [302]

Answer:

int withinArray(int * intArray, int size, int * ptr) {

      if(ptr == NULL) // if ptr == NULL return 0

            return 0;

      // if end of intArr is reached

      if(size == 0)

          return 0; // element not found

  else  

          {

            if(*(intArray+size-1) == *(ptr)) // check if (size-1)th element is equal to ptr

                   return 1; // return 1

            return withinArray(intArray, size-1,ptr); // recursively call the function with size-1 elements

         }

}

Explanation:

Above is the completion of the program.

4 0
2 years ago
Tyler wants to upgrade his very old operating system to the latest mac or microsoft operating system, but he cannot seem to inst
nordsb [41]
Every program has minimum system requirement, so at least His CPU is not good, or the amount of his RAM is too short to run the new version of OS.
6 0
2 years ago
An encryption system works by shifting the binary value for a letter one place to the left. "A" then becomes: 1 1 0 0 0 0 1 0 Th
Vika [28.1K]

Answer:

The hexadecimal equivalent of the encrypted A is C2

Explanation:

Given

Encrypted binary digit of A = 11000010

Required

Hexadecimal equivalent of the encrypted binary digit.

We start by grouping 11000010 in 4 bits

This is as follows;

1100 0010

The we write down the hexadecimal equivalent of each groupings

1100 is equivalent to 12 in hexadecimal

So, 1100 = 12 = C

0010 is represented by 2 in hexadecimal

So, 0010 = 2

Writing this result together; this gives

1100 0010 = C2

Going through the conversion process;

A is first converted to binary digits by shifting a point to the left

A => 11000010

11000010 is then converted to hexadecimal

11000010 = C2

Conclusively, the hexadecimal equivalent of the encrypted A is C2

8 0
2 years ago
Other questions:
  • Problem statement: Using loop, write a program that will ask the user to enter a character for left or right. Then, the user wil
    13·1 answer
  • An analyst receives an alert from the SIEM showing an IP address that does not belong to the assigned network can be seen sendin
    9·1 answer
  • When you park on a hill, think about which way _____.
    6·2 answers
  • "online privacy alliance (opa) is an organization of companies dedicated to protecting online privacy. members of opa agree to c
    15·1 answer
  • What text results in variable white space (blank) texts results In even white space
    9·1 answer
  • Modify the TimeSpan class from Chapter 8 to include a compareTo method that compares time spans by their length. A time span tha
    10·1 answer
  • To reduce costs and the environmental impact of commuting, your company decides to close a number of offices and to provide supp
    14·1 answer
  • Write a public static method named printArray, that takes two arguments. The first argument is an Array of int and the second ar
    15·1 answer
  • Accenture has partnered with a global construction company that specializes in building skyscrapers. The company wants better ma
    12·1 answer
  • Which characteristic of Cloud computing allows data centers to better manage hard drive failures and allocate computing resource
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!