Question: #include #include #include /** * Display binary of user entered items. * User can choose to enter decimal integer, character, or floating point numbers. *

#include  #include  #include  /** * Display binary of user entered items. * User can choose to enter decimal integer, character, or floating point numbers. * -----------------------You job-----------------------------------: * 1. Complete the program to handle case 'f', and 'd' in order to accept float type and double type input. * 2. Answer the following question (Add your answer to the end of each question): * 1). What is the binary of float type number 1.0? - * How many bits should a float type number actually consume? - * 2). What is the binary of double type number 1.35? - * How many bits should a double type number actually consume? - * 3). The following questions are based on IEEE 754 Floating Point Standard: * How many bits are used for sign in double type? - * How many bits are used for exponent in double type? - * How many bits are used for mantissa in double type? - * 4). Perform the following input in specified order, and then answer the questions below: * Enter d for choise * Enter 13.9 for input number * Enter f for choice * Enter 13.9 for input number * Questions: * What should be the actual binary for double type 13.9?- * What should be the actual binary for float type 13.9? - * Are these two binarys the same or not? Why? - * 5). What is the physical size of the unionNumber? * 6). Why is the binary in output always 64-bit instead of the length of your input type? - * 6). If you entered input for a type shorter than 64-bit, would the input overwrite the memory for the whole unionNumber? */ void clearBuffer(); void printBinary(char * p, int numOfBytes); void printArray(int numberOfElements); void getBinary(char * p, int numOfBits); //declare myUnionType which is a custom type template union myUnionType{ int i; char c; unsigned u; float f; //32-bit single precision floating point double d; //64-bit double precision floating point }; static int arr[8]; int main(void) { //declare a unionNumber, of type union myUnionType union myUnionType unionNumber; char userChoice = ' '; int numOfBytes = sizeof(unionNumber); char *p;//char type pointer, used to print binary p = (char *) & unionNumber; //Repeatition loop while(1 == 1){ //get user choice puts("Choose input type: i for int, c for char, u for unsigned int, f for float, d for double."); while(1 == 1){ // a loop to get userChoice. Only proceed when user provide good choice scanf("%c", &userChoice); if( userChoice == 'i' || userChoice == 'c' || userChoice == 'u' || userChoice == 'f' || userChoice == 'd' || userChoice == 'q'){ printf("%c - Good choice! ", userChoice); break; //only break when a valid input is received } else{ printf("%c - not a good choice, getting next one ... ", userChoice); } } if(userChoice == 'q'){ puts("Bye!"); //terminate the loop / program break; } clearBuffer(); //clean everything, for new input //get user input based on userChoice switch (userChoice) { case 'i': puts("Please enter a decimal integer:"); scanf("%d", &unionNumber.i); clearBuffer(); printf("You entered decimal integer %d ", unionNumber.i); break; case 'c': puts("Please enter a character:"); scanf("%c", &unionNumber.c);//try to test with , enter key, etc. clearBuffer(); printf("You entered character '%c' ", unionNumber.c); break; case 'u': puts("Please enter an unsigned integer:"); scanf("%u", &unionNumber.u); //test -1 clearBuffer(); printf("You entered unsigned integer %u ", unionNumber.u); break; /**************************************************************** * Complete the program to handle case 'f', and 'd' in order to * accept float type and double type input. * **************************************************************/ default: break; } //Display binary printf("Binary: "); printBinary(p, numOfBytes); } return EXIT_SUCCESS; } void clearBuffer(){ char c; while((c = getchar()) != ' '); } /** printBinary byte by byte * The byte order is determined by the endianness of the system *My system is a little-endian system which stores the least-significant byte at the smallest address. * so I have to output the bytes from the greatest address to the smallest * in order to view the binary correnctly*/ void printBinary(char * p, int numOfBytes){ int i = 0; char * currentPointer = p + numOfBytes - 1; for(; i < numOfBytes; i ++){ getBinary(currentPointer, 8); printArray(8); currentPointer --; } printf(" "); } /*getBinary - get the binary representation of number into the array arr. Arguments: arr - the integer array to hold the binary representation number - the integer to get binary from numOfBits - the size of the arr and the number of bits for the binary Algorithm requirement: make use of bitwise shifting and masking to get the bit values for the number */ void getBinary(char * p, int numOfBits){ int i = numOfBits - 1; int num = *p; //take 1 byte as integer for(; i >= 0; i --){ arr[i] = num & 01; //Apply mask 01 to keep only the lowest 1 bit (i.e., the right-most bit), and store the result to the array num = num >> 1; //Right-shift 1 bit } } /* printArray: print numberOfElements in the argument integer array */ void printArray(int numberOfElements){ int i = 0; for(; i < numberOfElements; i ++){ printf("%d", arr[i]); if((i + 1) % 32 == 0 ) printf(" "); } printf(" "); }

IN C

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!