meters to feet inches (rounding inches)

This is a discussion on meters to feet inches (rounding inches) within the C Programming forums, part of the General Programming Boards category; Whats up, I got a small problem with an assignment: so I am supposed to convert feet and inches to ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    meters to feet inches (rounding inches)

    Whats up, I got a small problem with an assignment: so I am supposed to convert feet and inches to meters; however, the function i must use must have feet and inches as (int) variables. by doing this the inch units will not be rounded....

    any idea how can i fix this using "int"?

    Thank you in advance...

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    void meters_to_feet_inches(double m, int *ft, int *in);
    int main()
    {
    	int feet, inches;
    	double meters;
    	
    	printf("enter meters to be converted\n");
    	scanf("%lf", &meters);
    	meters_to_feet_inches(meters, &feet, &inches);
    	printf("meters in inches is = %d feet, %d inches\n", feet, inches);
    
    
    
    	return 0;
    }
    
    
    
    
    void meters_to_feet_inches(double m, int *ft, int *in)
    {
    	double inft, temp;
    	inft=m*39.37;
    	*ft=inft/12;
    	*in=(int)inft%12;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    To round off a double:
    set your original translated unit to temp (as a double, still). Now add 0.5 to it, and continue.

    When that number is cast back to an int, it will be rounded off.

    BTW, you're supposed to round from a meter to feet and inches, not the other way around, according to your program code.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Thank you, it seems to work now though i dont know how the 0.5 makes it work

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Integer math truncates (cuts off) the part of a number less than one.

    So if you had 1.4 and added 0.5 you'd have 1.9, and after int math, that would be truncated to 1. so it rounds correctly.

    If the value was 1.5 instead, then 0.5 added to it, bring it to 2.0, which is then truncated by int math to just 2.

    So again, it's rounded just the right way.

    I don't know whether your program needed to round off the inches or not, because it wasn't explained, but that's what you asked about - so you go it, and it's a good trick to remember.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input problem when using user-defined functions.
    By 843 in forum C Programming
    Replies: 12
    Last Post: 10-16-2010, 01:33 PM
  2. Replies: 12
    Last Post: 07-05-2010, 08:57 PM
  3. Help please
    By jsking09 in forum C Programming
    Replies: 12
    Last Post: 02-15-2010, 04:08 AM
  4. Please someone help
    By jsking09 in forum C Programming
    Replies: 6
    Last Post: 02-14-2010, 11:29 PM
  5. Replies: 2
    Last Post: 12-09-2009, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21