# Thread: need help with homework code

1. ## need help with homework code

If someone could help me with this code I would be very greatful.
I need to get the depth value out of the main function so i can use it in the function celcius_at_depth. and then how would i get the celcius value into the farenheight function? Thank you.
Code:
```/*
Filename: HW3_2AGS.c
Author:

Explanation: This program takes a depth(in kilometers) inside the
earth as input data and then computes and displays the temperature
at this depth for Celcius and Ferenheight.

Algorithm
1. Explain program to user.
2. Take in data from file depth.txt
3. Calculate the data.
4. Output the data to temperature.txt

include files used <stdio.h>

variables used
double celcius
double ferenheight
double depth

Functions used

celcius_at_depth()
farenheight()
*/
#include <stdio.h>

void celcius_at_depth(double);
void farenheight(double);

int
main(void)
{
double celcius, farenheight, depth;
FILE *inp, /* pointer to input file */
*outp; /* pointer to output file */

inp = fopen("depth.txt", "r");
outp = fopen("temperature.txt", "w");
/* Explain program	 */
printf("This program takes a depth(in kilometers)\n"
"inside the earth as input data and then \n"
"computes and displays the temperature\n"
"at this depth for Celcius and Ferenheight\n"
"Input is collected from a text file and is written\n"
"to a text file.\n");
fscanf(inp, "%lf", &depth);

return (0);
}
void celcius_at_depth(double celcius)
{
return 10 * depth +20;

}
void farenheight(double farenheight)
{
return 1.8 * celcius + 32;
}```

2. your code is very buggy

1) after fopen() you need to check whether the function has been successful..
you will require something like
Code:
```       if(inp==NULL||outp==NULL)
{
print("ėrror opening file");
return 0;
}```
2) you need to call the functions..and the functions return some values, so their return type is not void. it should be double.

3) you need to call the functions and assign the values returned by them to some variables
Code:
```      double farn,cel;
cel=celcius_at_depth(depth);
farn=farenheight(cel);```
4) the functions do not take in proper variables..
Code:
```
double celsius_at_depth(double dep)
{
return 10 * dep +20;
}
double farenheight(double celsius)
{
return 1.8 * celcius + 32;
}```
there might be other things that you might want to look into. you are not saving the results in the temperature.txt file. you will need to do that as well

3. Code:
```void farenheight(double farenheight)
{
return 1.8 * celcius + 32;
} // celcius is not in the scope of this function. You need to pass celcius to it.
//There is a similar problem with your other function```