The program below (in C not in C++) reads a double before calling the chop function, which has
the following prototype:
void chop(double d, long *whole_part, double *fraction_part);
This function chops the double into two parts, the whole part and the fraction.
So “365.25” would be chopped into “365” and “.25”. Implement the function.
#include <stdio.h>
#include <math.h>
#include <limits.h>
void chop(double d, long *whole_part, double *fraction_part);
int main(void)
{
double d = 0.0;
long whole = 0;
double fraction = 0.0;
printf("enter a double ");
scanf("%lf", &d);
chop(d, &whole, &fraction);
printf("%lf chopped is %ld and %.5lg\n",
d, whole, fraction);
return 0;
}
void chop(double d, long *whole_part, double *fraction_part)
{
Insert the function here
}
1
Expert's answer
2012-11-12T08:16:33-0500
int main(void) { & double d = 1.5; & double i = 0; & double f = modf(d, &i); & printf("Number = %g\n", d); & printf("Integer part = %g, fractional part = %g\n", i, f); & return 0; }
Comments
Leave a comment