Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C++ CODING. Please identifty each task, thank you. In this assignment, you ar

ID: 3877958 • Letter: I

Question

IN C++ CODING.

Please identifty each task, thank you.

In this assignment, you are going to write a program for the following tasks. You must submit only a single cpp file. » Task 1: write a function that takes input a positive integer int n, and returns a string s The function should store the binary representation of n starting from the most significant bit. (Most significant bit means the left most bit.) For example, if the input is 26, then the function returns the string s = “11010". Here you do not need to worry about the problem of overflow » Task 2: write a function that converts a binary number into a decimal number. In particular the function takes input a string s that represents the binary digits from the most significant bit to the least significant bit. Your function returns an integer that is equal to the number s represents. For example, if your string is 11010, your return should be 26. You can assume that there is no overflow Note: if you don't know the "string" type in c++, google it » Task 3: In your main function, you should write some test cases to test your Tasks 1 and 2 The format is flexible. In the class, we have discussed about the algorithms. Here is a quick recap. Given a decimal number n, we can represent n into the following way where k is roughly log2(n) and all ai's are binary digits, i.e., either 0 or 1. The binary representation of n should be a^ak-1... ao. If you are given a binary string apak-i... ao (represented by an array), then you can compute n from the above formula directly The slightly more non-trivial part is to to compute the binary digits from n. Here we can first determine the ao by using mod 2. (Think about how and why). Then we know: So we can use the same idea to find out a1. Proceeding in this way, we can find out all the ai's For more information about binary numbers, you can find more details from Wikipedia (http //en.wikipedia.org/wiki/Binary_number)

Explanation / Answer

#include <iostream>

#include <cmath>

#include <string.h>

using namespace std;

string decimalToBinary(int);

int binaryToDecimal(string);

int main() {

std::cout << "decimalToBinary(16):" << decimalToBinary(16) << std::endl;

std::cout << "decimalToBinary(12):" << decimalToBinary(12) << std::endl;

std::cout << "binaryToDecimal('1100'):" << binaryToDecimal("1100") << std::endl;

std::cout << "binaryToDecimal('10000'):" << binaryToDecimal("10000") << std::endl;

return 0;

}

// function that converts decimal number to binary

string decimalToBinary(int n){

string result="";

while(n>0){

result=std::to_string(n%2)+result;

n/=2;

}

return result;

}

// function that converts binary number to decimal

int binaryToDecimal(string n){

int length = n.length();

int i=0;

int result=0;

for(i;i<length;i++){

if(n[i]=='1'){//if bit is 1, then only add it;s weight

result+=pow(2,length-i-1);

}

}

return result;

}

Output: