Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PROGRAM IN C NOT C++ MUST USE THE FOLLOWING CODE SEGMENT. unsigned int number =

ID: 3817508 • Letter: P

Question

PROGRAM IN C NOT C++ MUST USE THE FOLLOWING CODE SEGMENT.

unsigned int number = *((unsigned int*)&n);

I am writing a program that converts a floating point number to scientific format. like to 1.11E1  

I need to use bitwise opperators to do this program so the floating points must be cast to

unsigned int number = *((unsigned int*)&n);

can someone help me with this?

Thanks!

Explanation / Answer

char * float2s(float f) { return float2s(f, 2); } char * float2s(float f, unsigned int digits) { int index = 0; static char s[16]; // buffer to build string representation // handle sign if (f < 0.0) { s[index++] = '-'; f = -f; } // handle infinite values if (isinf(f)) { strcpy(&s[index], "INF"); return s; } // handle Not a Number if (isnan(f)) { strcpy(&s[index], "NaN"); return s; } // max digits if (digits > 6) digits = 6; long multiplier = pow(10, digits); // fix int => long int exponent = int(log10(f)); float g = f / pow(10, exponent); if ((g < 1.0) && (g != 0.0)) { g *= 10; exponent--; } long whole = long(g); // single digit long part = long((g-whole)*multiplier); // # digits char format[16]; sprintf(format, "%%ld.%%0%dld E%%+d", digits); sprintf(&s[index], format, whole, part, exponent); return s; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote