From bae2a3e3fcb1d406e43571880968d7a95aa25cf8 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Fri, 8 Feb 2019 23:43:27 -0500 Subject: [PATCH] Added getProduct() and fixed bugs in getDivisors() --- Algorithms.hpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Algorithms.hpp b/Algorithms.hpp index 3e3ee7f..ff01ef0 100644 --- a/Algorithms.hpp +++ b/Algorithms.hpp @@ -53,9 +53,12 @@ std::vector getFactors(T goalNumber); //This is a function that gets all the divisors of num and returns a vector containing the divisors template std::vector 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 T getSum(std::vector numbers); +//This is a function that returns the product of all elements in a vector +template +T getProduct(std::vector nums); //This is a function that searches a vecter for an element. Returns true if num is found in list template bool isFound(T num, std::vector list); @@ -204,10 +207,7 @@ std::vector getDivisors(T num){ } else if(num == 1){ divisors.push_back(1); - } - else{ - divisors.push_back(1); - divisors.push_back(num); + return divisors; } //You only need to check up to sqrt(num) T topPossibleDivisor = ceil(sqrt(num)); @@ -229,6 +229,7 @@ std::vector getDivisors(T num){ return divisors; } +//This is a function that returns the sum of all elements in a vector template T getSum(std::vector numbers){ T sum = 0; @@ -238,6 +239,16 @@ T getSum(std::vector numbers){ return sum; } +//This is a function that returns the product of all elmements in a vector +template +T getProduct(std::vector nums){ + T prod = 1; + for(T cnt = 0;cnt < nums.size();++cnt){ + prod *= nums.at(cnt); + } + return prod; +} + template bool isFound(T num, std::vector list){ for(int cnt = 0;cnt < list.size();++cnt){