Thread: Newbie at programming (for now) and in need of help

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    21

    Newbie at programming (for now) and in need of help

    I'm relatively sucky at programming and I don't even know where to begin on this assignment I have due. If anyone can help it would be much appreciated.

    C programming.

    Write a C program that defines and tests a function for converting Fahrenheit temperatures to Celsius. Your program should include three functions:

    * A conversion function to perform the actual conversion. This function should take one parameter, a double representing a Fahrenheit temperature, and return the equivalent Celsius temperature as a double. (So, for example, your_function(32) should return 0.)

    * A convert-and-print function that calls the conversion function and prints its input and output nicely -- for example:

    32 degrees Fahrenheit is 0 degrees Celsius

    (Don't worry too much about printing appropriate numbers of digits after the decimal point; you can do whatever is easy, or read the man page or other documentation for printf to find out how to get more control.)

    * A main function that calls the convert-and-print function at least four times with different inputs that you think will illustrate that the conversion is working right. (So this program is self-contained and doesn't prompt the user for anything!)


    I'm good with loops, but this program requires recursion which I'm terrible at. Thanks all

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, can you please clarify what you have done so far, and where you are stuck? This forum is not "post your assignment and we write it for you", it is "you do the work, we help you out when you are stuck or don't understand how it works".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    jus get the formula to convert fahrenheit to celsius ...

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    you dont need recursion on this code you even on main u could make it...

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    sorry about that. well the thing is, i'm having a hard time visualizing how this is done. i think i understand it conceptually not, but there's no guarantee about my program. i'll post it later today.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    okay this is what i have so far and its getting screwy with me.
    Code:
    #include <stdio.h>
    
    double convertFahrenheit(void)
    double convertPrint(void)
    
    int main(void)
    {
        convertPrint(temp);
    
    }
    
    double convertFahrenheit(void)
    {
        double number;
        double celsius;
    
    
        celsius = ((number-32)*(5/9));
    
        return celsius;
    }
    
    double convertPrint(temp)
    {
        double temp;
        temp = 212;
        printf("Your number %d", temp);
    
        return temp;
    }
    what im trying to do is get convertPrint to call convertFahrenheit (which i don't exactly know how to do). and then get the main function to call convertPrint (as shown from the criteria above). i tried doing a small run through with one number (212, boiling point), but i'm getting some syntax errors. input?

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Without reading your question, or intent I see something wrong.

    Code:
    double convertFahrenheit(void)
    {
        double number; <--- NOT INITIALIZED! Should be a parameter!!!
        double celsius;
    
    
        celsius = ((number-32)*(5/9));
    
        return celsius;
    }
    
    double convertPrint(temp) <-- What the hell?? You need a type
    {
        double temp;
        temp = 212;
        printf("Your number &#37;d", temp);
    
        return temp;
    }
    Change those to 32.0, 5.0, and 9.0, respectively.
    Last edited by master5001; 10-29-2008 at 05:29 PM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Remember, 5/9 is 0. Also remember, you have to give the types of the parameters when you declare functions. And, your prototype at the top and the actual function at the bottom have to agree as to the function parameters. Too, to call a function you type its name followed by a set of parentheses (which contain the arguments you want to pass).

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by tabstop View Post
    Remember, 5/9 is 0. Also remember, you have to give the types of the parameters when you declare functions. And, your prototype at the top and the actual function at the bottom have to agree as to the function parameters. Too, to call a function you type its name followed by a set of parentheses (which contain the arguments you want to pass).
    tabstop is like a tactful version of me at times

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    redux, that program was obviously bad, this is what i have currently...

    Code:
    #include <stdio.h>
    
    double convertFahrenheit();
    double convertPrint();
    
    int main(void)
    {
        convertPrint(212);
    
    
        return 0;
    }
    
    double convertFahrenheit()
    {
        double number;
        double celsius;
    
        celsius = (((number-32)*5)/9);
    
    
        return celsius;
    }
    
    double convertPrint()
    {
        double temp;
    
    
        temp = convertFahrenheit();
        printf("Your tempurature in Fahrenheit is %lf", temp);
    
        return temp;
    }
    what i want it to do is convert convertPrint(212) into celsius. then put convertPrint(32) and other types of fahrenheit degrees calculated.......but it's not quite there yet....

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok bud, I think you are having trouble with your understanding of functions. Do pay attention to my remarks about using 32.0 (the .0 is not trivial).

    Example:
    Code:
    double convertFahrenheit(double number)
    {
        double celsius;
    
        celsius = (((number-32.0)*5.0)/9.0);
    
        return celsius;
    }
    Similar should be done for the other function. It currently has no parameter, yet it should have one.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    actually it works in other programs without the .0, so it really isn't the problem. what i'm trying to do is get the main function to call convertPrint which calls the convertFahrenheit equation. and when i put that 212 in the main, i wanted it to go through convertPrint which goes through convertFahrenheit to convert the 212 into 100 celsius. problem is the code doesn't work....

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The way you have it now is all well and dandy since it will implicitly convert to doubles. In any event, I bolded the concept I was mostly trying to get you to focus on.

    And if you explicitly tell the compiler what you want, you can at least do things like this.

    Code:
        celsius = ((number-32)*5.0/9.0);

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    21
    i inputted your .0's but still no dice. what it executes is....

    Your temperature in Celsius is -17.7777778

    the answer's suppose to be 100 celsius. still no idea why convertPrint(212) doesn't take 212 through the convertFahrenheit equation.

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Again... bolded thing is a clue.... I don't have time to play games as I am running late for class...

    Code:
    double convertPrint(double temp)
    {
        temp = convertFahrenheit(temp);
        printf("Your tempurature in Fahrenheit is &#37;lf", temp);
    
        return temp;
    }

Popular pages Recent additions subscribe to a feed