Keeping the fractional part of a quotient

This is a discussion on Keeping the fractional part of a quotient within the C Programming forums, part of the General Programming Boards category; I am working on a lab problem. It is: Write a program that calculates the speed of sound (a) in ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    16

    Keeping the fractional part of a quotient

    I am working on a lab problem. It is: Write a program that calculates the speed of sound (a) in air of a given temperature T (in Fahrenheit). The formula to compute the speed in feet/second:
    a = 1086 * sqrt ((5T+297)/247)
    Be sure your program does not lose the fractional part of the quotient in the formula shown. As part of your solution, write and call a function that displays instructions to the program user.

    I know how to do everything else but keep the fractional part. Below is my program. If anyone could help me out with this I'd really appreciate it. In another problem from this lab we used the scale function to round a number so I'm not sure if we might need to use this function again?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (void)
    {
    	float sound, temperature;
    	printf("Enter a temperature T in fahrenheit: ");
    	scanf("%f", &temperature);
    
    	sound = 1086 * (sqrt ((5 * temperature + 297) / 247));
    
    	printf("When the temperature is %f F, the speed of sound in the air is %f\n", temperature, sound);
    	return 0;
    }
    Last edited by Salem; 03-17-2011 at 08:26 AM. Reason: Added [code][/code] tags

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    What you have looks like it should keep the fractional parts alright, although just to be safe, I'd recommend specifying the constants as floating point numbers (5.0 instead of 5, 247.0 instead of 247, etc.).

    Also, please use [CODE] tags when posting code.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    First off, thanks for posting code! But you should use CODE tags (they save indentation and make it easier to read)

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (void)
    {
        float sound, temperature;
        printf("Enter a temperature T in fahrenheit: ");
        scanf("%f", &temperature);
    
        sound = 1086 * (sqrt ((5 * temperature + 297) / 247));
    
        printf("When the temperature is %f F, the speed of sound in the air is %f\n",     temperature, sound);
        return 0;
    }
    (isn't that better?)

    Now, to your question...

    Are you losing the fractional part? Did you try using a calculator? I ask because this problem has been around the Internet for a while. I think they may just be wanting you to use floats and nothing more. I compiled it as is and saw no loss in precision. However, it's good defensive code to do what Clairvoyant suggests (or to cast your integers to floats).

    You may also want to put parenthesis around the 5*t term, just so that it's clear to someone that you intend to NOT add the 297 first. This is especially useful if you program professionally and you or others may look at this code years later.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    I really apologize for the lack of tags in my original post. I'm a first time user of this thread and shamefully admit that I didn't read the rules well enough. My fault entirely, it won't happen again.

    The wording threw me off with this problem and its context in my course. Thanks for all the help everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suspicious Pointer Conversion
    By mr_spanky202 in forum C Programming
    Replies: 35
    Last Post: 04-11-2003, 01:35 PM
  2. RichEdit, how to centralise the focused part?
    By Zahl in forum Windows Programming
    Replies: 1
    Last Post: 11-25-2002, 03:35 PM
  3. Beginning MFC (Prosise) Part III - Now What? :: C++
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2002, 06:58 PM
  4. keeping track of # of files
    By blind_collision in forum C Programming
    Replies: 1
    Last Post: 10-13-2001, 10:10 AM

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