writing functions.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 9
Reputation: jazzyangelz is an unknown quantity at this point 
Solved Threads: 0
jazzyangelz jazzyangelz is offline Offline
Newbie Poster

writing functions.

 
0
  #1
Oct 20th, 2008
I need help writing functions. In this program I need to be able to implement a number of useful functions using a header file and an implementation file. I also need to place the prototypes for my function in a .h file, and implement these functions in a .cpp file. I also need to write a driver to test your functions before you upload them.

Deliverables:
MyFunctions.h
MyFunctions.cpp
Driver.cpp

Function:

1. maximum
a. Precondition
i. two integer values exist
b. Postcondition
i. The value of the largest integer is returned.
ii. The original integers are unchanged
iii. If the integers have the same value then the value of either integer is returned.
c. Return
i. integer
d. Description
i. Function returns the value of the larger of two integers.
e. Prototype: int maximum ( int, int );



This is my headerfile so far.


/**************************************
headerfile
***************************************/

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include <iostream>

using namespace std;

int maximum (int, int);
double maximum (double, double);
int minimum (int, int);
double minimum (double, double);
int absolute (int);
double absolute (double);
double power (double, int);
double squareRoot (double);
double hypotenuse(double, double);


and my driver file
/**************************************
driver.cpp
***************************************/


#include "function.h"
int main ()

{
if(intmaximum>int)
{
cout<<intmax;
}
else if(intmax==int)
{
cout<<intmax<<int;
}
else
cout<<"ERROR!"<<endl;




}

i would really appreciate some help. Thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 429
Reputation: Denniz is on a distinguished road 
Solved Threads: 15
Denniz's Avatar
Denniz Denniz is offline Offline
Posting Pro in Training

Re: writing functions.

 
0
  #2
Oct 20th, 2008
Have you tried to compile?

Your header file needs the statement "#endif" at the end. The inclusion detection system works in this way:
  1. #ifndef FUNCTION_H
  2. #define FUNCTION_H
  3.  
  4. // Put all your declaration here
  5.  
  6. #endif

The reason for doing this is to prevent the compiler from parsing the same header file more than once (thus preventing duplicated declaration) when compiling a source file. The #endif statement tells the preprocessor the end of the scope for this inclusion detection (similar to the closing brace } ).

Your driver function simply doesn't work.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: jazzyangelz is an unknown quantity at this point 
Solved Threads: 0
jazzyangelz jazzyangelz is offline Offline
Newbie Poster

Re: writing functions.

 
0
  #3
Oct 20th, 2008
okay i fixed it now thank you.... although I am still very much confused on my driver function

/**************************************
**********header*****************************/

#ifndef FUNCTION_H
#define FUNCTION_H

#include <iostream>

using namespace std;

function prototypes
int maximum (int, int);
double maximum (double, double);
int minimum (int, int);
double minimum (double, double);
int absolute (int);
double absolute (double);
double power (double, int);
double squareRoot (double);
double hypotenuse(double, double);


#endif
Last edited by jazzyangelz; Oct 20th, 2008 at 9:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 429
Reputation: Denniz is on a distinguished road 
Solved Threads: 15
Denniz's Avatar
Denniz Denniz is offline Offline
Posting Pro in Training

Re: writing functions.

 
0
  #4
Oct 20th, 2008
Try writing out the function and post it here. The driver file is supposed to call the function, pass parameters to it, and gets return value (if any) back. It is used for the purpose of testing whether the functions work correctly.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: jazzyangelz is an unknown quantity at this point 
Solved Threads: 0
jazzyangelz jazzyangelz is offline Offline
Newbie Poster

Re: writing functions.

 
0
  #5
Oct 20th, 2008
/********myfunctions.cpp**********/
#include "functions.h"


double hypotenuse(double a, double b)//function definition
{

double c;

c=(((a/c)+c)/2)+((b/c)+c)/2)));

return c;

}


/**************************************
driver.cpp
***************************************/


#include "functions.h"
int main ()
{
double a, b, c;
a=2.5;
b=3.5;
c=hypotenuse( a, b);//function call
cout<<hypotenuse<<endl;
return 0;

}

/**************************************
**********header*****************************/

#ifndef FUNCTION_H
#define FUNCTION_H

#include <iostream>

using namespace std;

//declarations
double hypotenuse(double, double);


#endif



okay so this is what I have so far. I removed the rest of the function prototypes to see if this worked. But I still get compile errors. Something is wrong with the parentheses. I am trying to get the absolute value of c the hypotenuse.
Last edited by jazzyangelz; Oct 20th, 2008 at 10:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 429
Reputation: Denniz is on a distinguished road 
Solved Threads: 15
Denniz's Avatar
Denniz Denniz is offline Offline
Posting Pro in Training

Re: writing functions.

 
0
  #6
Oct 20th, 2008
The parentheses are wrong. Look closely:

  1. c=(((a/c)+c)/2)+((b/c)+c)/2)));

Also, are you sure that's the correct formula for calculating the hypotenuse? Why is variable "c" used in the formula for computing "c" ? Even if the formula is right, the variable "c" isn't initialized at all. And I am quite sure the formula itself is wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: jazzyangelz is an unknown quantity at this point 
Solved Threads: 0
jazzyangelz jazzyangelz is offline Offline
Newbie Poster

Re: writing functions.

 
0
  #7
Oct 20th, 2008
Your right this is the wrong formula. Is this correct?

hypotenuse=

c=((a*a)+(b*b))
c2=c*c

/********myfunctions.cpp**********/
#include "functions.h"


double hypotenuse(double a, double b)//function definition
{

double c, c2;

c=((a*a)+(b*b))
c2=c*c

return c2;

}


/**************************************
driver.cpp
***************************************/


#include "functions.h"
int main ()
{
double a, b, c, c2;
a=2.5;
b=3.5;
c2=hypotenuse( a, b);//function call
cout<<hypotenuse<<endl;
return 0;

}

/**************************************
**********header*****************************/

#ifndef FUNCTION_H
#define FUNCTION_H

#include <iostream>

using namespace std;

//declarations
double hypotenuse(double, double);


#endif
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 429
Reputation: Denniz is on a distinguished road 
Solved Threads: 15
Denniz's Avatar
Denniz Denniz is offline Offline
Posting Pro in Training

Re: writing functions.

 
0
  #8
Oct 20th, 2008
You should use the function sqrt() to find the square root of a variable.

For example:
c = sqrt(x);

You will need to include the header file <math.h> to use this function though.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: jazzyangelz is an unknown quantity at this point 
Solved Threads: 0
jazzyangelz jazzyangelz is offline Offline
Newbie Poster

Re: writing functions.

 
0
  #9
Oct 20th, 2008
I wish we were allowed to use functions from the cmath library but were not. We have to come up with out own equations.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 7
Reputation: Trader09 is an unknown quantity at this point 
Solved Threads: 0
Trader09 Trader09 is offline Offline
Newbie Poster

Re: writing functions.

 
0
  #10
Oct 21st, 2008
Originally Posted by jazzyangelz View Post
I wish we were allowed to use functions from the cmath library but were not. We have to come up with out own equations.
maybe this would work..

double hypotenuse(double a, double b)
{
double R;
R = squareRoot(power(a,2) + power(b,2));
return R;
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC