mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Fixed bug for empty array in get product
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
//myClasses/Algorithms.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-8-18
|
||||
//Modified: 1-30-19
|
||||
// Created: 11-08-18
|
||||
//Modified: 02-28-19
|
||||
//This file contains the declarations and implementations to several algoritms that I have found useful
|
||||
/*
|
||||
Copyright (C) 2018 Matthew Ellison
|
||||
Copyright (C) 2019 Matthew Ellison
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
@@ -244,6 +244,12 @@ T getSum(const std::vector<T>& ary){
|
||||
//This is a function that returns the product of all elmements in a vector
|
||||
template <class T>
|
||||
T getProduct(const std::vector<T>& ary){
|
||||
//Make sure there is something in the array
|
||||
if(ary.size() == 0){
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Multiply all elements in the array together
|
||||
T prod = 1;
|
||||
for(T cnt = 0;cnt < ary.size();++cnt){
|
||||
prod *= ary.at(cnt);
|
||||
|
||||
Reference in New Issue
Block a user