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

I need help getting my display correct. I have three functions the first the for

ID: 3542361 • Letter: I

Question

I need help getting my display correct.  I have three functions the first the format is good but the answer is wrong.  The second is correct.  The third I am not sure about I tried to use the same code I was using but it didn't work.  Here are the instructions and my code.

Thanks,

Create a program that has the following three functions:

a function called printChar that takes a character (char) argument. The function should print the binary representation of the character argument.
a function called printShort that takes a short integer (short) argument. The function should print the binary representation of the short argument.
a function called printFloat that takes a single-precision floating-point (float) argument. The function should print the binary representation of the float argument.
The textbook includes an algorithm for printing the binary representation of a value in Programming Problem Section 2.2 Problem 1 (page 77 in my book). In each function you will need a variable for a mask that is the same data type as the value, except that it should be unsigned. For a data value of n bits:

    initialize the mask variable to 2n - 1 (a 1 followed by all 0's in binary)
    for count running from n - 1 down to 0 do:
        if the bitwise-and (&) of the value and mask is non-zero
            display '1'
        else
            display '0'
        shift the bits in the mask to the right one position (use >>)

To make your output easier to read, print a space after every 4 bits.

The bitset class template in C++ contains functions that will automatically extract the bits from an integer. Do not use the bitset class template. I want you to write your own code following an algorithm similar to the one described in the textbook.  


Notes/Warnings

The bitwise-and, bitwise-or and bitwise-exclusive or operators have lower precedence than the comparison operators, so:

    value & mask != 0     compares mask to 0, then performs the bitwise-or

When these operators are used with other operators, you will normally need parentheses:

    (value & mask) != 0


The bitwise operators can only be applied to integral data types (char, short, int, long, and variations of these types). You cannot use them with the float data type. To print the binary representation of a float value, you will need a way to access the float value as if it were an integer. A C++ union will let you access the same memory location as a float or int (assuming they are the same size).

For example, the following code will print the same memory interpreted as a float and as an integer.


#include <iostream>
using namespace std;

union FloatValue
{
   int intView;
   float floatView;
};

int main( )
{
   FloatValue num;
   
   cout << "Enter a float value: ";
   cin >> num.floatView;
   cout << "You entered " << num.floatView << endl;
   cout << "If the bit pattern used to represent this value is interpreted"
        << " as an integer, the value is "
        << num.intView << endl;   
    
   return 0;
}

Write a small main program (driver) to test your functions. It should be menu-driven. That is, it should look something like:

    print menu
    read user selection
    while user did not select quit option
        if user selected character option
            read a character value
            display the character and its bit pattern
        if user selected short int option
            read a short int value
            display the short int and its bit pattern
        if user selected float option
            read a float value
            display the float and its bit pattern
        print menu
        read user selection
    end while

/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// My Code starts here

///////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

#include <iostream>
#include <bitset>
#include <math.h>
//#include
using namespace std;

// Function prototypes
void displayMessage();
void mainProcess();
bool done( );
void print_Character();
void print_Shortinteger();
void print_Float();

// Function: main

int main()
{

   displayMessage();
   mainProcess();      
   bool done( );

system ("pause");
return 0;
}

//    Function    : Display menu options

void displayMessage()
{
   
    cout << " Please choose from the following options. ";
    cout << endl;
    cout << endl;
    cout << " Menu Options: ";
    cout << " 1 - Print the binary representation of a character. ";
    cout << " 2 - Print the binary representation of a short integer. ";
    cout << " 3 - Print the binary representation of a float. ";
    cout << " 4 - Exit program. ";
    cout << endl;
}

//    Function    : mainProcess

void mainProcess()
{  
         int choice;
        
         bool done = false;

         while(! done)
         {
         cout << " Enter your choice:  ";
         cin >> choice;
                  
          // Process users choice
    
            switch(choice)
            {
             case 1:
                  print_Character();
                  break;

             case 2:
                  print_Shortinteger();
                  break;
    
             case 3:
                  print_Float();
                  break;

             case 4:
                  cout << " Program Ended. ";
                  done = true;
                  break;

             default:
                  cout << "Invalid option" <<endl;
                  cout << endl;
                  cout << " The menu options are 1-4, please selected a valid option. ";
            }    
         }              
}

//************************************************************************
void print_Character()
{
char inputCharacter;

cout << " Enter a character: ";
cin >> inputCharacter;
cout << " " ;
cout << " The binary representation for " << inputCharacter << " is: ";

       char bin;

             int i;

             for(i = 7; i >= 0; i --)
             {
               bin = (inputCharacter %2) + '0';
               inputCharacter /= 2;
               cout << bin;
               if(i % 4 == 0)
               {
                    putchar(' ');
               }
            }
     
      cout << endl;
     
    return;
}

// Function: A void function to print an int short's binary representation

void print_Shortinteger()
{
int number;

     cout << " Enter an integer: ";
     cin >> number;
     cout <<endl;
     cout << " The binary representation for " << number << " is: ";
     
     
          for (int i=15; i>=0; i--)
           {
            int bit = ( (number >> i) & 1);
            cout << bit;
            
            if(i % 4 == 0)
            {
             putchar(' ');
            }
           }
cout << endl;
cout << endl;
    return;
}

// Function: A void function to print a float's binary representation

void print_Float()
{
float number;

cout << " Enter a float: ";
cin >> number;
cout << " ";

union
{
float input; // assumes sizeof(float) == sizeof(int)
int output;

}


data;
data.input = number;
std::bitset<sizeof(float) * CHAR_BIT> binary(data.output);
std::cout << " The binary representation for " << number << " is: " << binary << std::endl;     

cout << endl;

return;
}

Explanation / Answer

please rate - thanks


it appears your int and float work

(used http://babbage.cs.qc.cuny.edu/IEEE-754.old/Decimal.html to check the float)


just your char doesn't work --I've corrected it

any questions-ask


/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// My Code starts here

///////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

#include <iostream>
#include <bitset>
#include <math.h>
#include <string>
//#include
using namespace std;

// Function prototypes
void displayMessage();
void mainProcess();
bool done( );
void print_Character();
void print_Shortinteger();
void print_Float();

// Function: main

int main()
{

displayMessage();
mainProcess();
bool done( );

system ("pause");
return 0;
}

// Function : Display menu options

void displayMessage()
{

cout << " Please choose from the following options. ";
cout << endl;
cout << endl;
cout << " Menu Options: ";
cout << " 1 - Print the binary representation of a character. ";
cout << " 2 - Print the binary representation of a short integer. ";
cout << " 3 - Print the binary representation of a float. ";
cout << " 4 - Exit program. ";
cout << endl;
}

// Function : mainProcess

void mainProcess()
{
int choice;

bool done = false;

while(! done)
{
cout << " Enter your choice: ";
cin >> choice;

// Process users choice

switch(choice)
{
case 1:
print_Character();
break;

case 2:
print_Shortinteger();
break;

case 3:
print_Float();
break;

case 4:
cout << " Program Ended. ";
done = true;
break;

default:
cout << "Invalid option" <<endl;
cout << endl;
cout << " The menu options are 1-4, please selected a valid option. ";
}
}
}

//************************************************************************
void print_Character()
{
char inputCharacter;

cout << " Enter a character: ";
cin >> inputCharacter;
cout << " " ;
cout << " The binary representation for " << inputCharacter << " is: ";
unsigned int mask=0x80,b;
int i;
for(i=0;i<8;i++)
{if(i%4==0)
printf(" ");
b=inputCharacter&mask;
if(b==0)
cout<<"0";
else
cout<<"1";
mask=mask/2;
}
cout<<endl;
}
// Function: A void function to print an int short's binary representation

void print_Shortinteger()
{
int number;

cout << " Enter an integer: ";
cin >> number;
cout <<endl;
cout << " The binary representation for " << number << " is: ";


for (int i=15; i>=0; i--)
{
int bit = ( (number >> i) & 1);
cout << bit;

if(i % 4 == 0)
{
putchar(' ');
}
}
cout << endl;
cout << endl;
return;
}

// Function: A void function to print a float's binary representation

void print_Float()
{
float number;

cout << " Enter a float: ";
cin >> number;
cout << " ";

union
{
float input; // assumes sizeof(float) == sizeof(int)
int output;

}


data;
data.input = number;
std::bitset<sizeof(float) * CHAR_BIT> binary(data.output);
std::cout << " The binary representation for " << number << " is: " << binary << std::endl;

cout << endl;

return;
}

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