# Help with Function Prototypes

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-12-2011
Valandu
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.
• 10-12-2011
quzah
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.
• 10-13-2011
Valandu
I'm not sure I understand completely. Could you give me an example using the function names I provided?
• 10-13-2011
quzah
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.
• 10-13-2011
Valandu
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?
• 10-13-2011
quzah
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.
• 10-13-2011
Valandu
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); }```
• 10-13-2011
AndrewHunter
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.
• 10-13-2011
laserlight
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?
• 10-13-2011
AndrewHunter
Quote:

Originally Posted by laserlight
You should format your code properly with indentation, e.g.,

Hmm....if this was only taken care of automatically with the code tags. :wink:
• 10-13-2011
CommonTater
Quote:

Originally Posted by AndrewHunter
Hmm....if this was only taken care of automatically with the code tags. :wink:

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```
• 10-13-2011
AndrewHunter
Quote:

Originally Posted by CommonTater
Hey Andrew... Hows trix?

Doing better, been a little sick lately. How's life Tater?
Quote:

Originally Posted by CommonTater
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... :tongue:
• 10-13-2011
CommonTater
Quote:

Originally Posted by AndrewHunter
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...

Quote:

Haha....we basically make those anyway... :tongue:
Oh crap... our secret is out!
• 10-14-2011
lolcats22
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.
• 10-14-2011
quzah