mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Added getProduct() and fixed bugs in getDivisors()
This commit is contained in:
@@ -53,9 +53,12 @@ std::vector<T> getFactors(T goalNumber);
|
|||||||
//This is a function that gets all the divisors of num and returns a vector containing the divisors
|
//This is a function that gets all the divisors of num and returns a vector containing the divisors
|
||||||
template<class T>
|
template<class T>
|
||||||
std::vector<T> getDivisors(T num);
|
std::vector<T> getDivisors(T num);
|
||||||
//This is a function that gets the sum of all elements in a vector and returns the number
|
//This is a function that returns the sum of all elements in a vector
|
||||||
template <class T>
|
template <class T>
|
||||||
T getSum(std::vector<T> numbers);
|
T getSum(std::vector<T> numbers);
|
||||||
|
//This is a function that returns the product of all elements in a vector
|
||||||
|
template <class T>
|
||||||
|
T getProduct(std::vector<T> nums);
|
||||||
//This is a function that searches a vecter for an element. Returns true if num is found in list
|
//This is a function that searches a vecter for an element. Returns true if num is found in list
|
||||||
template<class T>
|
template<class T>
|
||||||
bool isFound(T num, std::vector<T> list);
|
bool isFound(T num, std::vector<T> list);
|
||||||
@@ -204,10 +207,7 @@ std::vector<T> getDivisors(T num){
|
|||||||
}
|
}
|
||||||
else if(num == 1){
|
else if(num == 1){
|
||||||
divisors.push_back(1);
|
divisors.push_back(1);
|
||||||
}
|
return divisors;
|
||||||
else{
|
|
||||||
divisors.push_back(1);
|
|
||||||
divisors.push_back(num);
|
|
||||||
}
|
}
|
||||||
//You only need to check up to sqrt(num)
|
//You only need to check up to sqrt(num)
|
||||||
T topPossibleDivisor = ceil(sqrt(num));
|
T topPossibleDivisor = ceil(sqrt(num));
|
||||||
@@ -229,6 +229,7 @@ std::vector<T> getDivisors(T num){
|
|||||||
return divisors;
|
return divisors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//This is a function that returns the sum of all elements in a vector
|
||||||
template <class T>
|
template <class T>
|
||||||
T getSum(std::vector<T> numbers){
|
T getSum(std::vector<T> numbers){
|
||||||
T sum = 0;
|
T sum = 0;
|
||||||
@@ -238,6 +239,16 @@ T getSum(std::vector<T> numbers){
|
|||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//This is a function that returns the product of all elmements in a vector
|
||||||
|
template <class T>
|
||||||
|
T getProduct(std::vector<T> nums){
|
||||||
|
T prod = 1;
|
||||||
|
for(T cnt = 0;cnt < nums.size();++cnt){
|
||||||
|
prod *= nums.at(cnt);
|
||||||
|
}
|
||||||
|
return prod;
|
||||||
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
bool isFound(T num, std::vector<T> list){
|
bool isFound(T num, std::vector<T> list){
|
||||||
for(int cnt = 0;cnt < list.size();++cnt){
|
for(int cnt = 0;cnt < list.size();++cnt){
|
||||||
|
|||||||
Reference in New Issue
Block a user