mirror of
https://bitbucket.org/Mattrixwv/myhelpers.git
synced 2025-12-06 18:43:59 -05:00
Added getSum and getProd functions
This commit is contained in:
@@ -298,4 +298,28 @@ bool isSortedDynamicInt64Array(struct DynamicInt64Array* ary){
|
||||
return true;
|
||||
}
|
||||
|
||||
//This function returns the sum of all elements in a DynamicInt64Array
|
||||
int64_t getSumDynamicInt64Array(struct DynamicInt64Array* ary){
|
||||
int64_t sum = 0;
|
||||
//Look through every element in the array, adding the numbers to a running sum
|
||||
for(uint64_t location = 0;location < ary->size;++location){
|
||||
sum += ary->ptr[location];
|
||||
}
|
||||
|
||||
//Return the sum
|
||||
return sum;
|
||||
}
|
||||
|
||||
//This function returns the product of all elements in a DynamicInt64Array
|
||||
int64_t getProdDynamicInt64Array(struct DynamicInt64Array* ary){
|
||||
int64_t product = 1;
|
||||
//Look through every element in the array, multiplying it
|
||||
for(uint64_t location = 0;location < ary->size;++location){
|
||||
product *= ary->ptr[location];
|
||||
}
|
||||
|
||||
//Return the product
|
||||
return product;
|
||||
}
|
||||
|
||||
#endif //DYNAMIC_ARRAY_H
|
||||
|
||||
Reference in New Issue
Block a user