Challenge solution with Comments


#1
#include <stdio.h>
#include <math.h>

// I store integer in integer "meters" and addresses are stored in *pointers
void metersToFeetAndInches (double meters, unsigned int *ftPtr, double *intPtr) {
    
    double feet; // 9 feet
    double fractionalReturn; // 0.84299 use this to calculate inches
    
    // convert the number of meter into a flating point number of feet
    double rawFeet = meters * 3.281; //
    fractionalReturn = modf(rawFeet, &feet);
    
    
    //store the number of feet in as unsigned int in main function
    // to do this we use its address that was passed and store in pointer
    if (ftPtr){ // Check if its not NULL
    printf("Storing %f to the address %p\n", feet, ftPtr);
    *ftPtr = feet;
    }
    
    // Calculate inches
    double fractionalFoot = rawFeet - feet;
    double inches = fractionalFoot * 12.0;
    
    // store the number of inches at the supplied address
    if(intPtr){ // check if its not NULL
    printf("Storing %.2f inches to the address %p\n", inches, intPtr);
    *intPtr = inches;
    }
}

// Pass by reference
int main(int argc, const char * argv[])
{

    double aMeters = 3.0;
    unsigned int aFeet = 0;
    double aInches = 0;
    
    // Here I pass value of aMeters and addresses of aFeet and aInches as arguments
    metersToFeetAndInches(aMeters, &aFeet, &aInches);
    printf("%.1f meters is equal to %d feet and %.1f inches.\n", aMeters, aFeet, aInches);
    
    return 0;
}