[code]/***************************** Challenge ***************************/
/In metersToFeetAndInches(), you used floor () and subtraction to break rawFeet into its integer and fractional parts. Change metersToFeetAndInches() to use modf() instead./
#include <stdio.h>
#include <math.h>
void metersToFeetAndInches(double meters, unsigned int *ftPtr, double *inPtr)
{
// This function assumes meters is non-negative.
// Convert the number of meters into a floating-point number of feet
double rawFeet = meters * 3.281; // e.g. 2.4536
/***********How many complete feet as an unsigned int?****************/
// Double data type variables to break the number of feet into it's integer and modulus.
double integerPartFeet;
double fractionPartFeet;
// Passing the address of integerPartFeet as an argument
fractionPartFeet = modf(rawFeet, &integerPartFeet);
// Reference http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf page 264 of 701
// Find the integer part by subtracting the fraction from the total number of feet.
integerPartFeet = rawFeet - fractionPartFeet;
// Check for a non-null pointer
if (ftPtr) {
// Store the number of feet at the supplied address
printf("Storing %.2f ft to the address %p\n", integerPartFeet, ftPtr);
*ftPtr = integerPartFeet;
}
// Calculate inches from the modulus
double inches = fractionPartFeet * 12.0;
// Check for a non-null pointer
if (inPtr) {
// Store the number of inches at the supplied address
printf("Storing %.2f in to the address %p\n", inches, inPtr);
*inPtr = inches;
}
}
int main(int argc, const char * argv[])
{
// Arguments that will be needed int the function metersToFeetAndInches
double meters = 3.0;
unsigned int integerPartFeet;
double inches;
// Call the function meterToFeetAndInches
metersToFeetAndInches(meters, &integerPartFeet, &inches);
// Display the results of the conversion in a readable format
// %.1f will be a float data type with one decimal place and %u is for the unsign integer.
printf("%.1f meters is equal to %u feet and %.1f inches.\n", meters, integerPartFeet, inches);
return 0;
}[/code]