Help with Function Prototypes

This is a discussion on Help with Function Prototypes within the C Programming forums, part of the General Programming Boards category; Code: /* Alfredo Perez */ /* earth.c */ /* A program that takes depth in KM inside of earth and ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    Help with Function Prototypes

    Code:
    /* Alfredo Perez */
    /* earth.c */
    /* A program that takes depth in KM inside of earth and displays the 
    temperature at this depth in degrees Celsius and degrees Fahrenheit */
    #include <stdio.h>
    /* Function Declaration */
    double celsius_at_depth(double depth);
    double fahrenheit(double celsius_at_depth);
    int main()
    {
    double depth;
    double celsius_at_depth;
    double fahrenheit;
    print("Enter depth in kilometers => ");
    scanf("%lf", &depth);
    printf("The temperature is %.0f degrees C or %.1F degrees F.\n", 
    celsius_at_depth, fahrenheit);
    return (0);
    }
    /* Compute celsius with given depth */
    double celsius_at_depth(double depth)
    {
    return (10*depth+20);
    }
    /* Convert Celsius to Fahrenheit */
    double fahrenheit(double celsius_at_depth)
    {
    return ((9.0/5.0)*celsius_at_depth+32);
    }
    I need help fixing my program that I have attached here

    I do not get the values that I want when I run the program. The problem is:

    I have to write a program to take a depth (in kilometers) inside the earth as input data; compute and display the temperature at this depth in degrees Celsius and degrees Fahrenheit. So basically, when I compile my program, it should allow me to enter the depth value first. The relevant formulas are:

    Celsius = 10 * depth + 20

    Fahrenheit = 9/5 * Celcius + 32

    I have to include two functions in my program.

    Function celcius_at_depth should compute and return the Celsius temperature at a depth measured in kilometers.

    Function fahrenheit should convert a Celcius temperature to Fahrenheit.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    printf("The temperature is %.0f degrees C or %.1F degrees F.\n",
    celsius_at_depth, fahrenheit);
    return (0);
    }
    /* Compute celsius with given depth */
    double celsius_at_depth(double depth)
    {
    return (10*depth+20);
    }
    /* Convert Celsius to Fahrenheit */
    double fahrenheit(double celsius_at_depth)
    {
    return ((9.0/5.0)*celsius_at_depth+32);
    }
    To call the function correctly here, you need to be using the correct arguments. Consider the function foo:
    Code:
    void foo( int x )
    {
        printf( "x is %d\n", x );
    }
    To use this function correctly, we need to invoke it's name, followed by the correct argument list:
    Code:
    foo( 3 );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    I'm not sure I understand completely. Could you give me an example using the function names I provided?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I hate helping people who won't even try.
    Code:
    printf("The temperature is %.0f degrees C or %.1F degrees F.\n",
    celsius_at_depth, fahrenheit);
    return (0);
    }
    /* Compute celsius with given depth */
    double celsius_at_depth(double depth)
    {
    Do those two lines look even remotely close to one another?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Okay I understand now, but I have a problem when I try to call the function. The compiler tells me that celsius_at_depth and fahrenheit are not functions. Did I not write them correctly?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post your updated code.
    Code:
    void foo( void ); /* function protype so... */
    int main( void )
    {
        foo(); /* ...main knows about it, so we can call it */
        return 0;
    }
    
    void foo( void ) /* actual function definition is here */
    {
        ... do stuff ... 
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Code:
    /* Alfredo Perez */
    /* earth.c */
    /* A program that takes depth in KM inside of earth and displays the 
    temperature at this depth in degrees Celsius and degrees Fahrenheit */
    #include <stdio.h>
    /* Function Declaration */
    void celsius_at_depth(void);
    void fahrenheit(void);
    int main(void)
    {
    depth();
    celsius_at_depth();
    fahrenheit();
    print("Enter depth in kilometers => ");
    scanf("%lf", &depth);
    printf("The temperature is %.0f degrees C or %.1F degrees F.\n", 
    celsius_at_depth, fahrenheit);
    return (0);
    }
    /* Compute celsius with given depth */
    void celsius_at_depth(void)
    {
    return (10*depth+20);
    }
    /* Convert Celsius to Fahrenheit */
    void fahrenheit(void)
    {
    return ((9.0/5.0)*celsius_at_depth+32);
    }

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You have some serious misunderstandings of how functions work, how to declare variables, ect. I suggest you read our function tutorial and brush up on how to declare functions. Maybe read some other tutorials.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    You should format your code properly with indentation, e.g.,
    Code:
    /* Alfredo Perez
     * earth.c
     * A program that takes depth in KM inside of earth and displays the 
     * temperature at this depth in degrees Celsius and degrees Fahrenheit
     */
    
    #include <stdio.h>
    
    /* Function Declaration */
    void celsius_at_depth(void);
    void fahrenheit(void);
    
    int main(void)
    {
        depth();
        celsius_at_depth();
        fahrenheit();
        print("Enter depth in kilometers => ");
        scanf("%lf", &depth);
        printf("The temperature is %.0f degrees C or %.1F degrees F.\n", 
        celsius_at_depth, fahrenheit);
        return (0);
    }
    
    /* Compute celsius with given depth */
    void celsius_at_depth(void)
    {
        return (10*depth+20);
    }
    
    /* Convert Celsius to Fahrenheit */
    void fahrenheit(void)
    {
        return ((9.0/5.0)*celsius_at_depth+32);
    }
    Now, your program should fail to compile with some errors. What are they, and what do you think they mean?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by laserlight View Post
    You should format your code properly with indentation, e.g.,
    Hmm....if this was only taken care of automatically with the code tags.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Hmm....if this was only taken care of automatically with the code tags.
    Hey Andrew... Hows trix?

    No the code tags should NOT handle this... we would end up unleashing a whole new generation of GWBASIC programmers on the world...

    Code:
    PRINT "Hello World this is BASIC"
    PRINT "And it's so cool"
    GOTO 1

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by CommonTater View Post
    Hey Andrew... Hows trix?
    Doing better, been a little sick lately. How's life Tater?
    Quote Originally Posted by CommonTater View Post
    No the code tags should NOT handle this... we would end up unleashing a whole new generation of GWBASIC programmers on the world...
    Haha....we basically make those anyway...
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Doing better, been a little sick lately. How's life Tater?
    Ech... you know, same old same old... on the tail end a cold as well...

    Haha....we basically make those anyway...
    Oh crap... our secret is out!

  14. #14
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    Hello,

    I am having some problems with the same program too. Here is my code:

    Code:
    #include <stdio.h>
    
    
    double celsius_at_depth(double depth);
    double fahrenheit(double celsius_at_depth);  //Prototypes of the functions
    
    
    int main(void)
    {
      double depth, fahrenheit, celsius_at_depth;
      printf("Enter depth in kilometers=> ");
      scanf("%lf", &depth);
      printf("The temperature is %.0lf degrees C or %.1lf degrees F.", celsius_at_depth(depth), fahrenheit(celsius_at_depth));
    }
    
    
    double celsius_at_depth(double depth)
    {
     return (10 * depth + 20);
    }
    
    
    double fahrenheit(double celsius_at_depth)
    {
    return ( (9.0/5.0)*celsius_at_depth + 32);
    }
    I read Quzah's post and read the function tutorial, but I'm getting these errors:

    earth.c:13:84: error: called object 'celsius_at_depth' is not a function
    earth.c:13:103: error: called object 'fahrenheit' is not a function

    I don't understand what I'm not including when I try to call the function in main. Am I mixing up my functions below main and not connecting something? Or are the function names and variables skewed (do I need to write them differently?)

    Hints/Help appreciated.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's a bad idea to name your variables the same thing as your function names.


    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function prototypes
    By kawaikx15 in forum C Programming
    Replies: 1
    Last Post: 06-28-2011, 09:20 PM
  2. Function Prototypes
    By askinne2 in forum C Programming
    Replies: 6
    Last Post: 10-01-2010, 01:01 PM
  3. Function prototypes
    By XxPKMNxX in forum C Programming
    Replies: 4
    Last Post: 10-28-2009, 02:33 AM
  4. Function Prototypes
    By Kryota in forum C++ Programming
    Replies: 11
    Last Post: 12-28-2007, 03:52 AM
  5. 2 function prototypes
    By newbie101 in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2006, 05:49 AM

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