SlideShare a Scribd company logo
C++ Assignment Help
For any help regarding C++Assignment Help
Visit :- https://www.cpphomeworkhelp.com/ ,
Email :- info@cpphomeworkhelp.com or
Call us at :- +1 678 648 4277
Problem 1: Floating Point (floating)
In this problem, we will investigate how floating-point numbers are represented in memory. Recall that
a float is a 32-bit value with a single sign bit, eight exponent bits, and 23 mantissa bits. Specifically, a
floating point number x with sign bit ‘sign’, exponent e , and mantissa bits m0, m1, . . . , m22 can be
written1
x = ( 1)sign
(1.m22m21m20 ...m0) 2e bias
where the mantissa is, of course, in base two. You will be given a list of N floating point values x1,
x2, . . . , xN . For each xi , your program should write its binary representation to the output file as
indicated below.
Suggested approach: You’ll need to use bitwise operations, but you cannot do so on a floating-point
number directly. Instead, you will need a way of considering a variable as either a float or an unsigned
int. We will use a union, which is valid in this case because we assume the size of the two data types is
the same.
union float bits {
float f;
unsigned int bits;
};
II print hex( 5.Of ) outputs "The float looks like Ox4OaOOOOO in hex." void print
hex( float f) {
union float bits t;
t.f = f;
printf( "The float looks like Ox%x in hex.n", t.bits );
}
Output Format
Lines 1...N : Line i contains a representation of the floating-point number xi ,
formatted as shown in the sample output.
Sample Output (file floating.out)
1.1OOOOOOOOOOOOOOOOOOOOOO * 2�O
1.O1OOOOOOOOOOOOOOOOOOOOO * 2�-3
-1.11O1O1O1O1OO1111111OOOO * 2�2
Input Format
Line 1: One integer N
Lines 2...N + 1: Line i + 1 contains floating point number xi
Sample Input (file floating.in)
3
1.5
O.15625
-7.333
Except for the case where x is a denormal floating point number, as discussed in
class, in which case the (unbiased) exponent is -126 and mantissa is written 0.m22m21
m20 ...m0.
cpphomeworkhelp.com
/*
PROG: floating
LANG: C
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#define ABSOLUTE_WIDTH 31
#define MANTISSA_WIDTH 23
#define EXPONENT_WIDTH 8
#define EXPONENT_MASK 0xffu
#define MANTISSA_MASK 0x007fffffu
#define EXPONENT_BIAS 127
union float_bits {
float f;
uint32_t bits;
};
void print_float( FILE *output, float f ) {
union float_bits t; t.f = f;
uint32_t sign_bit = ( t.bits >> ABSOLUTE_WIDTH );
cpphomeworkhelp.com
if( exponent != 0 || mantissa != 0 ) {
fprintf( output, " * 2^%dn", (int) ( exponent - EXPONENT_BIAS ) );
uint32_t exponent = ( t.bits >> MANTISSA_WIDTH ) & EXPONENT_MASK;
uint32_t mantissa = ( t.bits & MANTISSA_MASK );
if( sign_bit != 0 ) {
fprintf( output, "-" );
}
if( exponent > 2 * EXPONENT_BIAS ) {
fprintf( output, "Infn" ); /* Infinity */
return;
} else if( exponent == 0 ) {
fprintf( output, "0." ); /* Zero or Denormal */
exponent = ( mantissa != 0 ) ? exponent + 1 : exponent;
} else {
fprintf( output, "1." ); /* Usual */
}
for( int k = MANTISSA_WIDTH - 1; k >= 0; --k ) {
fprintf( output, "%d", ( mantissa >> k ) & 1 );
}
}
}
cpphomeworkhelp.com
int main() {
FILE *input = fopen( "floating.in", "r" ),
*output = fopen( "floating.out", "w" );
size_t N; float f;
fscanf( input, "%zu", &N );
for( size_t i = 0; i < N; ++i ) {
fscanf( input, "%f", &f );
print_float( output, f );
}
fclose( input );
fclose( output );
return 0;
}
Below is the output using the test data:
floating: 1: OK [0.004 seconds] OK!
2: OK [0.004 seconds] OK!
3: OK [0.004 seconds] OK!
4: OK [0.004 seconds] OK!
5: OK [0.005 seconds] OK!
6: OK [0.004 seconds] OK!
7: OK [0.004 seconds] OK!
cpphomeworkhelp.com

More Related Content

Similar to CPP Homework help

C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
Saifur Rahman
 
Python Conditionals and Functions
Python Conditionals and FunctionsPython Conditionals and Functions
Python Conditionals and Functions
Pooja B S
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
Eelco Visser
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
jovannyflex
 
Matlab project
Matlab projectMatlab project
Matlab project
iftikhar ali
 
MODULE. .pptx
MODULE.                              .pptxMODULE.                              .pptx
MODULE. .pptx
Alpha337901
 
Interesting facts on c
Interesting facts on cInteresting facts on c
Interesting facts on c
Durgadevi palani
 
Verilogforlab
VerilogforlabVerilogforlab
Verilogforlab
Shankar Bhukya
 
Mcq cpup
Mcq cpupMcq cpup
Mcq cpup
tahir_ali786
 
Floating point basicsThe core idea of floating-point representatio.pdf
Floating point basicsThe core idea of floating-point representatio.pdfFloating point basicsThe core idea of floating-point representatio.pdf
Floating point basicsThe core idea of floating-point representatio.pdf
info235816
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
josies1
 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans incnayakq
 
Functions Practice Sheet.docx
Functions Practice Sheet.docxFunctions Practice Sheet.docx
Functions Practice Sheet.docx
SwatiMishra364461
 
Catastrophic Cancellation
Catastrophic CancellationCatastrophic Cancellation
Catastrophic Cancellation
C4Media
 
Python for Scientific Computing
Python for Scientific ComputingPython for Scientific Computing
Python for Scientific Computing
Albert DeFusco
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
happycocoman
 
Encoder, decoder, multiplexers and demultiplexers
Encoder, decoder, multiplexers and demultiplexersEncoder, decoder, multiplexers and demultiplexers
Encoder, decoder, multiplexers and demultiplexers
pubgalarab
 

Similar to CPP Homework help (20)

C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
Python Conditionals and Functions
Python Conditionals and FunctionsPython Conditionals and Functions
Python Conditionals and Functions
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
 
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptxLecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
 
Matlab project
Matlab projectMatlab project
Matlab project
 
MODULE. .pptx
MODULE.                              .pptxMODULE.                              .pptx
MODULE. .pptx
 
Interesting facts on c
Interesting facts on cInteresting facts on c
Interesting facts on c
 
Verilogforlab
VerilogforlabVerilogforlab
Verilogforlab
 
Mcq cpup
Mcq cpupMcq cpup
Mcq cpup
 
Floating point basicsThe core idea of floating-point representatio.pdf
Floating point basicsThe core idea of floating-point representatio.pdfFloating point basicsThe core idea of floating-point representatio.pdf
Floating point basicsThe core idea of floating-point representatio.pdf
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans inc
 
Functions Practice Sheet.docx
Functions Practice Sheet.docxFunctions Practice Sheet.docx
Functions Practice Sheet.docx
 
Catastrophic Cancellation
Catastrophic CancellationCatastrophic Cancellation
Catastrophic Cancellation
 
Python for Scientific Computing
Python for Scientific ComputingPython for Scientific Computing
Python for Scientific Computing
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Encoder, decoder, multiplexers and demultiplexers
Encoder, decoder, multiplexers and demultiplexersEncoder, decoder, multiplexers and demultiplexers
Encoder, decoder, multiplexers and demultiplexers
 
functions
functionsfunctions
functions
 

More from C++ Homework Help

cpp promo ppt.pptx
cpp promo ppt.pptxcpp promo ppt.pptx
cpp promo ppt.pptx
C++ Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
C++ Homework Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
C++ Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
C++ Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
C++ Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
C++ Homework Help
 
Get Fast C++ Homework Help
Get Fast C++ Homework HelpGet Fast C++ Homework Help
Get Fast C++ Homework Help
C++ Homework Help
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
C++ Homework Help
 
CPP Assignment Help
CPP Assignment HelpCPP Assignment Help
CPP Assignment Help
C++ Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
C++ Homework Help
 

More from C++ Homework Help (19)

cpp promo ppt.pptx
cpp promo ppt.pptxcpp promo ppt.pptx
cpp promo ppt.pptx
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Get Fast C++ Homework Help
Get Fast C++ Homework HelpGet Fast C++ Homework Help
Get Fast C++ Homework Help
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Assignment Help
CPP Assignment HelpCPP Assignment Help
CPP Assignment Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
 

Recently uploaded

Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 

Recently uploaded (20)

Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 

CPP Homework help

  • 1. C++ Assignment Help For any help regarding C++Assignment Help Visit :- https://www.cpphomeworkhelp.com/ , Email :- info@cpphomeworkhelp.com or Call us at :- +1 678 648 4277
  • 2. Problem 1: Floating Point (floating) In this problem, we will investigate how floating-point numbers are represented in memory. Recall that a float is a 32-bit value with a single sign bit, eight exponent bits, and 23 mantissa bits. Specifically, a floating point number x with sign bit ‘sign’, exponent e , and mantissa bits m0, m1, . . . , m22 can be written1 x = ( 1)sign (1.m22m21m20 ...m0) 2e bias where the mantissa is, of course, in base two. You will be given a list of N floating point values x1, x2, . . . , xN . For each xi , your program should write its binary representation to the output file as indicated below. Suggested approach: You’ll need to use bitwise operations, but you cannot do so on a floating-point number directly. Instead, you will need a way of considering a variable as either a float or an unsigned int. We will use a union, which is valid in this case because we assume the size of the two data types is the same. union float bits { float f; unsigned int bits; }; II print hex( 5.Of ) outputs "The float looks like Ox4OaOOOOO in hex." void print hex( float f) { union float bits t; t.f = f; printf( "The float looks like Ox%x in hex.n", t.bits ); }
  • 3. Output Format Lines 1...N : Line i contains a representation of the floating-point number xi , formatted as shown in the sample output. Sample Output (file floating.out) 1.1OOOOOOOOOOOOOOOOOOOOOO * 2�O 1.O1OOOOOOOOOOOOOOOOOOOOO * 2�-3 -1.11O1O1O1O1OO1111111OOOO * 2�2 Input Format Line 1: One integer N Lines 2...N + 1: Line i + 1 contains floating point number xi Sample Input (file floating.in) 3 1.5 O.15625 -7.333 Except for the case where x is a denormal floating point number, as discussed in class, in which case the (unbiased) exponent is -126 and mantissa is written 0.m22m21 m20 ...m0. cpphomeworkhelp.com
  • 4. /* PROG: floating LANG: C */ #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <math.h> #define ABSOLUTE_WIDTH 31 #define MANTISSA_WIDTH 23 #define EXPONENT_WIDTH 8 #define EXPONENT_MASK 0xffu #define MANTISSA_MASK 0x007fffffu #define EXPONENT_BIAS 127 union float_bits { float f; uint32_t bits; }; void print_float( FILE *output, float f ) { union float_bits t; t.f = f; uint32_t sign_bit = ( t.bits >> ABSOLUTE_WIDTH ); cpphomeworkhelp.com
  • 5. if( exponent != 0 || mantissa != 0 ) { fprintf( output, " * 2^%dn", (int) ( exponent - EXPONENT_BIAS ) ); uint32_t exponent = ( t.bits >> MANTISSA_WIDTH ) & EXPONENT_MASK; uint32_t mantissa = ( t.bits & MANTISSA_MASK ); if( sign_bit != 0 ) { fprintf( output, "-" ); } if( exponent > 2 * EXPONENT_BIAS ) { fprintf( output, "Infn" ); /* Infinity */ return; } else if( exponent == 0 ) { fprintf( output, "0." ); /* Zero or Denormal */ exponent = ( mantissa != 0 ) ? exponent + 1 : exponent; } else { fprintf( output, "1." ); /* Usual */ } for( int k = MANTISSA_WIDTH - 1; k >= 0; --k ) { fprintf( output, "%d", ( mantissa >> k ) & 1 ); } } } cpphomeworkhelp.com
  • 6. int main() { FILE *input = fopen( "floating.in", "r" ), *output = fopen( "floating.out", "w" ); size_t N; float f; fscanf( input, "%zu", &N ); for( size_t i = 0; i < N; ++i ) { fscanf( input, "%f", &f ); print_float( output, f ); } fclose( input ); fclose( output ); return 0; } Below is the output using the test data: floating: 1: OK [0.004 seconds] OK! 2: OK [0.004 seconds] OK! 3: OK [0.004 seconds] OK! 4: OK [0.004 seconds] OK! 5: OK [0.005 seconds] OK! 6: OK [0.004 seconds] OK! 7: OK [0.004 seconds] OK! cpphomeworkhelp.com