Question
ANSWER IN C++

Short Answer Given the following class declaration for a binary string/integer pair (the binary number, in the form of a stri

2. (10 pts) Write a (recursive) function of the convertitos method. This method should convert an integer to its correspondin

Constructors
media%2F05a%2F05ad7264-3ede-48a1-8f77-65
Short Answer Given the following class declaration for a binary string/integer pair (the binary number, in the form of a string. should be the binary form of the integer): class binint public: int num; // Note this is an address! string *bin; // Note that this is an address! binint (int n); binint (string s); void convertitos(int n); void convertstoi(string s); II NOTE: you do not have to do anything // with this method you can assume it exists and successfully // converts a string to an integer and sets the num field to point // to that integer -binint():
2. (10 pts) Write a (recursive) function of the convertitos method. This method should convert an integer to its corresponding binary string. To convert an integer to a binary string, you append the number mod 2 to the beginning of the string, and then loop on the number divided by 2. So, for instance, 5's corresponding string would be: s-1, n/2-2 s-01, п/2-1 S-101, n/2 0 so we're done 9% corresponding string would be: S=01, n/2 = 2 S 1001, n/2 0 so we're done NOTE: you can assume you have a to string method that converts an integer to a string
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <bits/stdc++.h>
#include<iostream>
std::string binary(int);


std::string binary(int num) // recursive funtion
{
  
if(num<=0) // base condition to recursive function
return "";
  
int i=num%2;
  
std::string str2 = std::to_string(i);
  
return binary(num/2)+str2; // recursive call
  
  
  
}

// Driver code
int main()
{
  
int num;
// std::cout<<" Enter the number to find binary string : ";
  
std:: cin>>num; // number input

  
  

std::cout <<"The binary of "<<num <<" is :"<< binary(num) << '\n';
return 0;
}

D sardar Vallabhbha Nel :1) Che pave Chi uge ㄨ e ANSWER IN C++ Cor × Execute MATL a octa X GG Converting string to r × a6 std

Add a comment
Know the answer?
Add Answer to:
Short Answer Given the following class declaration for a binary string/integer pair (the binary n...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Implement an algorithm to convert binary number into an integer. Binary number is represented as...

    1. Implement an algorithm to convert binary number into an integer. Binary number is represented as a string. Ex. int n = to_int("01010"); int to_int(const std::string& b) { } 2. Implement an algorithm to convert a decimal number to binary. The return type is string which holds the binary number as string. std::string to_binary(int n) { } 3. Implement a function to check if the number is positive or negative. The function should return true if number is positive. bool...

  • Implement a Java method named addBinary() that takes two String arguments (each representing a binary value)...

    Implement a Java method named addBinary() that takes two String arguments (each representing a binary value) and returns a new String corresponding to the result of performing binary addition on those arguments. Before you begin, if one of the arguments is shorter than the other, call your pad() method from the previous step to extend it to the desired length. Note: ped() method is public static String pad(String input, int size) { if(input.length()>=size) { return input; } String a =...

  • Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray();...

    Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray(); char[] reverse = new char[original.length]; for(int i =0; i<s.length(); i++){ reverse[i] = original[original.length-1-i]; } return new String(reverse); } /**  * Takes in a string containing a first, middle and last name in that order.  * For example Amith Mamidi Reddy to A. M. Reddy.  * If there are not three words in the string then the method will return null  * @param name in...

  • Need help with following assignment written in Java. For this assignment, you’re going to create...

    Need help with following assignment written in Java. For this assignment, you’re going to create a new class named “Fraction” for managing rational numbers (fractions). (You should not submit a main program with the Fraction class; however, you'll definitely want to write one for your own testing purposes.) Details are as follows: CLASS DEFINITION CONSTRUCTORS: Fraction(int num,int denom) num/denom creates a new number whose value is Fraction(int num) METHODS (assume x is a Fraction): creates a new number whose value...

  • Given the following code for a binary search, how many times this method have to be...

    Given the following code for a binary search, how many times this method have to be executed in order to find the number 5? A) 1 time B) 2 times C) 3 times D) 4 times E) more than 5 times public class BinarySearch{ public static void main(String []args){ if (right >= left) { int middle = left + (right - left) / 2; if (arr[middle] == num) return middle; if (arr[mid] > num) return binarySearch(arr, left, middle - 1,...

  • Please explain if the following code is actually correct. If the following code correct, please explain...

    Please explain if the following code is actually correct. If the following code correct, please explain why the code works and is also correct. Don’t use * Java’s Integer .toBinaryString(int) in this program./* According to the textbook and the instructor, I am not supposed to use arrays such as int binary[] = new int[25]; I guess this is the reason why this problem is starting to look kind of hard.   Chapter 5 Exercise 37: Java Programming * * (Decimal to...

  • What is the output of following program: public class Test{ public static void main(String[] args) {...

    What is the output of following program: public class Test{ public static void main(String[] args) { A a = new A(): a method B(): } } class A{ public A(){ System out println("A's constructor is executed"): } public void method(){ System out printin ("methodA is executed"): } public void methodAB(){ System out printin ("As methodAB is executed"): } } class B extends A { private int num = 0: public B (){ super(): System out printin ("B's constructor is executed"):...

  • answer in java Given a Binary Search Tree containing integer values, write a method to print...

    answer in java Given a Binary Search Tree containing integer values, write a method to print the values in descending order. public class Treenode { int val; Treenode left; Treenode right; Treenode (int x) { val = x; } The method signature is: public void reverseSorted(Treenode root){ // your code

  • Using the program segment below, write two short functions to complete the program. Use the test...

    Using the program segment below, write two short functions to complete the program. Use the test cases to ensure the program works properly. Prototypes: void int2bin(int, int[8]); The first function will convert an integer that is between 0 - 255 to its binary representation. You are to store the binary number in the array passed to the function. void printBinary(int[8]); The second function will print the binary number stored in the array passed to the function. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Test 1: 13...

  • e fellowing class declaration to answer the questionist below class WeatherStation public emp, double wind, string...

    e fellowing class declaration to answer the questionist below class WeatherStation public emp, double wind, string wind dir, double prediplk void void void int Winda int getTempo double getWindSpeed) : string getWind Direction): double getPrecipitation: void setTemp(int t void setWindData(double w, string dir): void setPrecip(double pl printTempO private int temperature double windSpeed, precipitation string windDir method 14) 14) Refer to the class declaration above. Which of the following methods is an accessor A) int WindChillo: C) WeatherStation: B) void setPrecipO...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT