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;

}

Edited 6 Years Ago by Christoph928: had a typo in file heading

Sorry I know this is a long post, but I am stuck and would appreciate the help.

Stuck WHERE? Describe the problem. Does it compile? Does it run to completion? Does it give inaccurate results? Do you need help designing it? Point us to a specific problem and specific line numbers. Post any error messages.

Stuck WHERE? Describe the problem. Does it compile? Does it run to completion? Does it give inaccurate results? Do you need help designing it? Point us to a specific problem and specific line numbers. Post any error messages.

Sorry for being vague, I would say I'm stuck on lines 88 - 93

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

, 100 - 110 would like to know what to put if negative number

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

, and 117 - 123 don't know how to get the square root of d (can't use cmath)

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

in MyFunctions .cpp.

I would also like help with this:

if ((squareRoot ( -4.0 ) == ? ))	//don't know how to test this	
{		
cout << "Pass!" << endl;	
}	
else	
{		
cout << "\a" << endl;	
}

testing squareRoot for a negative...Thanks

Edited 6 Years Ago by Christoph928: wow totally messed up code blocks

you have already a function to calculate SquareRoot in <cmath>

#include <cmath>
using namespace std;
int num = 144;
cout<<"Square root of "<<num<<" is "<<sqrt(num)<<endl;

checking for negative number? try this

if(num<0){
//number is negative
//do whatever you want
}
else{
//number is positive
//do what you want
}
if what you want is just skip negatives you can go with this
if(!num<0){
//do something
}

Edited 6 Years Ago by Stefano Mtangoo: fix code tags

you have already a function to calculate SquareRoot in <cmath>

#include <cmath>
using namespace std;
int num = 144;
cout<<"Square root of "<<num<<" is "<<sqrt(num)<<endl;

Yes, i know but my prof does not want us using cmath, but to create our own functions to calculate in place of cmath.

don't know how to get the square root of d (can't use cmath)

what is issue with cmath? Have lecturer banned you from using it?
Cool then here is the necessary algorithm to do it.
See this one

what is issue with cmath? Have lecturer banned you from using it?
Cool then here is the necessary algorithm to do it.
See this one

Thank you i figured out how to do the sqrt of d but i need help with my Driver program. How would i test my squareRoot() function for a negative? Here is what i have so far:

if ((squareRoot ( -4.0 ) == ? ))	//don't know how to test this	
{		
cout << "Pass!" << endl;	
}	
else	
{		
cout << "\a" << endl;	
}

and I also need help with my power() function, if the exponent is a negative what do i need to do...Here is what i have:

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

Thank you i figured out how to do the sqrt of d but i need help with my Driver program.

Square root is defined for non-negative numbers. Your square root function should test for negativity before it does anything else. If it's negative, it should return something like -1 or it should abort the program or it should fill in some error flag or whatever. Define what it is supposed to do for illegal parameters. Having it return -1 is the easiest. Test that return value.

Unfortunately, your spec says to return 0, which is a LEGITIMATE square root value, but do what you're told.


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

and I also need help with my power() function, if the exponent is a negative what do i need to do

Make the exponent positive and return the reciprocal.

Square root is defined for non-negative numbers. Your square root function should test for negativity before it does anything else. If it's negative, it should return something like -1 or it should abort the program or it should fill in some error flag or whatever. Define what it is supposed to do for illegal parameters. Having it return -1 is the easiest. Test that return value.

Unfortunately, your spec says to return 0, which is a LEGITIMATE square root value, but do what you're told.

Make the exponent positive and return the reciprocal.

ok , but how would i code the reciprocal of the number ?

ok , but how would i code the reciprocal of the number ?

double number = 5.0;
double reciprocalOfNumber =  1.0 / number;

Thank you i figured out how to do the sqrt of d but i need help with my Driver program.

well glad I helped!
Let's go again!

How would i test my squareRoot() function for a negative?

Well answered by Vernon Dosier

and I also need help with my power() function, if the exponent is a negative what do i need to do...Here is what i have: double

let see
if if x is raised to power of -y then it is equal to reciprocal of x raised to y. If that is true, mystery is then solved as your function would recurse itself y times while each times accumulating x*x; The final answer is then reciprocated and that is the power. Note that test for y and if it is negative (i.e <0), do reciprocal before retuning an answer. Otherwise don't reciprocate. That will solve for both positive and negative values of y

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.