Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write a C++ function for the following prototypes: (without using math.c

ID: 3565604 • Letter: P

Question

Please write a C++ function for the following prototypes: (without using math.c lib)

Also write a driver file that will test the functions and print pass if they are correct.

int absoulteValue (int);

double absoluteValue (double);

double squareRoot(int);

double fractorial(int);

Sample Parameter Values

double combination (int M, int N);


double permutation( int M, int N);

Sample Parameter Values

m factorial(m) 5 120 8 40320 Please write a C++ function for the following prototypes: (without using math.c lib) Also write a driver file that will test the functions and print pass if they are correct. int absoulteValue (int); double absoluteValue (double); double squareRoot(int); double fractorial(int); mPn = m!/(m - n)! Sample Parameter Values m n permutation(m,n) 5 3 60 8 2 56 mCn = m!/n!(m - n)! m n combination(m,n) 5 3 10 8 2 28 double permutation( int M, int N); n! = {1, if n = 0 n k = 1 (k) if n > 0 Sample Parameter Values m factorial(m) 5 120 8 40320 double combination (int M, int N);

Explanation / Answer

#include<iostream>

#define PRECISION 0.00001

using namespace std;

int myAbs(int num){
if(num > 0){
return num;
}else{
return -num;
}
}

double myFabs(double num){
if(num > 0){
return num;
}else{
return -num;
}
}

double SqrtNumber(double num){
double lower_bound=0;
double upper_bound=num;
double temp=0;

while(myFabs(num - (temp * temp)) > PRECISION){
temp = (lower_bound+upper_bound)/2;
if (temp*temp >= num){
upper_bound = temp;
}
else{
lower_bound = temp;
}
}
return temp;
}

double factorial(int num){
if(num ==0 || num == 1){
return 1;
}
double fact = 1;

while(num > 1){
fact = fact *num;
num--;
}
return fact;
}

double Combination(int m, int n){
double res = factorial(m)/(factorial(n)*(factorial(m-n)));

return res;
}

double permutation(int m, int n){
double res = factorial(m)/factorial(m-n);

return res;
}


int main(){
if(myAbs(-5) == 5){
cout << "Pass" << endl;
}else{
cout << "Fail" << endl;
}

if(myFabs(-6.9) == 6.9){
cout << "Pass" << endl;
}else{
cout << "Fail" << endl;
}

if(SqrtNumber(9)-3.00 < PRECISION){
cout << "Pass" << endl;
}else{
cout << "Fail" << endl;
}

if(factorial(8) == 40320){
cout << "Pass" << endl;
}else{
cout << "Fail" << endl;
}

if(Combination(8, 2) == 28){
cout << "Pass" << endl;
}else{
cout << "Fail" << endl;
}

if(permutation(8, 2 ) == 56){
cout << "Pass" << endl;
}else{
cout << "Fail" << endl;
}

return 0;
}