Meters to Feet and Inches Program pg. 73 with comments

Hey all,
I found this program quite difficult to understand at first so I spent about 40 minutes breaking it down line by line and since a lot of people have said they found it difficult too, I’m posting the program with my own explanatory comments alongside. It isn’t perfect but might help someone else here.

[code]void metersToFeetAndInches(double meters, int *ftPtr, double *inPtr) {
//From main(), where this function gets called, 3.0 gets passed in as the value for meters, and then this function is supplied with locations (&feet and &inches) where the values for feet and inches can be stored. So the placeholders/parameters for these are inevitably pointers to an address.

//Convert meters to feet. 1 meter = 3.281 feet so we need a double.

double rawFeet = meters * 3.281; //variable 'rawFeet' would appear to be total feet (i.e. not broken down into inches). rawFeet has a value of 9.483ft.

int feet = (int)floor(rawFeet); //floor() is used to convert rawFeet into an integer stored in variable feet. Feet has a value of 9.

printf("Storing %d to the address %p\n", feet, ftPtr); //Store the number of feet at the supplied address
*ftPtr = feet;  //stores that integer in *ftPtr



//Calculate inches
double fractionalFoot = rawFeet - feet; //floor() and subtraction are used to break rawFeet into its integer and fractional parts. floor() has taken rawFeet which was 9.843ft and provided us with the integer 9 which is our value for feet. So here fractionalFoot = 9.843ft - 9 = 0.843ft. Note: This 0.843 is still measured in feet.
double inches = fractionalFoot * 12.0;  //inches = 0.843feet * 12.0 inches (as 1 foot = 12 inches). Inches evaluates to 10.1.

//Store the number of inches at the supplied address
printf("Storing %.2f to the address %p\n", inches, inPtr);
*inPtr = inches;

}

int main(int argc, const char * argv[]) {
double meters = 3.0;
int feet;
double inches;

metersToFeetAndInches(meters, &feet, &inches);
printf("%.1f meters is equal to %d feet and %.1f inches.", meters, feet, inches);

return 0;

}[/code]

1 Like

Can someone explain the code line under //How many complete feet as an unsigned int?

Why is unsigned int in brackets, and what purpose does floor serve here?

These are completely new concepts that just appear out of nowhere!

Thank you.

I had the same problem with “floor”, it turns out it is a function in the <math.h> library. A hint, when something new that doesn’t make sense pops up, start typing the function (or whatever) in Xcode and look for the “more” at the bottom of the helper suggestions, that link will go to the documentation. Here it is for convenience. Basically, in this program it is used to round the value of rawFeet to an even integer without rounding up by setting the limit to the highest int value of itself. So rawFeet = 9.84300. “floor” converts that to the highest int, without going over 9 (which makes the nearest int 9) and stores that value in feet.

FLOOR(3) BSD Library Functions Manual FLOOR(3)

NAME
floor – round to largest integral value not greater than x

SYNOPSIS
#include <math.h>

 double
 floor(double x);

 long double
 floorl(long double x);

 float
 floorf(float x);

DESCRIPTION
The floor() functions return the largest integral value less than or equal to x.

SPECIAL VALUES
floor(±_) returns ±0.

 floor(+-infinity) returns +-infinity.

VECTOR OPERATIONS
If you need to apply the floor() function to SIMD vectors or arrays, using the following functions pro-vided provided
vided by the Accelerate.framework may give significantly better performance:

 #include <Accelerate/Accelerate.h>

 vFloat vfloorf(vFloat x);
 void vvfloorf(float *y, const float *x, const int *n);
 void vvfloor(double *y, const double *x, const int *n);

SEE ALSO
ceil(3), round(3), trunc(3), math(3)

STANDARDS
The floor() functions conform to ISO/IEC 9899:2011.

BSD December 11, 2006 BSD

Thank you Haolejon!! :smiley:

Is it correct that a new concept of typecasting was added here?
Where floor() does not normally return an unsigned integer, we have to typecast (i.e. “force”) the floor function to do so by preceding it with (unsigned int)?
Hence:
unsigned int feet = (unsigned int)floor(rawFeet);

I really appreciate how BNR teaches, point you in the direction but no training wheels, teaching problem solving from all directions.

[quote]Where floor() does not normally return an unsigned integer, we have to typecast (i.e. “force”) the floor function to do so by preceding it with (unsigned int)?
[/quote]
To be more precise here, typecasting doesn’t really force the floor function to return an unsigned integer. The function still returns a value of the type promised in its declaration, but typecasting takes that value and converts it to a value of the desired type.

[code]double floor (double x);
// Takes a double and returns a double

unsigned int feet = (unsigned int)floor (rawFeet);
[/code]
So the above code can be written more verbosely like this.

[code]double floor (double x);
// Takes a double and returns a double

const double v1 = floor (rawFeet);
unsigned int feet = (unsigned int)v1
[/code]