No clue


#1

I don’t have a clue where to start on changing the floor() into a modf() could someone plz help

[code]//
// main.c
// PBR Advanced (10)
//
// Created by Cynikz on 15/06/2014.
// Copyright © 2014 Cynikz. All rights reserved.
//

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

void metersToFeetAndInches(double meters, unsigned int *ftPtr, double *inPtr)
{
//This assumes function 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?
unsigned int feet = (unsigned int)floor(rawFeet);
//Store the number of feet at the supplied address
if(ftPtr) {
printf("Storing %u 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(inPtr) {
printf("Storing %.2f to the address %p\n", inches, inPtr);
*inPtr = inches;
}

}

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

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

return 0;

}

[/code]


#2

To get some clues, study the following code (and the comments at the bottom):

//  main.m

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    double PI = M_PI;

    printf ("Floor and modf PI = %f...\n", PI);
    
    double ipart = floor (PI);
    printf ("floor PI: ipart = %f\n", ipart);

    ipart = 0;;
    double fpart = modf (PI, &ipart);
    printf ("modf PI: frac = %f\n", fpart);
    printf ("modf PI: ipart = %f\n", ipart);
    
    return 0;
}

/*
Function: double floor (double x);
 
"The floor() functions return the largest integral value less than or equal to x."
 
Function: double modf (double value, double *iptr);

"The modf () [function] breaks value into integral and fractional parts,
each of which has the same sign as the argument.
It returns the fractional part, and stores the integral part
(as a floating-point number) in the object pointed to by iptr."

*/

Then build and run to see the output produced:

Floor and modf PI = 3.141593...
floor PI: ipart = 3.000000
modf PI: frac = 0.141593
modf PI: ipart = 3.000000

#3

That code doesn’t work it brings up multiple errors for me, plz help, thx


#4

I worked it out now but thanks for the help anyways will post my code below. One thing was that the 9 you get had 9.00000000 instead of the 9. Would be great if someone knew how to fix that. Here is the code that I ended up coming up with.

//
//  main.c
//  PBR Advanced (10)
//
//  Created by Sharon Common on 15/06/2014.
//  Copyright (c) 2014 Jacob. All rights reserved.
//

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

void metersToFeetAndInches(double meters, unsigned int *ftPtr, double *inPtr)
{
    //This assumes function is non-negative
    
    //Convert the number of meters into a floating-point number of feet
    double rawFeet = meters * 3.281; //e.g. 2.4536
    double intergerPart;
    double feet;
    
    //How many complete feet as an unsigned int?
    intergerPart = modf(rawFeet, &feet);
    //Store the number of feet at the supplied address
    if(ftPtr) {
        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(inPtr) {
        printf("Storing %.2f to the address %p\n", inches, inPtr);
        *inPtr = inches;
    }
}

int main(int argc, const char * argv[])
{
    double meters = 3.0;
    unsigned int feet;
    double inches;
    
    metersToFeetAndInches(meters, &feet, &inches);
    printf("%.1f meters is equal to %d feet and %.1f inches.\n",meters, feet, inches);
    
    return 0;
}