Sample of how the finished code should look like. Use C++ Code in C++ Part A (De
ID: 3853142 • Letter: S
Question
Sample of how the finished code should look like.
Use C++
Code in C++ Part A (Decimal to Binar An algorithm for converting a decimal value into its binary equivalent involves repeated (integer) division by 2 (down to a result of 0), collection of the remainders, and reading those remainders 'backwards.' (see sample above) You are to code this algorithm usinn Reausuncion calls itself (usually sending the You are to code this algorithm using Recursion Recursion is a looping technique wherein a function calls itself (usually sending the recursive call a new set of parameter values). The recursive process (here the division by 2 step of each iteration) may proceed any number of times before a control condition is reached that stops the final routine from making another recursive call; this initiatesa series of 'step out actions wherein the recursively called routines step backwards to the beginning of the process (this method will be explained in a posted lecture) uil d the program's recursive algorithm, you must make each call Note that when you b by sending only the number to be divided by 2 for the current iteration (i.e., only one parameter in the recursive function no additional 'control' values). Part B (Binary to Decimal A 'standard' algorithm for a binary to decimal conversion evaluates the positional values of the binary digits. For example, the binary value of: 101 is 5 because you have the following: 2"22^12"O Binary powers of 2: Positional value: Binary digits So the value ^(1*4) (0*2)(1*1)-5 Likewise, the binary value of 11111101-253 because you have the following: 2n7 128 214 16 2^2 4 2"3 2^1 2"O 2"6 6432 2^5 Powers of 2: Positional values: Binary digits So the value- 128 64 +3216+ 8+4+1-253 Code this part of the program using the above algorithm. Make sure input values are validated as having only ones and zeroes, and use separate function calls for data validation and conversionExplanation / Answer
A) decimal to binary
#include<iostream>
#include<conio.h>
#include<math. h>
using namespace std;
long int decimalToBinary(int ) ;
static long int binary=0;
static int i=0;
long int decimalToBinary(int num) {
if(num! =0)
{
binary=binary+pow(10,i)*(num%2);
i++;
decimalToBinary(num/2);
}
return(binary);
}
int main()
{int num;
long int binary_ans=0;
cout<<"please enter your decimal number to be converted(0 to quit) ";
cin>>num;
binary_ans=decimalToBinary(num) ;
cout<<" the value is="<<binary_ans;
_getch() ;
return(0);
}
B)binary to decimal
int binaryToDecimal(string binary, unsigned int i=0)
{
int ans=0;
if(i<binary.length())
{
If(binary[i]=='1')
ans = pow(2,i);
else if (binary[i]! ='0')
throw "string is not formatted in binary" ;
return ans+binaryToDecimal(binary, ++i) ;
} return ans;
}
Converting decimalto binary vice versa programs so by giving user input decimal format it converts to binary vice versa
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.