In this programming assignment I need to implement a number of useful functions using a header file and an implementation file. I will place the prototypes for my function in a .h file, and implement these functions in a .cpp file. I will also need to write a driver (a program designed to test programs) to test my functions. Sorry I know this is a long post, but I am stuck and would appreciate the help. Thanks!!
Deliverables:
MyFunctions.h
MyFunctions.cpp
Driver.cpp
Function:
maximum
Precondition
i. two integer values exist
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.
Return
i. integer
Description
i. Function returns the value of the larger of two integers.
Prototype: int maximum ( int, int );
maximum
Precondition
i. two double values exist
Postcondition
i. The value of the larger double is returned.
ii. The original doubles are unchanged
iii. If the doubles have the same value then the value of either double is returned.
Return
i. double
Description
i. Function returns the value of the larger of two double.
Prototype: double maximum (double, double);
minimum
Precondition
i. two integer values exist
Postcondition
i. The value of the smallest 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.
Return
i. integer
Description
i. Function returns the value of the smaller of two integers.
Prototype: int minimum ( int, int );
minimum
Precondition
i. two double values exist
Postcondition
i. The value of the smaller double is returned.
ii. The original doubles are unchanged
iii. If the doubles have the same value then the value of either double is returned.
Return
i. double
Description
i. Function returns the value of the smaller of two double.
Prototype: double minimum (double, double);
absolute
Precondition
i. Some integer value exists
Postcondition
i. Integer value is unchanged
Return
i. Integer
Description
i. This function returns the absolute value of an integer
Prototype: int absolute( int );
absolute
Precondition
i. Some double value exists
Postcondition
i. Double value is unchanged
Return
i. Double
Description
i. This function returns the absolute value of a double
Prototype: double absolute( double );
power
Precondition
i. Double value X and integer value Y exist
Postcondition
i. The value of X and Y are unchanged
Return
i. The double X raised to the power of Y
Description
i. This function will calculate XY
Prototype: double power ( double, int );
squareRoot
Precondition
i. Some integer radicand X exists
Postcondition
i. The value of the radicand X is unchanged
Return
i. The square root of X
ii. 0 if an error is encountered
Description
i. The function calculates the square root of a number using Newton’s method with 25 iterations.
Prototype: double squareRoot( double );
hypotenuse
Precondition
i. For some right triangle ABC where side a = side AB and b = side BC, a and b are positive doubleing point values.
Postcondition
i. The values of a and b are unchanged
Return
i. The length the hypotenuse of triangle ABC
Description
i. This function calculates the length of a hypotenuse given the length of the two sides of a right triangle.
Prototype: double hypotenuse ( double, double );
Here are my programs:
MyFunctions.h
#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H
//Function Returns the value of the Larger of two Integers.
int maximum ( int, int );
//Function Returns the value of the Larger of two Integers.
double maximum ( double, double );
//Function Returns the Smaller of two Integers.
int minimum ( int, int );
//Function Returns the Smaller of two Doubles.
double minimum ( double, double );
//Function Returns the absolute of a Integer.
int absolute ( int );
//Function Returns the absolute of a Double.
double absolute ( double );
//Function will calculate X^Y.
double power ( double, int );
//The function calculates the square root of a number using Newton’s method with 25 iterations.
double squareRoot( double );
//This function calculates the length of a hypotenuse given the length of the two sides of a right triangle.
double hypotenuse( double, double );
#endif
MyFunctions.cpp
#include "MyFunctions.h"
#include <iostream>
//Function Returns the value of the Larger of two Integers.
int maximum ( int val, int num )
{
int temp = val;
if ( num > val)
{
temp = num;
}
return temp;
}
//Function Returns the value of the Larger of two Integers.
double maximum ( double val, double num )
{
double temp = val;
if ( num > val)
{
temp = num;
}
return temp;
}
//Function Returns the Smaller of two Integers.
int minimum ( int val, int num )
{
int temp = val;
if ( num < val)
{
temp = num;
}
return temp;
}
//Function Returns the Smaller of two Doubles.
double minimum ( double val, double num )
{
double temp = val;
if ( num < val)
{
temp = num;
}
return temp;
}
//Function Returns the absolute of a Integer.
int absolute ( int num )
{
if( num >= 0 )
{
return num;
}
else
{
return -num;
}
}
//Function Returns the absolute of a Double.
double absolute ( double num )
{
if( num >= 0 )
{
return num;
}
else
{
return -num;
}
}
//Function will calculate X^Y.
double power ( double X, int Y )
{
double result = 1;
int i;
for (i=0; i < Y; i++) //don't know if this is right
{
result *= X;
}
return result;
}
//The function calculates the square root of a number using Newton’s method with 25 iterations.
double squareRoot( double num)
{
double guess;
double count = 0; //what do i need if the num is negative?
for ( guess = 1; count < 25; count++)
{
guess = ( ( num / guess ) + guess ) / 2;
}
return guess;
}
//This function calculates the length of a hypotenuse given the length of the two sides of a right triangle.
double hypotenuse( double a, double b )
{
double c , d;
d = (a * a) + (b * b);
c = //need help getting the squareRoot of d with out using cmath
return c;
}
Driver.cpp
#include "MyFunctions.h"
#include <iostream>
using namespace std;
int main ()
{
cout << " Testing maximum() function: \n";
if (( maximum ( 5, 3 ) == 5))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( maximum ( 3, 3 ) == 3))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( maximum ( 3, 5 ) == 5))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( maximum ( 5, -3 ) == 5))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( maximum ( -5, 3 ) == 3))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( maximum ( -5, -3 ) == -3))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( maximum ( -5, -5 ) == -5))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( maximum ( 5.0, 3.0) == 5.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( maximum ( 3.0, 3.0 ) == 3.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( maximum ( 3.0, 5.0 ) == 5.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( maximum ( 5.0, -3.0 ) == 5.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( maximum ( -5.0, 3.0 ) == 3.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( maximum ( -5.0, -3.0 ) == -3.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( maximum ( -5.0, -5.0 ) == -5.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
cout << " Testing minimum() function: \n";
if (( minimum ( 5, 3 ) == 3))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( minimum ( 3, 3 ) == 3))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( minimum ( 3, 5 ) == 3))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( minimum ( 5, -3 ) == -3))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( minimum ( -5, 3 ) == -5))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( minimum ( -5, -3 ) == -5))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( minimum ( -5, -5 ) == -5))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( minimum ( 5.0, 3.0) == 3.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( minimum ( 3.0, 3.0 ) == 3.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( minimum ( 3.0, 5.0 ) == 3.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( minimum ( 5.0, -3.0 ) == -3.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( minimum ( -5.0, 3.0 ) == -5.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( minimum ( -5.0, -3.0 ) == -5.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if (( minimum ( -5.0, -5.0 ) == -5.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
cout << "Testing absolute() function: \n";
if ((absolute ( 5 ) == 5))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if ((absolute ( 3 ) == 3))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if ((absolute ( -3 ) == 3))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if ((absolute ( -5 ) == 5))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if ((absolute ( 5.0 ) == 5.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if ((absolute ( 3.0 ) == 3.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if ((absolute ( -3.0 ) == 3.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if ((absolute ( -5.0 ) == 5.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
cout << "Testing power() function: \n";
if ((power ( 5.0, 2) == 25.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if ((power ( 3.0, 3 ) == 27.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if ((power ( 3.0, -2 ) == 1.5))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if ((power ( -5.0, 3 ) == -125.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if ((power ( -5.0, -2 ) == 2.5))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
cout << "Testing squareRoot() function: \n";
if (( squareRoot ( 4.0 ) == 2.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if ((squareRoot ( 25.0 ) == 5.0))
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
if ((squareRoot ( -4.0 ) == ? )) //don't know how to test this
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
cout << "Testing hypotenuse() function: \n";
if ((hypotenuse ( 3.0, 4.0 ) == 5)
{
cout << "Pass!" << endl;
}
else
{
cout << "\a" << endl;
}
return 0;
}