mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Added function to find factorial of number
This commit is contained in:
@@ -576,6 +576,15 @@ std::vector<std::string> split(std::string str, char delimiter){
|
|||||||
return splitStrings;
|
return splitStrings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Return the factorial of the number passed in
|
||||||
|
template <class T>
|
||||||
|
T factorial(T num){
|
||||||
|
T fact = 1;
|
||||||
|
for(T cnt = 1;cnt <= num;++cnt){
|
||||||
|
fact *= cnt;
|
||||||
|
}
|
||||||
|
return fact;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,15 +53,16 @@ bool testSearch();
|
|||||||
bool testFindMin();
|
bool testFindMin();
|
||||||
bool testFindMax();
|
bool testFindMax();
|
||||||
bool testFindNumOccurrence();
|
bool testFindNumOccurrence();
|
||||||
|
bool testFactorial();
|
||||||
|
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
mee::Stopwatch timer;
|
mee::Stopwatch timer;
|
||||||
bool passedTest = false;
|
bool passedTest = false;
|
||||||
std::vector<boolFn> functions {testGetPrimes, testGetNumPrimes, testIsPrime, testGetFactors, tetsGetDivisors, testGetSum, testGetProduct, testIsFound, testGetPermutations,
|
std::vector<boolFn> functions {testGetPrimes, testGetNumPrimes, testIsPrime, testGetFactors, tetsGetDivisors, testGetSum, testGetProduct, testIsFound, testGetPermutations,
|
||||||
testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax, testFindNumOccurrence};
|
testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax, testFindNumOccurrence, testFactorial};
|
||||||
std::vector<std::string> names {"getPrimes", "getNumPrimes", "isPrime", "getFactors", "getDivisors", "getSum", "getProduct", "isFound", "getPermutations",
|
std::vector<std::string> names {"getPrimes", "getNumPrimes", "isPrime", "getFactors", "getDivisors", "getSum", "getProduct", "isFound", "getPermutations",
|
||||||
"getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax", "findNumOccurrence"};
|
"getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax", "findNumOccurrence", "factorial"};
|
||||||
|
|
||||||
//Start doing tests and print out the results of each
|
//Start doing tests and print out the results of each
|
||||||
for(int cnt = 0;cnt < functions.size();++cnt){
|
for(int cnt = 0;cnt < functions.size();++cnt){
|
||||||
@@ -482,6 +483,35 @@ bool testFindNumOccurrence(){
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool testFactorial(){
|
||||||
|
//Test 1
|
||||||
|
int num = 1;
|
||||||
|
int correctAnswer = 1;
|
||||||
|
int answer = mee::factorial(num);
|
||||||
|
if(correctAnswer != answer){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Test 2
|
||||||
|
num = 10;
|
||||||
|
correctAnswer = 3628800;
|
||||||
|
answer = mee::factorial(num);
|
||||||
|
if(correctAnswer != answer){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Test 3
|
||||||
|
num = -5;
|
||||||
|
correctAnswer = 1;
|
||||||
|
answer = mee::factorial(num);
|
||||||
|
if(correctAnswer != answer){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//If it hasn't failed a test then return true for passing all the tests
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
Function getPrimes() passed the test
|
Function getPrimes() passed the test
|
||||||
The test took 0.000 nanoseconds
|
The test took 0.000 nanoseconds
|
||||||
|
|||||||
Reference in New Issue
Block a user