Answer to Quiz 9(b) Thu 25 Oct 2001
1. Consider the following program :
#include<stdio.h>
p c
int main(void) +-----+ +-----+
{ | &c -|---> | 'A' |
char c = 'A'; +-----+ +-----+
char *p = &c;
after *p = 'B' :
*p = 'B';
putchar(c); p c
return 0; +-----+ +-----+
} | &c -|---> | 'B' |
+-----+ +-----+
Draw a diagram at the right of the program to illustrate
the relations between p and c. Indicate the
values of those variables before and after executing *p = 'B'.
What does the program print? 'B'
2. A float has a fraction and an exponent, e.g. 1.234000e+04
has exponent 4 and fraction 1.234000. For a nonzero float x,
we can compute the exponent by casting the decimal logarithm of the
absolute value of x to an int. The fraction is then the result of
the division of x by 10^exponent.
Consider the prototype of the function frexp :
void frexp ( float x, float *fraction, int *exponent );
/* computes fraction and exponent of a nonzero float x */
Use log10, abs, and pow from math.h in your definition for this function :
void frexp ( float x, float *fraction, int *exponent )
{
*exponent = (int) log10(abs(x));
*fraction = x/pow(10,*exponent);
}