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

Edited 3 Years Ago by happygeek: fixed formatting

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.

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

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.

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

Edited 3 Years Ago by happygeek: fixed formatting

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.

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

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.

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;
}

Edited 3 Years Ago by happygeek: fixed formatting

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.

This article has been dead for over six months. Start a new discussion instead.