Question

Create a design document for a program that will take a number from the user and...

Create a design document for a program that will take a number from the user and convert it to IEEE single precision format. The program displays the IEEE representation (Single precision) of the number entered by the user.

PLEASE ADD THE PSEUDOCODE AND CODE WITH C PROGRAMMING

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pseudocode:

All sections have been commented appropriately in regards to the explanation.

The entire process takes advantage of how the union data type works. Since the values stored in each of the members is the same as the first member of the union, the first member to be declared is the float number that we give as the input.

As for the Union data type, the ordering of the declaration is also important due to how the program assigns the values from the float to the unsigned integers. Since the size of the struct ieee is 32 bits, float is also taken as 32 bits. The values are then converted and stored by the program starting from LSB to MSB and distributed across the 3 sub members of res, namely significand, exponent and sign.

Code:

#include<stdio.h>
void printBinary(int n, int i)
{
int k;
int cur_digit;
for (k = i - 1; k >= 0; k--) {
cur_digit=n>>k;
if (cur_digit & 1)
printf("1");
else
printf("0");
}
/*NOTE: WE USE BITWISE OPERATORS HERE AS SINCE BITWISE OPERATORS OPERATE ON BIT VALUES ONLY IT SAVES US THE TROUBLE
OF TRYING TO MANUALLY CONVERT, THEY GET CONVERTED AUTOMATICALLY*/
/*PRINTBINARY HERE RIGHT SHIFTS AND THEN PERFORMS BITWISE AND OPERATION STARTING FROM THE MSB
HERE SMALL i REPRESENTS THE NUMBER OF BITS UNTIL WHICH WE WANT TO PRINT THE BINARY NUMBER TO
>> IS THE A BINARY RIGHT SHIFT OPERATOR WHICH SHIFTS THE FIRST OPERAND TO THE RIGHT BY NUMBER OF
DIGITS SPECIFIED BY THE SECOND OPERAND.SINCE i IS THE SIZE OF THE NUMBER ITSELF, SHIFTING BY i-1
GIVES US THE MSB OF THE NUMBER WHOSE BINARY REPRESENTATION WE ARE TRYING TO GET. THE OPERATOR &
IS ALSO A BITWISE OPERATOR, HERE THE cur_digit STORES THE BITWISE VALUES STARTING FROM MSB TO LSB.
PERFORMING & OPERATION WITH IT GIVES US "1" WHEN THE BIT VALUE WAS 1 ELSE "0", THUS PRINTING THE BIT VALUES OF
THE INPUT NUMBER ONE BY ONE STARTING FROM MSB TO LSB*/
   printf("|");
}
/*MEMBERS OF UNION SHARE MEMORY AND STORE SAME VALUES, AND THE SIZE OF THE UNION IS THE SAME AS THE SIZE OF THE LARGEST MEMBERS
HERE THE LARGEST MEMBER IS THE STRUCT IEEE THAT IS A TOTAL OF 32 BITS; AS FOR TYPEDEF, THE SYNTAX IS
TYPEDEF <ACTUAL DATA TYPE> <DERIVED DATA TYPE NAME (GIVEN BY USER)>, HERE num IS THE NAME OF THE UNION DATA TYPE, ORDERING IS IMPORTANT AND IS STORED
FROM LSB TO MSB*/
typedef union{
   float real;
   struct ieee{
       unsigned int significand : 23;//we use bit fields when we know maximum bits that will be used, significand has 23 bits
       unsigned int exponent : 8;/*exponent has 8 bits, and the exponent stored here is the biased exponent.
       Bias value is defined as (2^n)-1 for n bits, so it is +127 of true exponent value*/
       unsigned int sign : 1;//sign only requires 1 bit
       /*here the values that actually get sored in significand, exponent, and sign are already the values that correspond to the ieee format due
       them sharing the same memory, now as C will only output the numbers in decimal, that is base 10, all we need to do is output the binary
       equivalent stored at these places*/
   }res;
}num;
void getIEEE(num in)
{
printf("|%d|",in.res.sign);//as sign bit is only a single digit we can directly print it out
printBinary(in.res.exponent,8);
printBinary(in.res.significand,23);
//we need to convert exponent and significand to their binary equivalents
printf("\n");
}
int main()
{
num in;//stores the input and the result
   printf("Enter real number :");
   scanf("%f",&in.real);
   printf("Decimal equivalents of the number stored in memory are :\n");
   printf("Significand :%u\n",in.res.significand);
printf("Exponent :%u\n",in.res.exponent);
printf("Sign :%u\n",in.res.sign);
printf("IEEE754 form of %f is :\n",in.real);
getIEEE(in);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Create a design document for a program that will take a number from the user and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design a program that asks the user to enter a series of 20 numbers. The program...

    Design a program that asks the user to enter a series of 20 numbers. The program should store the numbers in the array and then display the following data: The lowest number in the array The highest number in the array The total of the number in the array The average of the numbers in the array PLEASE GIVE THE PSEUDOCODE AND CODE IN C PROGRAMMING

  • Design a program that lets the user enter the total rainfall for each of 12 months...

    Design a program that lets the user enter the total rainfall for each of 12 months into an array. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amount. PLEASE MODULARIZED THE CODE   PLEASE USE C PROGRAMMING AND ADD PSEUDOCODE

  • Design pseudocode for a program that accepts a single number from the user then outputs one...

    Design pseudocode for a program that accepts a single number from the user then outputs one of the following message as appropriate: The number is larger than 0, The number is smaller than 0, The number is 0. HINTS AND NOTES: Be sure to prompt the user for all input.

  • a. Design the logic for a program that allows a user to enter 12 numbers, then...

    a. Design the logic for a program that allows a user to enter 12 numbers, then displays them in the reverse order of entry. b. Modify the reverse-display program so that the user can enter any amount of numbers up to 12 until a sentinel value is entered. ( I really could use more of a pseudocode rather than actual code. also an explanation of each step. The answer I got last time was just c++ code that including a...

  • Please write a program to find the area code, exchange code, and the user number from...

    Please write a program to find the area code, exchange code, and the user number from any phone number (i.e., 10 digits) entered by the user. [hints: assume the common phone number format (3 digits area code)-(3 digits exchange code)-(4 digits user number)] in C language.

  • Can you send the code and the screenshot of it running? 1) Create a PYHW2 document...

    Can you send the code and the screenshot of it running? 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both flowchart and pseudocode forms for the following two functions: A. Write a Python function that receives a real number argument representing the sales amount for videos rented so far this month The function asks the user for the number...

  • USING RAPTOR For the following Programming Challenges, use the modular approach and pseudocode to design a suitable program to solve it. Create a program that allows the user to input a list of first...

    USING RAPTOR For the following Programming Challenges, use the modular approach and pseudocode to design a suitable program to solve it. Create a program that allows the user to input a list of first names into one array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. The output should be a list of email addresses where the address is of the following from: first.last@mycollege.edu

  • Total Sales Design a program that asks the user to enter a store’s sales for each...

    Total Sales Design a program that asks the user to enter a store’s sales for each day of the week. The amounts should be stored in an array. Use a loop to calculate the total sales for the week and display the result. Need: variable lists, IPO Chart, pseudocode, Flowchart, and working Python code. 2. Lottery Number Generator Design a program that generates a 7-digit lottery number. The program should have an Integer array with 7 elements. Write a loop...

  • PLEASE DO IN PYTHON Program 2: Design (pseudocode) and implement (source code) a program (name it...

    PLEASE DO IN PYTHON Program 2: Design (pseudocode) and implement (source code) a program (name it FeetMeters) to display a conversion tables for feet and meter as show below. Document your code and properly. Feet Meter 1.0 0.305 2.0 0.610 3.0 0.915 . . . . . . 19.0 5.7.95 20.0 6.100 Meter Feet 1.0 3.279 2.0 6.558 3.0 9.837 . . . . . . 19.0 62.301 20.0 65.574 The program defines the following methods: Method feetToMeter() converts from...

  • Design a program using Python and using from flask Import flask that generates a lottery number...

    Design a program using Python and using from flask Import flask that generates a lottery number but in a website.. The program should have an Integer array with 9 elements. Write a loop that steps through the array, randomly generating a number in the range of 0 through 42 for each element. (Use the random function) Then write another loop that displays the contents of the array. Each number should be displayed as a list, the numbers should be generated...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT