GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Sep-2005, 15:11
boousaf boousaf is offline
New Member
 
Join Date: Sep 2005
Posts: 7
boousaf is on a distinguished road

First prog, using functions that pass values...


Hi guys,
This is my first post as I am a newbie to c... Pardon the ignorance. I am taking my first prog class and I have this assignment: Make a program that contains five functions for the following problem.

1)read depth of a point inside the earth in a function, get_depth() and return it to main
2)Pass depth to function compute_cel() that computes and returns celsius temp at that point using formula: celsius = 18*depth_in_km+20
3)Write another fcn (compute_fahren()) that accepts celsius temp and computes and returns Fahrenheit temp to main. Formula for converting c to f = 1.8*celsius+32
4)Print all three items in a function called print_info(). Remember you have to pass all variable to this function before you can print them.
5)Write top_of_info() function - im fine with this one.

Here is what I have, and im just stuck... can anyone point me in a direction where I can at least begin to correct this thing? Thank you so much!
Chris



CPP / C++ / C Code:
#include <stdio.h>

void top_of_page_info (void);
float get_depth (void);
float compute_cel (get_depth);
float compute_fahren (compute_cel);
float print_info (float depth_in_km, float celsius, float fahrenheit);


int main (void)
{   
    float depth_in_km;
    float celsius;
    float fahrenheit;
    
    printf("Enter depth(in km) of a point inside the earth:");
    scanf("%f", &depth_in_km);
    printf("The depth of a point inside the earth is:%f\n", depth_in_km);
    printf("The Celsius temp. at that point is:%f\n", celsius);
    printf("The Fahrenheit temp. at that point is:&f\n", fahrenheit);
    
}    /* end of main program */    


#define NAME "Christopher ******"
#define DASHES "------------------------------------------------------"
void top_of_page_info (void)
{     printf ("%s\n\n", DASHES);
      printf ("\t\tProgrammer Name: %s\n", NAME);
      printf ("\t\tLab No.        : 03\n");
      printf ("\t\tCourse/Section : CSI 147/Section 875\n");
      printf ("\t\tDate Due       : 09/30/2005\n\n");
      printf ("\t\tInstructor     : Dr. Raj Gill, Comp Sci\n");
      printf ("%s\n\n\n", DASHES);
}     
/* ------------------------end top_of_page_info-------------------------------------*/
 
 


float get_depth (void)
{
      float depth_in_km;
      return depth_in_km;
}



float compute_cel (get_depth)
{
      float depth_in_km;
      float celsius;
      celsius = 18 * depth_in_km + 20;
      return celsius;            
}





float compute_fahren (compute_cel)
{
      float celsius;
      float fahrenheit;
      fahrenheit = 1.8 * celsius + 32;
      return fahrenheit;
}



               
float print_info (float depth_in_km, float celsius, float fahrenheit)
{
      return depth_in_km, celsius, fahrenheit;
}
Last edited by LuciWiz : 28-Sep-2005 at 02:22. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 27-Sep-2005, 17:00
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: First prog, using functions that pass values...


hi..
The function declaration should be of this type:
CPP / C++ / C Code:
return-type function-name(parameter list)
{
declarations and statements;
return expression;
}
The return-type specifies the type of data that the function returns. The parameter list is a comma-separated list of variable names and their associated types that receive the values of the arguments when the function is called . All function parameters must be declared individually, each including both the type and name. That is, the parameter declaration list for a function takes this general form:
f(type varname1, type varname2, . . . , type varnameN)

In your function compute_cell, you have not defined the parameter list correctly.
Hence it should be written as:
CPP / C++ / C Code:
float compute_cel (float depth_in_km);
similarly, the function should be written as:
CPP / C++ / C Code:
float compute_fahren (float celsius);

You said:
Quote:
read depth of a point inside the earth in a function, get_depth() and return it to main
You can use scanf inside the get_depth function and return the float value.
CPP / C++ / C Code:
float get_depth(void){
float depth_in_km;
printf("Enter depth(in km) of a point inside the earth:");
scanf("%f", &depth_in_km);
return depth_in_km;
}

and your compute_cel and compute_fahren functions should be changes as:
CPP / C++ / C Code:
float compute_cel (float depth_in_km)
{
float celsius;
celsius = 18 * depth_in_km + 20;
return celsius; 
}

float compute_fahren (float celsius){
float fahrenheit;
fahrenheit = 1.8 * celsius + 32;
return fahrenheit;
}
Do you understand till now?
Now on to the main function.
It should look like:
CPP / C++ / C Code:
int main (){ 
float depth_in_km;
float celsius;
float fahrenheit;

depth_in_km=get_depth();             //get the depth using the function
printf("The depth of a point inside the earth is:%f\n", depth_in_km);
celsius=compute_cel(depth_in_km); //compute the celsius using the function compute_cel
printf("The Celsius temp. at that point is:%f\n", celsius);
fahrenheit=compute_fahren(celsius);//compute the fahrenheit using the function compute_fahren
printf("The Fahrenheit temp. at that point is:&f\n", fahrenheit);

} /* end of main program */

This link describes about using functions in c
Using Functions in C
  #3  
Old 27-Sep-2005, 17:10
boousaf boousaf is offline
New Member
 
Join Date: Sep 2005
Posts: 7
boousaf is on a distinguished road

Re: First prog, using functions that pass values...


Paramesh,
That was MOST helpful, thank you very much!
Chris
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
Noob question on c arrays and functions brett C Programming Language 1 20-Apr-2005 04:59
conflict between printf and stdarg.h va functions mirizar C Programming Language 3 12-Jul-2004 09:11
Understanding functions tommy69 C Programming Language 15 15-Mar-2004 18:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:02.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.