954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

writing functions.

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

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<

jazzyangelz
Newbie Poster
9 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Have you tried to compile?

Your header file needs the statement "#endif" at the end. The inclusion detection system works in this way:

#ifndef FUNCTION_H
#define FUNCTION_H

// Put all your declaration here

#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.

Denniz
Posting Pro in Training
429 posts since Sep 2008
Reputation Points: 118
Solved Threads: 15
 

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

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

jazzyangelz
Newbie Poster
9 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Denniz
Posting Pro in Training
429 posts since Sep 2008
Reputation Points: 118
Solved Threads: 15
 

/********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<

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.

jazzyangelz
Newbie Poster
9 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

The parentheses are wrong. Look closely:

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.

Denniz
Posting Pro in Training
429 posts since Sep 2008
Reputation Points: 118
Solved Threads: 15
 

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<

using namespace std;

//declarations double hypotenuse(double, double);

#endif

jazzyangelz
Newbie Poster
9 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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 to use this function though.

Denniz
Posting Pro in Training
429 posts since Sep 2008
Reputation Points: 118
Solved Threads: 15
 

I wish we were allowed to use functions from the cmath library but were not. We have to come up with out own equations.

jazzyangelz
Newbie Poster
9 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 
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;
}

Trader09
Newbie Poster
7 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

use the functions youve been building in your program up to this point.

Trader09
Newbie Poster
7 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

I just realize from the other similar thread that this is the last question on your assignment. So before this question, you would have to implement the power function and squareroot function. Why jump straight to this one? You need to implement those two functions and then call them inside this hypotenuse function.

Denniz
Posting Pro in Training
429 posts since Sep 2008
Reputation Points: 118
Solved Threads: 15
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You