Write a recursive, int -valued function named productOfOdds that accepts an integer array, and the number of?
-
Write a recursive, int -valued function named productOfOdds that accepts an integer array, and the number of elements in the array and returns the product of the odd-valued elements of the array. You may assume the array has at least one odd-valued element. The product of the odd-valued elements of an integer-valued array recursively may be calculated as follows: If the array has a single element and it is odd, return the value of that element; otherwise return 1. Otherwise, if the first element of the array is odd, return the product of that element and the result of finding the product of the odd elements of the rest of the array; if the first element is NOT odd, simply return the result of finding the product of the odd elements of the rest of the array my answer is(but wrong): void productOfOdds(int a[], int n) { if (n < 0) {return 0;} if (n > 1) {return a[n-1]} if(a[0]>1){ productOfOdds(a+1, n-1); return 1;} } with C++ please.
-
Answer:
With such clear instructions, the function practically writes itself. You have a few problems, including: . your function's return value is not int . you're not checking for odd values . your function does not compute a sum It should look like this: int productOfOdds(const int *a, size_t n) { if (n == 1) { if (a[0] & 1) { return a[0]; } else { return 1; } } if (a[0] & 1) { return a[0] * productOfOdds(a + 1, n - 1); } else { return 0 + productOfOdds(a + 1, n - 1); } }
abn at Yahoo! Answers Visit the source
Other answers
well, go ahead and do your homework. it will be a good execrise for you. I am sure recursion was covered in class. I hope you took good notes for this one. you can think of your instructor kind of like your engineering manager. he may even say that. if he does, good for him. he's giving you good instruction.
Jim
Show your code. = People will be able to help you get it right.
tbshmkr
Related Q & A:
- Is it correct to extend a bean class and write a class which holds logic to populate the bean we extend?Best solution by stackoverflow.com
- How does one create a recursive lexer?Best solution by stackoverflow.com
- How long should I give a prof to write a letter of recommendation for a graduate school application?Best solution by Yahoo! Answers
- How do I write a resume for a part-time job?Best solution by Yahoo! Answers
- How to write a persuasive letter to a bank?Best solution by wikihow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.