So the next task in my travels is rounding. I don't mean to come on here each week and pester those that have better things to do, but I hope it is proper for me to ask so frequently. (If not, tell me so.
)
The following that I have works. It does just what I need - Round to two decimal places, up or down, based on simple math. However my instructor insists we must have another snippet of her code in the program. Why? I'm not too sure. I guess I'm looking for a how-to-and-why sort of thing.
The /*double scale_factor*/ portion is what I don't get. Without it, it already does what I need. All in all, I don't know what her little portion does or why I'd need it. Again, thank you greatly in advance to all.
The following that I have works. It does just what I need - Round to two decimal places, up or down, based on simple math. However my instructor insists we must have another snippet of her code in the program. Why? I'm not too sure. I guess I'm looking for a how-to-and-why sort of thing.
#include <stdio.h>
#include <math.h>
int main(void)
{
double x;
int rounded_x;
printf ("Welcome.\n");
printf("Enter a positive number: \n");
scanf("%lf", &x);
rounded_x = (int) (x + .5);
// double scale_factor;
// {
// double scale_factor;
// scale_factor = pow(10,rounded_x);
// return (x * scale_factor);
// }
printf("Rounded number is: %.2lf \n", x);
return 0;
}
The /*double scale_factor*/ portion is what I don't get. Without it, it already does what I need. All in all, I don't know what her little portion does or why I'd need it. Again, thank you greatly in advance to all.