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

COMMENT THE CODE and explain the logic. #include #include #include using namespa

ID: 3802338 • Letter: C

Question

COMMENT THE CODE and explain the logic.

#include
#include
#include
using namespace std;
const int width_32 = 8;// the IEEE 32 BIT WIDTH
const int width_64 = 11;// THE IEEE 64 BIT WIDTH
// FUNCTION PROTOTYPES
void collection(float &number);
void menu(int &choice);
void ieee754(int choice, float number, string &sign, int whole_part, float &fraction_part, string fraction_part_binary, string whole_part_binary,
   string ieee, int biased_expo_32, string mantissa, string exponent_binary, int biased_expo_64);
void split(float number, string & sign, int & whole_part, float &fraction_part);
void whole_conversion(int whole_part, string & whole_part_binary, int width_32);
void whole_conversion2(int whole_part, string & whole_part_binary, int width_64);
void fraction_conversion(float fraction_part, string & fraction_part_binary, int iterations);
void fraction_conversion2(float fraction_part, string & fraction_part_binary, int iterations);
void exponent_conversion(int biased_expo_32, string &exponent, int width_32);
void exponent_conversion2(int biased_expo_64, string &exponent_binary, int width_64);
void convertohex(string ieee);
void normalize(int &biasedexpo, string ieee, string & mantissa, string whole_part_binary, string fraction_part_binary);
void normalize2(int &biasedexpo, string ieee, string & mantissa, string whole_part_binary, string fraction_part_binary);

int main()
{
   bool found = false;
   string   mantissa = "",
       whole_part_binary = "",
       sign,
       fraction_part_binary,
       exponent_binary,
       ieee;
   float   number,
       fraction_part;
   int       whole_part=0,
       biased_expo_32 = 127,
       biased_expo_64 = 1023,
       choice = 0;
      

   collection(number);
   menu(choice);
   ieee754(choice, number, sign, whole_part, fraction_part, fraction_part_binary, whole_part_binary, ieee, biased_expo_32, mantissa, exponent_binary, biased_expo_64);


   return 0;
}
void collection(float &number)
{

   cout << "Please enter a number to convert to IEEE 754 :";
   cin >> number;

}
void menu(int &choice)
{
   cout << "1. IEEE 754 floating point Single Precision" << endl;
   cout << "2. IEEE 754 floating point Double precision" << endl;
   cin >> choice;


}
void ieee754(int choice,float number,string &sign,int whole_part,float &fraction_part,string fraction_part_binary, string whole_part_binary,
   string ieee, int biased_expo_32, string mantissa, string exponent_binary, int biased_expo_64)
{
   if (choice == 1)
   {
       split(number, sign, whole_part, fraction_part);
       fraction_conversion(fraction_part, fraction_part_binary, 32);

       whole_conversion(whole_part, whole_part_binary, width_32);


       normalize(biased_expo_32, ieee, mantissa, whole_part_binary, fraction_part_binary);

       exponent_conversion(biased_expo_32, exponent_binary, width_32);


       ieee = sign + exponent_binary + mantissa;
       ieee = ieee.substr(0, 32);
       cout << "Sign: " << sign << endl;
       cout << "Exponent: " << exponent_binary << endl;
       cout << "Mantissa: " << mantissa << endl;
       cout << "......................................................" << endl;
       cout << "IEEE 754: " << endl;
       cout << ieee << endl;
       convertohex(ieee);
      
   }
   else if (choice == 2)
   {
       split(number, sign, whole_part, fraction_part);

       fraction_conversion2(fraction_part, fraction_part_binary, 64);

       whole_conversion2(whole_part, whole_part_binary, width_64);
      
      
       normalize2(biased_expo_64, ieee, mantissa,whole_part_binary, fraction_part_binary);
       exponent_conversion2(biased_expo_64, exponent_binary, width_64);
       ieee = sign + exponent_binary + mantissa;
       ieee = ieee.substr(0, 64);
       cout << "Sign: " << sign << endl;
       cout << "Exponent: " << exponent_binary << endl;
       cout << "Mantissa: " << mantissa << endl;
       cout << "......................................................" << endl;
       cout << "IEEE 754: " << endl;
       cout << ieee << endl;
       convertohex(ieee);

   }
   else
       cout << "Invalid Choice" << endl;
}

void split(float number, string & sign, int & whole_part, float &fraction_part)
{



   if (number >= 0)
       sign = "0";
   else sign = "1";

   number = abs(number);
   whole_part = int(number);
   fraction_part = number - whole_part;
}

void whole_conversion(int whole_part, string & whole_part_binary, const int x)
{
  
   bitset whole_binary(whole_part);
   whole_part_binary = whole_binary.to_string();
}
void whole_conversion2(int whole_part, string & whole_part_binary, const int x)
{

   bitset whole_binary(whole_part);
   whole_part_binary = whole_binary.to_string();
}
void fraction_conversion(float fraction_part, string & fraction_part_binary, int iterations)
{

   for (int i = 0; i < iterations; i++)
   {
       fraction_part *= 2;
       if (fraction_part >= 1)
       {
           fraction_part_binary = fraction_part_binary + "1";
           fraction_part -= 1;
       }
       else
           fraction_part_binary = fraction_part_binary + "0";
   }
}
void fraction_conversion2(float fraction_part, string & fraction_part_binary, int iterations)
{

   for (int i = 0; i < iterations; i++)
   {
       fraction_part *= 2;
       if (fraction_part >= 1)
       {
           fraction_part_binary = fraction_part_binary + "1";
           fraction_part -= 1;
       }
       else
           fraction_part_binary = fraction_part_binary + "0";
   }
}

void convertohex(string ieee)
{
   int length = ieee.length();
   int start = 0, end = 4;
   string str = "";
   for (int i = 0; i < length + 1; i += 4)
   {
       str = ieee.substr(start, end);
       start += 4;

       if (str == "0000") cout << "0 ";
       if (str == "0001")
           cout << "1 ";
       if (str == "0010") cout << "2 ";
       if (str == "0011") cout << "3 ";
       if (str == "0100") cout << "4 ";
       if (str == "0101") cout << "5 ";
       if (str == "0110") cout << "6 ";
       if (str == "0111") cout << "7 ";
       if (str == "1000") cout << "8 ";
       if (str == "1001") cout << "9 ";
       if (str == "1010") cout << "A ";
       if (str == "1011") cout << "B ";
       if (str == "1100") cout << "C ";
       if (str == "1101") cout << "D ";
       if (str == "1110") cout << "E ";
       if (str == "1111") cout << "F ";
       str = "";
   }
}

void normalize(int &biasedexpo, string ieee, string & mantissa,string whole_part_binary, string fraction_part_binary)
{
   ieee = whole_part_binary + fraction_part_binary;
   int length = ieee.length();
   char z;
   int counter = 0;
   for (int i = 0; i < length; i++)
   {
       z = ieee.at(i);
       if (i >7)
           counter++;
       if (z == '1')
       {
           if (i < 7)
           {
               biasedexpo = biasedexpo + i + 1;
               mantissa = ieee.substr(i + 1, length) + mantissa;
           }
           else
           {
               biasedexpo = biasedexpo - counter;
               mantissa = ieee.substr(i + 1, length) + mantissa;
           }
           break;
       }
   }
}
void normalize2(int &biasedexpo, string ieee, string & mantissa, string whole_part_binary, string fraction_part_binary)
{
   ieee = whole_part_binary + fraction_part_binary;
   int length = ieee.length();
   char z;
   int counter = 0;
   for (int i = 0; i < length; i++)
   {
       z = ieee.at(i);
       if (i >7)
           counter++;
       if (z == '1')
       {
           if (i < 7)
           {
               biasedexpo = biasedexpo + i + 1;
               mantissa = ieee.substr(i + 1, length) + mantissa;
           }
           else
           {
               biasedexpo = biasedexpo - counter;
               mantissa = ieee.substr(i + 1, length) + mantissa;
           }
           break;
       }
   }
}
void exponent_conversion(int biased_expo_32, string &exponent_binary, const int x)
{
   bitset expo(biased_expo_32);
   exponent_binary = expo.to_string();
}
void exponent_conversion2(int biased_expo_64, string &exponent_binary, const int x)
{
   bitset expo(biased_expo_64);
   exponent_binary = expo.to_string();
}

Explanation / Answer

#include
#include
#include

using namespace std;
const int width_32 = 8; // the IEEE 32 BIT WIDTH
const int width_64 = 11; // THE IEEE 64 BIT WIDTH

// FUNCTION PROTOTYPES
void collection(float & number);
void menu(int & choice);
void ieee754(int choice, float number, string & sign, int whole_part, float & fraction_part, string fraction_part_binary, string whole_part_binary,
string ieee, int biased_expo_32, string mantissa, string exponent_binary, int biased_expo_64);
void split(float number, string & sign, int & whole_part, float & fraction_part);
void whole_conversion(int whole_part, string & whole_part_binary, int width_32);
void whole_conversion2(int whole_part, string & whole_part_binary, int width_64);
void fraction_conversion(float fraction_part, string & fraction_part_binary, int iterations);
void fraction_conversion2(float fraction_part, string & fraction_part_binary, int iterations);
void exponent_conversion(int biased_expo_32, string & exponent, int width_32);
void exponent_conversion2(int biased_expo_64, string & exponent_binary, int width_64);
void convertohex(string ieee);
void normalize(int & biasedexpo, string ieee, string & mantissa, string whole_part_binary, string fraction_part_binary);
void normalize2(int & biasedexpo, string ieee, string & mantissa, string whole_part_binary, string fraction_part_binary);

int main() {
bool found = false;
string mantissa = "",
whole_part_binary = "",
sign,
fraction_part_binary,
exponent_binary,
ieee;
  
float number,
fraction_part;
  
int whole_part = 0,
biased_expo_32 = 127, // biased exponent for a 32 bit number is 7 bits - 1
biased_expo_64 = 1023, // biased exponent for a 64 bit number is 10 bits - 1
choice = 0;

collection(number);
menu(choice);
  
// convert the user entered number to user opted format
ieee754(choice, number, sign, whole_part, fraction_part, fraction_part_binary, whole_part_binary, ieee, biased_expo_32, mantissa, exponent_binary, biased_expo_64);

return 0;
}

// function which ask user to enter the number
void collection(float & number) {

cout << "Please enter a number to convert to IEEE 754 :";
cin >> number;

}

// function to ask user to select the choice, whether single precision or double
void menu(int & choice) {
cout << "1. IEEE 754 floating point Single Precision" << endl;
cout << "2. IEEE 754 floating point Double precision" << endl;
cin >> choice;

}
void ieee754(int choice, float number, string & sign, int whole_part, float & fraction_part, string fraction_part_binary, string whole_part_binary,
string ieee, int biased_expo_32, string mantissa, string exponent_binary, int biased_expo_64) {

// if single precision is required
if (choice == 1) {
   // get the integral, fractional and sign bit from the original number
split(number, sign, whole_part, fraction_part);
  
   // convert fractional part to binary
fraction_conversion(fraction_part, fraction_part_binary, 32);

   // convert integral part to binary
whole_conversion(whole_part, whole_part_binary, width_32);

   // to fill the mantissa and biased exponent based on whole part and fractional part
normalize(biased_expo_32, ieee, mantissa, whole_part_binary, fraction_part_binary);

   // fill the exponent string
exponent_conversion(biased_expo_32, exponent_binary, width_32);

   // concatenate integral, fractional and exponent string to form the output
ieee = sign + exponent_binary + mantissa;
ieee = ieee.substr(0, 32);
cout << "Sign: " << sign << endl;
cout << "Exponent: " << exponent_binary << endl;
cout << "Mantissa: " << mantissa << endl;
cout << "......................................................" << endl;
cout << "IEEE 754: " << endl;
cout << ieee << endl;
convertohex(ieee);

} else if (choice == 2) {
   // if double precision is required
  
   // get the integral, fractional and sign bit from the original number
split(number, sign, whole_part, fraction_part);

   // convert fractional part to binary
fraction_conversion2(fraction_part, fraction_part_binary, 64);

   // convert integral part to binary
whole_conversion2(whole_part, whole_part_binary, width_64);

   // to fill the mantissa and biased exponent based on whole part and fractional part for double precision
normalize2(biased_expo_64, ieee, mantissa, whole_part_binary, fraction_part_binary);
  
   // fill the exponent string
exponent_conversion2(biased_expo_64, exponent_binary, width_64);
  
   // concatenate integral, fractional and exponent string to form the output
ieee = sign + exponent_binary + mantissa;
ieee = ieee.substr(0, 64);
cout << "Sign: " << sign << endl;
cout << "Exponent: " << exponent_binary << endl;
cout << "Mantissa: " << mantissa << endl;
cout << "......................................................" << endl;
cout << "IEEE 754: " << endl;
cout << ieee << endl;
convertohex(ieee);

} else
cout << "Invalid Choice" << endl;
}

// function to split a number to sign bit, integer part and fractional part
void split(float number, string & sign, int & whole_part, float & fraction_part) {
  
// set sign string
if (number >= 0)
sign = "0";
else sign = "1";

// abs() gets the absolute value
number = abs(number);
  
// only integral part
whole_part = int(number);
  
// set the fraction part
fraction_part = number - whole_part;
}

