The ability to write functions is one of the more powerful c
The ability to write functions is one of the more powerful capabilities in C++. Functions allow code to be reused, and provides a mechanism for introducing the concept of modularity to the programming process. In addition to the concept of functions, C++ allows complex programs to be organized using header files (.h files) that contain the prototypes of functions and implementation files that (.cpp files) that contain the implementation (definition) of the functions.
In this programming assignment you will be asked to implement a number of useful functions using a header file and an implementation file. You will place the prototypes for your function in a .h file, and implement these functions in a .cpp file. You will need to write a driver (a program designed to test programs) to test your functions before you upload them.
[edit]Deliverables:
main.cpp
myFunctions.cpp
myFunctions.h
[edit]Function:
[edit]max
Precondition
two integer values exist
Postcondition
The value of the largest integer is returned.
The original integers are unchanged
If the integers have the same value then the value of either integer is returned.
Return
integer
Description
Function returns the value of the larger of two integers.
Prototype:
int max ( int, int )
Sample Parameter Values
[edit]max
Precondition
two double values exist
Postcondition
The value of the larger double is returned.
The original doubles are unchanged
If the doubles have the same value then the value of either double is returned.
Return
double
Description
Function returns the value of the larger of two doubles.
Prototype:
double max (double, double)
Sample Parameter Values
[edit]min
Precondition
two integer values exist
Postcondition
The value of the smallest integer is returned.
The original integers are unchanged
If the integers have the same value then the value of either integer is returned.
Return
integer
Description
Function returns the value of the smaller of two integers.
Prototype:
int min ( int, int )
Sample Parameter Values
[edit]min
Precondition
two double values exist
Postcondition
The value of the smaller double is returned.
The original double are unchanged
If the double have the same value then the value of either double is returned.
Return
double
Description
Function returns the value of the smaller of two double.
Prototype:
double min (double, double)
Sample Parameter Values
[edit]absoluteValue
Precondition
Some integer value exists
Postcondition
Integer value is unchanged
Return
Integer
Description
This function returns the absolute value of an integer
Prototype:
int absoluteValue ( int )
Sample Parameter Values
[edit]absoluteValue
Precondition
Some double value exists
Postcondition
double value is unchanged
Return
double
Description
This function returns the absolute value of a double
Prototype:
double absoluteValue ( double )
Sample Parameter Values
[edit]squareRoot
Precondition
Some integer radicand X exists
Postcondition
The value of the radicand X is unchanged
Return
double
Description
The function calculates the square root of a number using Newton’s method with 25 iterations.
Prototype:
double squareRoot( int )
Sample Parameter Values
[edit]Factorial
Precondition
Some integer value N exists
Postcondition
The value of the N unchanged
Return
double
Description
The function calculates factorial.
Prototype:
double factorial( int )
formula:
Sample Parameter Values
[edit]Combination
Precondition
Two integer value M and N exists
Postcondition
The value of the M and N unchanged
Return
The number of combinations of M items taken N at a time.
Description
The function calculates the number of combinations of M items taken N at a time.
Prototype:
double combination( int M, int N)
formula:
Sample Parameter Values
[edit]Permutation
Precondition
Two integer value M and N exists
Postcondition
The value of the M and N unchanged
Return
The number of permutations of M items taken N at a time.
Description
The function calculates the number of permutations of M items taken N at a time.
Prototype:
double permutation( int M, int N)
Formula:
Sample Parameter Values
Newton\'s method for calculating the square root of N starts by making a (positive number) guess at the square root. It then uses the original guess to calculate a new guess, according to the following formula:
guess = (( N / guess) + guess) / 2 ;
No matter how wild the original guess is, if we repeat this calculation the algorithm will eventually find the square root of N.
| m | n | max(m,n) |
| 5 | 10 | 10 |
| -3 | -6 | -3 |
Solution
// myFunctions.h
#ifndef myFunctions_H
#define myFunctions_H
class Functions
{
public:
// returns the maximum of two integers
int max ( int, int );
// returns maximum of 2 doubles
double max (double, double);
// returns minimum of two integers
int min ( int, int );
// returns minimum of two doubles
double min (double, double);
//This function returns the absolute value of an integer
int absoluteValue ( int );
// returns absolute value of double
double absoluteValue ( double );
//The function calculates the square root of a number using Newton’s method with 25 iterations.
double squareRoot( int );
// returns factorial of number
double factorial( int );
//The function calculates the number of combinations of M items taken N at a time.
double combination( int , int);
//The function calculates the number of permutations of M items taken N at a time.
double permutation( int , int);
};
#endif
// myFunctions.cpp
#include \"myFunctions.h\"
int Functions::max ( int n, int m )
{
if(n > m) return n;
else return m;
}
// returns maximum of 2 doubles
double Functions::max (double n, double m)
{
if(n > m) return n;
else return m;
}
// returns minimum of two integers
int Functions::min ( int n, int m)
{
if(n < m) return n;
else return m;
}
// returns minimum of two doubles
double Functions::min (double n, double m)
{
if(n < m) return n;
else return m;
}
//This function returns the absolute value of an integer
int Functions::absoluteValue ( int n )
{
return (-1)*n;
}
// returns absolute value of double
double Functions::absoluteValue ( double n )
{
return (-1)*n;
}
//The function calculates the square root of a number using Newton’s method with 25 iterations.
double Functions::squareRoot( int N )
{
double guess = 1+ (N-1)/2;
for (int i = 0; i < 25; ++i)
{
guess = (( N / guess) + guess) / 2 ;
}
return guess;
}
// returns factorial of number
double Functions::factorial( int n )
{
int f = 1;
for(int i = 1; i <=n; ++i)
{
f *= i;
}
return f;
}
//The function calculates the number of combinations of M items taken N at a time.
double Functions::combination( int m , int n)
{
return factorial(m)/(factorial(n) * factorial(m-n));
}
//The function calculates the number of permutations of M items taken N at a time.
double Functions::permutation( int m , int n)
{
return factorial(m)/factorial(m-n);
}
// main.cpp
#include <iostream> // std::cout, std::fixed
#include \"myFunctions.h\"
using namespace std;
int main()
{
Functions f;
cout << f.max (5,10) << endl;
// output: 10
cout << f.max (5.5,1.7) << endl;
// output: 5.5
cout << f.min (5,10) << endl;
// output: 5
cout << f.min (5.5,1.7) << endl;
// output: 1.1
cout << f.absoluteValue(-8) << endl;
// output: 8
cout << f.absoluteValue (-3.6) << endl;
// output: 3.6
cout << f.squareRoot(42) << endl;
// output: 6.48074
cout << f.factorial(5) << endl;
// output: 120
cout << f.combination(5,3) << endl;
// output: 10
cout << f.permutation(5,3) << endl;
// output: 60
return 0;
}






