Answer to Question #10867 in C++ for Jo

Question #10867
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(&quot;Number = %g\n&quot;, d);
& printf(&quot;Integer part = %g, fractional part = %g\n&quot;, i, f);
& return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS