1234567891011121314151617181920212223242526 |
- /* Create a function that returns the product of all odd
- * integers in an array.
- */
- int humph(std::vector<int> arr, int pnok = 1) {
- bool ok = false;
- for(int i = 0; i < arr.size(); i++) if(arr[i]%2) {
- pnok *= arr[i];
- ok = true;
- }
- return ok ?pnok :0;
- }
- int oddProduct(std::vector<int> arr) {
- return humph(arr);
- std::vector<int> odds;
- int result = 1;
- for(int i = 0;i<arr.size();i++){
- if((arr[i]%2) == 1){
- odds.push_back(arr[i]);
- }
- }
- for(int j = 0;j<odds.size();j++){
- result*=odds[j];
- }
- return result;
- }
|