// function to convert the integral part for single precision into binary
void whole_conversion(int whole_part, string & whole_part_binary,
const int x) {
  
// create bitset class object
bitset whole_binary(whole_part);
whole_part_binary = whole_binary.to_string();
}

// function to convert the integral part for double precision into binary
void whole_conversion2(int whole_part, string & whole_part_binary,
const int x) {

// create bitset class object
bitset whole_binary(whole_part);
whole_part_binary = whole_binary.to_string();
}

// function to convert the fractional part for single precision into binary
// it will set the fraction_part_binary string
void fraction_conversion(float fraction_part, string & fraction_part_binary, int iterations) {

for (int i = 0; i < iterations; i++) {
   // in each iteration, last value is double, and then added
fraction_part *= 2;
if (fraction_part >= 1) {
fraction_part_binary = fraction_part_binary + "1";
fraction_part -= 1;
} else
fraction_part_binary = fraction_part_binary + "0";
}
}

// function to convert the fractional part for double precision into binary
// it will set the fraction_part_binary string
void fraction_conversion2(float fraction_part, string & fraction_part_binary, int iterations) {

for (int i = 0; i < iterations; i++) {
   // in each iteration, last value is double, and then added
fraction_part *= 2;
if (fraction_part >= 1) {
fraction_part_binary = fraction_part_binary + "1";
fraction_part -= 1;
} else
fraction_part_binary = fraction_part_binary + "0";
}
}

// function which converts a binary string into hex string and prints
void convertohex(string ieee) {
int length = ieee.length();
int start = 0, end = 4;
string str = "";
  
// processing the binary string in chunk of 4characters at a time
for (int i = 0; i < length + 1; i += 4) {
  
   // get this chunk
str = ieee.substr(start, end);
start += 4;

   // compare, which hex character matches the binary
if (str == "0000") cout << "0 ";
if (str == "0001")
cout << "1 ";
if (str == "0010") cout << "2 ";
if (str == "0011") cout << "3 ";
if (str == "0100") cout << "4 ";
if (str == "0101") cout << "5 ";
if (str == "0110") cout << "6 ";
if (str == "0111") cout << "7 ";
if (str == "1000") cout << "8 ";
if (str == "1001") cout << "9 ";
if (str == "1010") cout << "A ";
if (str == "1011") cout << "B ";
if (str == "1100") cout << "C ";
if (str == "1101") cout << "D ";
if (str == "1110") cout << "E ";
if (str == "1111") cout << "F ";
str = "";
}
}

// fucntion fills the mantissa and biased exponent, based on whole part, and fractional part for single precison number
void normalize(int & biasedexpo, string ieee, string & mantissa, string whole_part_binary, string fraction_part_binary) {
ieee = whole_part_binary + fraction_part_binary;
int length = ieee.length();
char z;
int counter = 0;
for (int i = 0; i < length; i++) {
z = ieee.at(i);
if (i > 7)
counter++;
if (z == '1') {
if (i < 7) {
biasedexpo = biasedexpo + i + 1;
mantissa = ieee.substr(i + 1, length) + mantissa;
} else {
biasedexpo = biasedexpo - counter;
mantissa = ieee.substr(i + 1, length) + mantissa;
}
break;
}
}
}

// fucntion fills the mantissa and biased exponent, based on whole part, and fractional part for double precison number
void normalize2(int & biasedexpo, string ieee, string & mantissa, string whole_part_binary, string fraction_part_binary) {
ieee = whole_part_binary + fraction_part_binary;
int length = ieee.length();
char z;
int counter = 0;
for (int i = 0; i < length; i++) {
z = ieee.at(i);
if (i > 7)
counter++;
if (z == '1') {
if (i < 7) {
biasedexpo = biasedexpo + i + 1;
mantissa = ieee.substr(i + 1, length) + mantissa;
} else {
biasedexpo = biasedexpo - counter;
mantissa = ieee.substr(i + 1, length) + mantissa;
}
break;
}
}
}

// function to fil the exponent_binary part of the number, based on the biased exponent for single precison number
void exponent_conversion(int biased_expo_32, string & exponent_binary,
const int x) {
bitset expo(biased_expo_32);
exponent_binary = expo.to_string();
}

// function to fil the exponent_binary part of the number, based on the biased exponent for double precison number
void exponent_conversion2(int biased_expo_64, string & exponent_binary,
const int x) {
bitset expo(biased_expo_64);
exponent_binary = expo.to_string();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote