SlideShare a Scribd company logo
1 of 7
For any Assignment related queries, Call us at : - +1 (315) 557-6473
mail us at : - support@codingassignmenthelp.com, or
reach us at : - https://www.codingassignmenthelp.com/
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.
https://www.codingassignmenthelp.com/
C Programming Assignment and Solution
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 );
}
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
https://www.codingassignmenthelp.com/
1Except 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.m22m21m20 . . .
m0.
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
https://www.codingassignmenthelp.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 );
uint32_t exponent = ( t.bits » MANTISSA_WIDTH ) & EXPONENT_MASK;
https://www.codingassignmenthelp.com/
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 );
}
if( exponent != 0 || mantissa != 0 ) {
fprintf( output, " * 2^%dn", (int) ( exponent - EXPONENT_BIAS ) );
}
}
int main() {
FILE *input = fopen( “floating.in”, “r” ),
https://www.codingassignmenthelp.com/
*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!
https://www.codingassignmenthelp.com/

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Effective Programming In C

  • 1. For any Assignment related queries, Call us at : - +1 (315) 557-6473 mail us at : - support@codingassignmenthelp.com, or reach us at : - https://www.codingassignmenthelp.com/
  • 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. https://www.codingassignmenthelp.com/ C Programming Assignment and Solution
  • 3. 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 ); } 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 https://www.codingassignmenthelp.com/
  • 4. 1Except 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.m22m21m20 . . . m0. 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 https://www.codingassignmenthelp.com/
  • 5. /* 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 ); uint32_t exponent = ( t.bits » MANTISSA_WIDTH ) & EXPONENT_MASK; https://www.codingassignmenthelp.com/
  • 6. 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 ); } if( exponent != 0 || mantissa != 0 ) { fprintf( output, " * 2^%dn", (int) ( exponent - EXPONENT_BIAS ) ); } } int main() { FILE *input = fopen( “floating.in”, “r” ), https://www.codingassignmenthelp.com/
  • 7. *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! https://www.codingassignmenthelp.com/