Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

Write two short C programs and solve four exam-style problems.

Details on the programs are as follows.

Program : builddbl.c

This program takes four inputs representing the sign, exponent, and integer and fractional parts of the significand of a double. The significand M will look like d.xxxxxxxxxxxxx where each x is a hexadecimal digit. The digit d left of the "decimal point" is the integer part of the M. It is 1 for normalized doubles and 0 for denormalized doubles. It will not appear directly in the double you build, but it will affect the exponent. The thirteen x's represent the fractional part of the double. Your program should use the four parts to construct a double and print it both as a double (using %.17g format) and its 16-digit hexadecimal representation. Your program need not deal with infinity or NaN (not a number), but it should deal properly with both normalized and denormalized doubles. Here are some sample outputs to show the behavior of this program:

As with assignment #1 I want you to write both programs with main functions that take care of command line inputs and printing the output. The main funcitons will call auxiliary functions to do the work of assembling or disassembling the double's. These auxiliary functions will use a C structure to hold the parts of adouble. The definition of the structure and the declarations of the auxiliary functions are given in hw2.h. You should include it in your programs with the following statement: #include "hw2.h". Note the change in punctuation. Library headers are written , while headers from your own directory are written"hw2.h".

Also included in hw2.h are declarations for two functions d2ll and ll2d given below. These functions convert double to long long int and back again without changing the bit pattern. You will use them to convert your double's to long long int's before taking them apart using integer arithmetic. You will build yourdouble's using integer arithmetic to make a long long int with the desired bit pattern. Then you convert it to a double as a last step. If the inputs are invalid and you cannot build the double, your auxiliary function should return NaN (Not a Number) which I have defined in hw2.h.

When your auxiliary functions compile successfully and run correctly on a few test inputs you should submit them to the grading server just as you did with functions h2d and d2h in assignment #1.

Instructions are given below.

> builddbl
Usage: builddbl sign, E, intpart, fracpart (parts of a double)
> builddbl + 1023 1 fffffffffffff
value = 1.7976931348623157e+308 (7fefffffffffffff)
> builddbl - 0 1 0000000010000
value = -1.0000000000145519 (bff0000000010000)
> builddbl + -2 1 e62433b79890d
value = 0.47474747474747475 (3fde62433b79890d)
> builddbl - -1022 0 0000000314159
value = -1.594840446316023e-317 (8000000000314159)
> builddbl - -1022 1 0000000314159
value = -2.220738601020418e-308 (8010000000314159)
> builddbl + 10 2 fffff00000ddd
Usage: sign(+ or -), E(-1022 to +1023), d(0 or 1), f(13 hex digits)
Build failed
> builddbl + -1023 1 0000000000000
Usage: sign(+ or -), E(-1022 to +1023), d(0 or 1), f(13 hex digits)
Build failed
> builddbl + -1021 0 7777777777777
Invalid parts for normalized double
Build failed
> builddbl + 1024 1 0000020000000
Usage: sign(+ or -), E(-1022 to +1023), d(0 or 1), f(13 hex digits)
Build failed

Your code should always print an error message if a command line input is not of the proper form or a value is out of the proper range. If f is given with more than 13 hex digits, you may just use the last 13 digits.

Program #2 : dblparts.c

This program is roughly the inverse of Program #1. It takes a list of doubles and prints out the parts: sign, exponent, and integer and fractional parts of the significand. Here are some sample outputs:

>dblparts
Usage: dblparts f1 f2 f3 ... (list of floating point numbers)

>dblparts .47474747474747475 -1.594840446316023e-317 -1.7976931348623157e308
input 1 = .47474747474747475
sign = +, E = -2 intpart = 1 fracpart = e62433b79890d

input 2 = -1.594840446316023e-317
sign = -, E = -1022 intpart = 0 fracpart = 0000000314159

input 3 = -1.7976931348623157e308
sign = -, E = 1023 intpart = 1 fracpart = fffffffffffff

Try to make your output look as close to these sample outputs as possible.

Here are two functions that you might find useful. The function ll2d turns a long long int into a double with the same bit pattern. The function d2ll turns a double into an long long int with the same bit pattern. This is a different result than you get with a cast: ll2d(x) != (double)x . Try to figure out how they work, but that is not part of this homework:

double ll2d(unsigned long long int l)

/* convert 64-bit integer to double with same bit pattern */

{
union {
double dd;
unsigned long long int ll;
} a;
a.ll = l;
return a.dd;
}

unsigned long long int d2ll(double d)

/* convert double to 64-bit integer with same bit pattern */

{
union {
double dd;
unsigned long long int ll;
} a;
a.dd = d;
return a.ll;
}

The problems that go with this assignment are the Piazza Resources page: Problems_2.rtf. Fill in the answers and put it in your drop box.

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M9716571
  • Price:- $35

Priced at Now at $35, Verified Solution

Have any Question?


Related Questions in C/C++

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As