Thread: need help with homework code

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    21

    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. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    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
    Last edited by PING; 02-15-2005 at 10:41 PM.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM