code.cpp 534 B

1234567891011121314151617181920212223242526
  1. /* Create a function that returns the product of all odd
  2. * integers in an array.
  3. */
  4. int humph(std::vector<int> arr, int pnok = 1) {
  5. bool ok = false;
  6. for(int i = 0; i < arr.size(); i++) if(arr[i]%2) {
  7. pnok *= arr[i];
  8. ok = true;
  9. }
  10. return ok ?pnok :0;
  11. }
  12. int oddProduct(std::vector<int> arr) {
  13. return humph(arr);
  14. std::vector<int> odds;
  15. int result = 1;
  16. for(int i = 0;i<arr.size();i++){
  17. if((arr[i]%2) == 1){
  18. odds.push_back(arr[i]);
  19. }
  20. }
  21. for(int j = 0;j<odds.size();j++){
  22. result*=odds[j];
  23. }
  24. return result;
  25. }